Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cant update fetch after some operation? #1758

Unanswered
gpbaculio asked this question in Problem-solving
Discussion options

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?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant
Converted from issue

This discussion was converted from issue #1757 on March 30, 2025 19:49.

AltStyle によって変換されたページ (->オリジナル) /