-
Notifications
You must be signed in to change notification settings - Fork 279
Cant update fetch after some operation? #1758
Unanswered
gpbaculio
asked this question in
Problem-solving
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment