测试一个功能,不要开玩笑

时间:2019-09-10 11:03:31

标签: reactjs jestjs enzyme

我想测试一个没有覆盖率的函数,我真的不知道如何正确地做。 我想测试onUnitChange函数。

const SettingsItemDistanceUnit = ({ intl, updateUnit }) => {
    const currentUnit = supportedUnits.filter(
        (key) => DistUnit[key] === intl.getDistUnitObject())[0] || supportedUnits[0]
    const onUnitChange = (value) => currentUnit !== value.target.value && updateUnit([
        {
            name: settingsUnit,
            values: [value.target.value]
        }
    ])
 
    return (
        ...
    )
}

测试

test('test', () => {
    const wrapper = shallow(<SettingsItemDistanceUnit {...mockPropsForComponent} />)
    const value = { target: { value: 'mi' } }
    const instance = wrapper.instance()
    expect(instance.onUnitChange(value)).toBe()
})

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我认为您应该检查是否已执行从父级传递给组件的函数

test('test', () => {
    const wrapper = shallow(<SettingsItemDistanceUnit {...mockPropsForComponent} />)
    const value = { target: { value: 'mi' } }
    const instance = wrapper.instance()
    instance.onUnitChange(value)
    expect(mockPropsForComponent.updateUnit).toBeCalledWith([
        {
            name: settingsUnit,
            values: [value.target.value]
        }
    ])
})