我想在运行按钮onPress功能后注册组件的快照。
这是我的测试套件:
describe('Login component', () => {
// This runs fine. Snapshot OK
it('Renders correctly', () => {
const tree = renderer.create(<Login />).toJSON()
expect(tree).toMatchSnapshot()
})
// This does not run fine. Snapshot is 'null'
it('Contains error message when submit with empty fields', () => {
const login = renderer.create(<Login />)
const instance = login.getInstance()
// Submit the form (default values = empty)
instance.submit()
// Force update (rerender) the component
login.update()
expect(login.toJSON()).toMatchSnapshot()
})
})
上面的测试套件创建了两个快照。第一个测试在默认条件下快照组件的结构,并且似乎正确注册。
第二个测试应该在提交运行后记录组件结构,显示错误消息,提醒用户该表单不能用空字段提交。
不幸的是,在login.update()
运行后,login.toJSON()
会返回null
,这不会按预期运行。
我显然做错了什么,但是什么? The docs似乎表明update
符合我的想法。