使用技术
概要
- 测试 props
- 测试 state
- 测试函数调用
1. 测试 props
import React from 'react'
import { shallow } from 'enzyme'
it('disabled, cursor should be "not-allowed"', () => {
const wrapper = shallow(<LikeButton disabled />)
expect(wrapper.prrops().style).toHaveProperty('cursor', 'not-allowed')
})
2. 测试 state
import React from 'react'
import { shallow } from 'enzyme'
it('mouse enter, hover should be true', () => {
const wrapper = shallow(<LikeButton />)
wrapper.find('.common').simulate('mouseenter')
// == wrapper.state('hover')
expect(wrapper.state().hover).toEqual(true)
})
3. 测试函数调用
import React from 'react'
import { shallow } from 'enzyme'
it('TextArea onChange', () => {
// mock
const onchange = jest.fn()
const wrapper = shallow(
<SimpleText
type="simple-text"
value="This is a Simple Text."
onChange={onchange}
/>
)
wrapper.find(TextArea).simulate('change', {
target: { value: 'welcome test SimpleText part.' }
})
expect(onchange.mock.calls.length).toBe(1)
})