Last active
July 5, 2020 08:56
-
-
Save fitrianij/f0dd4872db8de789089dfaa84c33a6c8 to your computer and use it in GitHub Desktop.
Cheat Sheet for Jest and Enzyme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Enzyme, { mount } from 'enzyme'; | |
// mount a component with props | |
const loginProps = { title: 'Login Form' } | |
const loginForm = mount(<LoginPage {...loginProps} />); | |
// access child component based on prop key value | |
const passwordField = loginForm.find('FormItem').filterWhere((item => { | |
return item.prop('name') === 'password'; | |
})); | |
// check a prop value | |
expect(loginform.prop('title')).toBe('Login Form'); | |
// check a state value | |
expect(loginForm.state('password')).toEqual('123456'); | |
// Check effect after submit a form | |
const flushPromises = () => new Promise(setImmediate); | |
loginForm.find('form').simulate('submit', event); | |
await flushPromises(); | |
loginForm.update(); | |
expect(loginForm.state('error')).toEqual('incorrect password'); | |
// test with js-cookie | |
import Cookie from 'js-cookie'; | |
it('sets token to cookie', async() => { | |
// other codes ... | |
loginForm.find('form').simulate('submit', event); | |
await flushPromises(); | |
loginForm.update(); | |
expect(Cookie.get('token')).toEqual('token-mock'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment