# react-stub-component

> A StubComponent library for React

Latest version **1.0.4** (published 2022-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-stub-component
pnpm add react-stub-component
yarn add react-stub-component
bun add react-stub-component
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-09-19 |
| First published | 2022-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | RickCardoso |
| Maintainers | rick.cardoso |
| Keywords | stub, react, test |

## Links

- npm: https://www.npmjs.com/package/react-stub-component
- Repository: https://github.com/RickCardoso/react-stub-component
- Homepage: https://github.com/RickCardoso/react-stub-component#readme
- Issues: https://github.com/RickCardoso/react-stub-component/issues
- npm.io page: https://npm.io/package/react-stub-component

## Dependencies (2)

- [sinon](https://npm.io/package/sinon.md) ^14.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-09-19
- 1.0.4-rc.5 (rc) — 2022-09-19
- 1.0.0-rc.2 (canary) — 2022-08-17
- 1.0.4-rc.4 — 2022-09-19
- 1.0.4-rc.3 — 2022-08-23
- 1.0.4-rc.2 — 2022-08-23
- 1.0.4-rc.1 — 2022-08-23
- 1.0.3 — 2022-08-19
- 1.0.2 — 2022-08-19
- 1.0.1 — 2022-08-18
- 1.0.0-rc.8 — 2022-08-18
- 1.0.0-rc.7 — 2022-08-18
- 1.0.0-rc.6 — 2022-08-18
- 1.0.0-rc.5 — 2022-08-17
- 1.0.0-rc.4 — 2022-08-17
- … 2 more at https://npm.io/package/react-stub-component/versions

## README

# react-stub-component

This library has the sole purpose of simplifying React component unit testing by 
providing a stub component that can be used to test the component without actually 
having to render the component itself or rely on internal subcomponent logic.

## Getting Started

Install the library:

```shell
#NPM
npm i react-stub-component --save-dev
```
```shell
#Yarn
yarn add react-stub-component --dev
```
```shell
#Rush
rush add -p react-stub-component --dev
```

## Usage

The library has one main method called `stubComponent` that takes the following:
- `module`: the actual module of the component you want to stub
- `componentName`: the name of the component you want to stub from that module

Then, with `sinon`, we create a component stub and return the following properties:
- `getPropValue: (propName: string) => any`: the function you will use to get the value of a prop provided to the stubbed component
- `getReactNodePropTestId: (propName: string) => any`: the function you will use to get the value of a ReactNode prop provided to the stubbed component
- `fireMockEvent: (propName: string, ...params: any[]) => void`: the function you will use to fire a mock event on the stubbed component with parameters
- `stubTestId: string`: the test id of the stubbed component which you can use to validate if it has been simply rendered
- `restoreStubs: () => void`: the function you will use to restore the stubs and actually use real components

```jsx
import { stubComponent } from 'react-stub-component';
import * as YourComponentModule from 'components/YourComponent';

const { getPropValue, fireMockEvent, stubTestId } = stubComponent(YourComponentModule, 'YourComponent');

const setup = () => render(<YourParentComponent />);

describe('Example', () => {
  describe('check subcomponents', () => {
    it('should render YourComponent', () => {
      const { getByTestId } = setup();
      expect(getByTestId(stubTestId)).toBeInTheDocument();
    });
    
    it('should render YourComponent with correct props', () => {
      const { getByTestId } = setup();
      expect(getPropValue('prop1')).toBe('value1');
      expect(getPropValue('prop2')).toBe('value2');
    });
    
    describe('when YourComponent triggers callback', () => {
      it('should call parentCallback with same params', () => {
        const parentCallback = jest.fn();
        const { getByTestId } = setup({ parentCallback });
        fireMockEvent('onCallback', onCallbackParams);
        expect(parentCallback).toHaveBeenCalledWith(onCallbackParams);
      });
    });
  });
});
```

---
_Source: https://npm.io/package/react-stub-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
