dimanche 25 novembre 2018

Testing fetch request with Jest and Enzyme

I started learning to test React applications, now I want to test fetch request, I succeeded, seems like it works, but what's the proper way to do it, here is my try:

const fetchSpy = jest.spyOn(global, 'fetch')
        .mockImplementation(() => Promise.resolve({
            json: () => ([{ id: 0, title: "Title", body: "Body", userId: 1 }]),
        }));

    let wrapper = await shallow(<Posts />);

    expect(fetchSpy).toHaveBeenCalled();

    setTimeout(async () => {
        await wrapper.update()
        expect(wrapper.state('posts').length).toEqual(1)
    })

And also, what would be better way to wait for component to update instead setTimeout ?

Aucun commentaire:

Enregistrer un commentaire