On the mouseDown event, a dispatchUser({type:"REMOVE"}) is fired, but i don't understand how to test and receive the dispatch in my test. I know i have to modify the dispatch value in my context (value={[{value}, function to write]}, but i'm stucked :(
On the mouseDown event, a dispatchUser({type:"REMOVE"}) is fired, but i don't understand how to test and receive the dispatch in my test. I know i have to modify the dispatch value in my context (value={[{value}, function to write]}, but i'm stucked :(
Share
Improve this question
edited Jan 22, 2020 at 9:24Guarn
asked Jan 22, 2020 at 8:34
GuarnGuarn8222 silver badges66 bronze badges1
Can you show your app.js file, so we can see what providers are you using, also the context provider file which you dispatch actions? Also the whole ponent and tests, because tests are tricky I need to see everything to help :)
– onuriltan
CommentedJan 22, 2020 at 9:10
Add a ment
|
1 Answer
1
Reset to default
3
You should mock the userDispatch function
import React from 'react';
import {
render,
cleanup,
fireEvent,
} from '@testing-library/react';
// other imports here eg: userContext.Provider
afterEach(() => {
cleanup();
jest.clearAllMocks();
});
const renderConnexion = (mockUserDispatch, mockUser) => {
return render(
<userContext.Provider
value={[
{
userDispatch: mockUserDispatch
// mock other values here by taking them as a parameter
// for example, for user also take it as parameter
user: mockUser
},
() => {}
]}
>
<Connexion />
</userContext.Provider>
);
};
it('calls dispatchUser when mouseD', () => {
// Given
const mockedUserDispatch = jest.fn();
const mockUser = {}
// When
const ponent = renderConnexion(mockedUserDispatch, mockUser);
fireEvent.mouseDown(ponent.getByTestId("deconnexion"));
// Then
expect(mockedUserDispatch).toHaveBeenCalled();
});