mercredi 22 janvier 2020

React - Testing useContext() dispatch value

I'm in trouble with testing a dispatch value on a useContext hook.

I have a simple component:

Connexion.tsx :

    const Connexion = () => {
       const [user, userDispatch] = React.useContext(userContext);
    //...other stuff
    }

And i'm looking to check the value of a dispatch in a test, so my test file is :

    Connexion.test.jsx :

...
    const renderConnexion = () => {
        return render(
            <userContext.Provider
                value={[
                    {
                        connecte: true,
                        // ...other stuff
                    },
                    () => {}
                ]}
            >
                <Connexion />
            </userContext.Provider>
        );
    };

...

    test("Déconnexion", async () => {
            const component = renderConnexion();
            fireEvent.mouseDown(component.getByTestId("deconnexion"));
        });

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 :(

Can someone help me ?

Aucun commentaire:

Enregistrer un commentaire