[英]how to test if component rerenders after state change for react hooks testing library
I'm using https://github.com/testing-library/react-hooks-testing-library to test my component but not sure how to check if my component has been re-rendered after a state change in this test我正在使用https://github.com/testing-library/react-hooks-testing-library来测试我的组件,但不确定如何检查我的组件是否在此测试中发生 state 更改后重新呈现
const { container, getAllByText, getAllByRole, queryByLabelText, getByText, getByLabelText } = renderNavBar(
initialState,
dispatchMock,
);
// get all divs
let divs = getAllByRole('div');
.
.
.
const { result, waitForNextUpdate } = renderHook(() => useReducer(Reducer, initialState));
const [ , dispatch ] = result.current;
act(() => {
dispatch({ type: 'SET_ACTIVE_LINK', payload: 'about' });
fireEvent.click(getByText('home'));
});
// get all divs after the state change,
divs = getAllByRole('div'); // <---- this still has the NavBar component with the old state
The state successfully updates after the dispatch/click event. state 在调度/点击事件后成功更新。 I want to be able to get the component re-rendered with new state but it's still showing the original component with the previous state
我希望能够使用新的 state 重新渲染组件,但它仍然显示具有先前 state 的原始组件
await const divs = findAllByRole('div');
findAllBy is async and will wait to find it. findAllBy 是异步的,将等待找到它。 findAll will throw an error if it doesn't find the value.
如果找不到值,findAll 将抛出错误。 https://testing-library.com/docs/react-testing-library/cheatsheet/
https://testing-library.com/docs/react-testing-library/cheatsheet/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.