lundi 18 janvier 2021

React testing components problem with fetch

We are using redux store in our project and fetch methods for getting data from backend. For testing we decided to create an additional method that creates fake server configuration to be able to correctly render components which uses that configuration. Here is this method:

export async function callGetMockConfiguration() {
    try {
        var data = {
            method: "POST",
            credentials: "same-origin",
            headers: { "Content-Type": "application/json" }
        };
        const response = await fetch(getBaseUrl() + "auth/getmockconfiguration", data).then((x) => x.json());
        return response;
    } catch (e) {
        throw e;
    }
}

My test code fragment looks like this:

 it("test", async () => {
        await callGetMockConfiguration().then((response) => store.dispatch(configurationActionCreators.setConfiguration(response)));

        const wrappe = mount(
            <Provider store={store}>
                <RecoilRoot>
                    <Component />
                </RecoilRoot>
            </Provider>
        );
        expect(wrapper).toMatchSnapshot();
    });

So I want to create that fake configuration and set store by that. The problem is that I get this error:

enter image description here

and also this one at the same time:

enter image description here

Aucun commentaire:

Enregistrer un commentaire