It's a list of snippets, that will help you test your component properly.
- Proper values
- Passing undefined or null value
- In case you are calculating something:
- proper numbers
- zero values (helps to test dividing)
- null/undefined (worth to check NaN or infinite values)
importReactfrom'react';import{render}from'react-native-testing-library';import{Button}from'components';describe('components/Button',()=>{constdefaultProps={text: 'Hello world',};test('snapshot',()=>{consttree=render(<Button{...defaultProps}/>).toJSON();expect(tree).toMatchSnapshot();});});importReactfrom'react';import{render,fireEvent}from'react-native-testing-library';import{Button}from'components';describe('components/Button',()=>{constdefaultProps={text: 'Hello World'onPress: jest.fn(),};test('Should handle click',()=>{const{ getByTestId }=render(<ButtonCardtestID="button"{...defaultProps}/>,);fireEvent.press(getByTestId('button'));expect(defaultProps.onPress).toHaveBeenCalledTimes(1);});});Simple test to check changes in snapshots. Pretty useful, but not really testing logic, but just props and style. Easy to ignore.
It should be only your base. You will start here and add more tests to it.
describe('Component: EXAMPLECOMPONENT',()=>{describe('with basic props',()=>{constprops={
...defaultProps}constwrapper=shallow(<EXAMPLECOMPONENT{...props}/>)it('Should render snapshot properly',()=>{expect(wrapper).toMatchSnapshot()})})})To make sure some components are on screen you can easily add test to find it.
it('Should render properly',()=>{expect(wrapper.find(View).length).toBe(1)})<ViewtestID="COMPONENT_TO_TEST">it('Should has component',()=>{constcomponent=wrapper.findWhere(node=>node.prop('testID')==='COMPONENT_TO_TEST')expect(component).toBeDefined()})Sometimes we need to has some specific styles, e.g. mandatory style for button.
it('Should has mandatory style',()=>{constcomponent=wrapper.findWhere(node=>node.prop('testID')==='COMPONENT_TO_TEST')expect(component.props().style).toHaveProperty('color','red')})it('Should has mandatory style',()=>{constexpectedStyle={color: 'red'}constcomponent=wrapper.findWhere(node=>node.prop('testID')==='COMPONENT_TO_TEST')expect(component.props().style).toMatch(expectedStyle)})Sometimes we have some helpers functions inside our components, e.g. getIcon, getValue or anything.
import{icons}from'../assets'it('Should return default icon',()=>{consticon=wrapper.instance().getIcon()expect(icon).toEqual(icons.default)})it('Should return 42',()=>{wrapper.instance().makeCalculation()wrapper.update()constvalue=wrapper.instance().getValue()expect(value).toEqual(42)})Sometimes we pass function through props like this:
<MyComponentonPress={()=>amazing()}/>and inside MyComponent we use it like this to call this amazing function
<TouchableOpacityonPress={()=>this.props.onPress()}>So we can check does this prop was called and how many times
it('Should handle item click',()=>{wrapper.find(TouchableOpacity).simulate('press')expect(props.onPress).toHaveBeenCalledTimes(1)})