Skip to content

Cant update fetch after some operation? #1757

@gpbaculio

Description

@gpbaculio

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

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions