I'm trying to write a test with jest and react testing library to see if the store updates a state and the new state is shown inside the component.
I have a component like:
import { getName } from 'src/store/actions/hello/getName';
const HelloComponent = () => {
const dispatch = useDispatch();
const { name, isLoading } = useSelector((state:RootState) => state.Hello)
useEffect( () => {
dispatch(getName());
}, []);
return (
<div>
{ name &&
Hello {name}
}
</div>
)
}
There is the store which calls an API like:
const getName = () => (dispatch) => {
const URL = getUrl()
fetch(URL, {method: 'GET'})
.then((response) => response.json())
.then(({ data }) => dispatch({
type: 'SAVE_NAME',
payload: data.name
})) # This action updates the name inside the redux state
};
I'm using mswjs to mock the API call and I'd like to test that after the component mount, the DOM shows 'Hello John'.
This is the test I've written, but it doesn't work:
it('shows the name', async () => {
const {findByText} = renderWithStore(<HelloComponent />);
expect(await findByText('Hello John')).toBeInTheDocument();
});
renderWithStore mocks the store.
import configureStore from 'redux-mock-store';
import { render as rtlRender } from '@testing-library/react'
import { initialState as Hello } from '../src/store/reducers/helloReducer';
const mockStore = configureStore()
const initialStateMock = {
Hello
}
function render(
ui
) {
const store = mockStore(initialStateMock);
function Wrapper({ children }) {
return <Provider store={store}>{children}</Provider>
}
return rtlRender(ui, { wrapper: Wrapper })
}
It seems like it doesn't wait for the state to update.
any help is much appreciated
Thanks