【发布时间】:2019-09-26 14:01:36
【问题描述】:
你好我是新的测试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,如果是true或false。
<LoadingIndicator {...{ isLoading }} />
测试:
test('should return the default value', () => {
const wrapper = mount(<OptiDriveCard {...props} />)
expect(wrapper.find('LoadingIndicator').props).toBeTruthy()
})
错误:
Invariant Violation:元素类型无效:期望字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义
有什么建议吗?
【问题讨论】:
-
根据您显示的错误,似乎某些组件不正确(错误表明某些组件返回
undefined而不是字符串、类或函数)。如果您检查此sandbox,您将看到代码按预期运行。 -
@mgarcia 这是导入错误,抱歉 :(
标签: reactjs jestjs enzyme react-lifecycle-hooks