I'm writing the test suite of my React Native application and I was wondering how to test the shallow rendering of a React Native child component with Jest & Enzyme.
import { shallow } from 'enzyme'
import toJson from 'enzyme-to-json'
import React from 'react'
import NextButton from './NextButton'
describe('NextButton component', () => {
describe('Rendering', () => {
let onPress, text, wrapper
beforeEach(() => {
onPress = jest.fn()
text = 'Test button'
wrapper = shallow(<NextButton onPress={onPress}>{text}</NextButton>)
})
test('should render NextButton correctly', () => {
expect(toJson(wrapper)).toMatchSnapshot()
})
test('should have text rendered as a child', () => {
expect(toJson(wrapper.prop('children'))).toEqual(text)
})
})
})
The previous code gives this error, Jest doesn't find the children
prop.
Expected: "Test button"
Received: undefined
I think you just need to test the text
of your component (and not its props), try with:
expect(wrapper.text()).toEqual(text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With