1.0.4 • Published 2 years ago
react-stub-component v1.0.4
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:
#NPM
npm i react-stub-component --save-dev
#Yarn
yarn add react-stub-component --dev
#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 stubcomponentName
: 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 componentgetReactNodePropTestId: (propName: string) => any
: the function you will use to get the value of a ReactNode prop provided to the stubbed componentfireMockEvent: (propName: string, ...params: any[]) => void
: the function you will use to fire a mock event on the stubbed component with parametersstubTestId: string
: the test id of the stubbed component which you can use to validate if it has been simply renderedrestoreStubs: () => void
: the function you will use to restore the stubs and actually use real components
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);
});
});
});
});
1.0.4-rc.3
2 years ago
1.0.4-rc.4
2 years ago
1.0.4-rc.1
2 years ago
1.0.4-rc.2
2 years ago
1.0.4
2 years ago
1.0.4-rc.5
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0-rc.8
2 years ago
1.0.0-rc.7
2 years ago
1.0.0-rc.6
2 years ago
1.0.0-rc.5
2 years ago
1.0.0-rc.4
2 years ago
1.0.0-rc.3
2 years ago
1.0.0-rc.2
2 years ago
1.0.0-rc.1
2 years ago
1.0.0
2 years ago