0

你好我是新的测试Hooks,所以我真的很迷茫,试图找到答案但没有结果。

所以问题是,我如何useEffect从这段代码中进行测试?

代码:

const [ range, setRange ] = useState(DATE_OPTIONS.LAST_30_DAYS)
const [ isLoading, setIsLoading ] = useState(true)

const startLoading = () => setIsLoading(true)
const stopLoading = () => setIsLoading(false)

useEffect(() => {
    startLoading()
    fetchYearlyOptiDrive(range, objectId)
        .then(stopLoading)
        .catch(stopLoading)
}, [range, objectId])

我想从代码中测试的是LoadingIndicator,检查道具isLoading,如果是trueor false

<LoadingIndicator {...{ isLoading }} />

测试:

    test('should return the default value', () => {
        const wrapper = mount(<OptiDriveCard {...props} />)
        expect(wrapper.find('LoadingIndicator').props).toBeTruthy()
    })

错误:

不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义

有什么建议吗?

4

1 回答 1

1

要检查传递给组件的值,可以使用酶prop方法。在你的情况下:

expect(wrapper.find('LoadingIndicator').prop('isLoading')).toBe(true);
于 2019-09-26T15:50:58.767 回答