vendredi 14 février 2020

Testing React container that multi fetches data using Hooks

My container has 2 state which be updated in useEffect after fetch

const fetch = () => {
    get(url1).then(data1 => setState1(data1));
    get(url2).then(data2 => setState2(data2));
};

useEffect(() => {
    fetch();
}, []);

my test file

test('SnapshotTests', async done => {
    get.mockImplementation(() => {
        return Promise.resolve({
            data: data1
        });
    });
    const tree = await mount(<TestComponent />);
    setTimeout(() => {
        tree.update();
        expect(toJson(tree)).toMatchSnapshot();
        done();
    }, 500);
});

im only able to mock one response data for first get function, how can i mock different response data for the second one?

Aucun commentaire:

Enregistrer un commentaire