May 2019
Intermediate to advanced
496 pages
10h 38m
English
Let's display an error to the user if the fetch returns an ok value of false. This would occur if the HTTP status code returned was in the 400 or 500 range, although for our tests we won't need to worry about the specific status code. Follow these steps:
it('renders error message when fetch call fails', async () => { fetchSpy.stubReturnValue(Promise.resolve({ ok: false })); render(<CustomerForm />); await act(async () => { ReactTestUtils.Simulate.submit(form('customer')); }); const errorElement = container.querySelector('.error'); expect(errorElement).not.toBeNull(); expect(errorElement.textContent).toMatch('error occurred');});
Read now
Unlock full access