如何在状态更改后测试组件是否重新提供反应钩子测试库

时间:2020-06-25 00:18:35

标签: javascript reactjs unit-testing testing react-testing-library

我正在使用https://github.com/testing-library/react-hooks-testing-library测试我的组件,但不确定在此测试中状态更改后如何检查我的组件是否已重新渲染

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

状态在分派/单击事件后成功更新。我希望能够以新状态重新渲染组件,但它仍显示具有先前状态的原始组件

0 个答案:

没有答案