-
Notifications
You must be signed in to change notification settings - Fork 276
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
This is my mockFetch:
global.fetch = jest.fn();
const mockFetch = global.fetch as jest.Mock;
Then inside it:
const iphoneHandlerRef = { current: false };
mockFetch.mockImplementation((url, init) => {
// Use the endpoint handlers approach
const endpointHandlers = getEndpointHandlers(iphoneHandlerRef.current);
const handler = endpointHandlers.find((h) =>
h.match(url.toString(), init),
);
return Promise.resolve(
handler ? handler.response() : mockResponses.default,
);
});
const user = userEvent.setup();
const { rerender, unmount } = render(<MockFlipSetup />);
...
jest.restoreAllMocks();
jest.clearAllMocks();
iphoneHandlerRef.current = true;
mockFetch.mockImplementation((url, init) => {
// Use the endpoint handlers approach
const endpointHandlers = getEndpointHandlers(iphoneHandlerRef.current);
const handler = endpointHandlers.find((h) =>
h.match(url.toString(), init),
);
return Promise.resolve(
handler ? handler.response() : mockResponses.default,
);
});
// Advance timers
await act(() => {
unmount();
});
// Rerender the component
rerender(<MockFlipSetup />);
The fetch still returns the initialized response?
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested