reactjs - React,快照测试 useParams undefined
问题描述
我正在尝试为我的组件编写一个快照测试在我的组件中我有useParams()
运行测试时它返回一个未定义的错误:
TypeError: Cannot read property 'match' of undefined
20 | const [value, setValue] = useState();
21 | const { show, changeDetails } = props;
> 22 | const { id } = useParams();
| ^
组件位于
<Switch>
<Route
exact
path="/myroute/:id"
component={() => (
<>
<ModuleDetails />
</>
)}
/>
</Switch>
和 test.js 就像
it('renders correctly the assessment show page', () => {
const history = createMemoryHistory();
history.push('/myroute/1');
let tree = renderer
.create(
<Router history={history}>
<ModuleDetails />
</Router>
)
.toJSON();
expect(tree).toMatchInlineSnapsot(``);
});
解决方案
const container = document.createElement('div');
render(
<MemoryRouter initialEntries={['/myroute/1']}>
<Route
exact
path="/myroute/:id"
component={() => (
<>
<ModuleDetails />
</>
)}
/>
</MemoryRouter>,
container
);
推荐阅读
- php - 使用 Laravel 和 Passport 的 401 未经身份验证的问题
- python - 如何解决:项目中未使用 Google Sheets API
- jquery - 'this' 功能需要 JQuery 帮助。列表项中的按钮
- python - Python Pandas 对列填充/过滤的条件更改正确
- java - 按分隔符拆分 PDF?
- reactjs - Next js中如何正确导入图片和视频
- windows - 有没有办法在小任务栏图标中显示搜索框
- javascript - 如何检查节点列表中的所有元素是否具有相同的类或相同的样式属性值?
- spring - java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z
- react-native - 如何将此 RN 代码转换为带有钩子的功能组件?