javascript - 下一个 router.useRouter 甚至不是一个模拟 RouterContext 的函数
问题描述
我正在尝试测试一个使用useRouter
from next/router
hook 来获取的组件route
,但即使是模拟 RouterContext,我也会收到以下错误:
Error: Uncaught [TypeError: (0 , _router.useRouter) is not a function]
我的应用程序使用redux,它也被react-redux
Provider
and模拟mockStore
。
这是我渲染组件的实用程序代码:
const getCustomWrapper =
(router, initialState) => {
const Wrapper = ({ children }) => {
const store = mockStore({
...initialStateModel,
...initialState
})
return (
<Provider store={store}>
<RouterContext.Provider value={{ ...mockRouter, ...router }}>
{children}
</RouterContext.Provider>
</Provider>
)
}
Wrapper.propTypes = {
children: PropTypes.node
}
return Wrapper
}
const customRender = async (ui, { options, router, initialState } = {}) => {
const Wrapper = getCustomWrapper(router, initialState)
return render(<Wrapper>{ui}</Wrapper>, options)
}
我的测试是这样的:
const createComponent = (props = defaultProp, initialState = defaultInitialState) => {
customRender(<MyComponet {...props} />, { initialState })
}
it('should render my text', async () => {
createComponent()
const myText = screen.getByText('my text')
expect(myText).toBeInTheDocument()
})
版本
next: 10.1.2,
react-redux: 7.2.5,
react: 16.13.1
更新它现在不是一个选项=/
提前谢谢
解决方案
推荐阅读
- python - 如何使用 python 在循环中有效地调用函数?
- java - 休眠错误:查询方法公共抽象 java.util.List 的验证失败
- c - C malloc 结构抛出内存冲突异常(准系统示例)
- python - 使用 Openpyxl 突出显示单元格
- c# - 当不是每个查询都适用于我的 Click 事件时
- laravel - Laravel 的 belongsTo 方法返回 null
- flask - 如何配置 .hosts 文件以便其他设备可以访问它
- reactjs - 如果我使用自定义组件而不是普通的 HTML5 输入标签,使用 react-hook-form 的 ref 会在控制台中引发错误
- c++ - 为什么用c而不是c ++编写的postgres?c 有更好的性能还是只是历史原因?
- javascript - NgClass Error Angular(不能绑定到'ngClass',因为它不是'a'的已知属性)