Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apollo Link Rest - MockProvider #303

Open
JayYoungman opened this issue Nov 18, 2022 · 1 comment
Open

Apollo Link Rest - MockProvider #303

JayYoungman opened this issue Nov 18, 2022 · 1 comment

Comments

@JayYoungman
Copy link

JayYoungman commented Nov 18, 2022

Hey

Im trying to Mock an API request following the Apollo docs for MockProvider, for some reason jest dom is returning a "?", nothing inside my mockQuery is firing it seems, even the error is not being displayed. Any console logs also don't get rendered.

Does MockProvider even work with rest link? Any help would be much appreciated. Some example code below if it helps

export const currentUserFragment: DocumentNode = gql`
  fragment UserFields on User {
    avatar: _avatar
    name
  }
`;
const queryMocks = [
  {
    error: new Error("something went wrong"),
    request: {
      query: query(currentUserFragment),
      variables: {
        id: "1",
      },
    },
    result: {
      data: {
        user: {
          avatar: {
            url: "image.jpg",
          },
          id: "1",
          name: "Jay Youngman",
        },
      },
    },
  },
];

const renderWithContext = (
  overrideContextValue: Partial<GlobalContextValue> = {},
  overrideProps: Partial<HeaderProps> = {}
) =>
  render(
    <GlobalContext.Provider
      value={{ ...globalContextMockValue, ...overrideContextValue }}
    >
      <MockedProvider addTypename={false} mocks={queryMocks}>
        <Header {...defaultProps} {...overrideProps} />
      </MockedProvider>
    </GlobalContext.Provider>
  );


it("displays the users name", async () => {
    renderWithContext();

    await wait(0);

    const userName = screen.getByText("Jay Youngman");
    screen.debug();
    expect(userName).toBeInTheDocument();
  });

Query

  gql`
    query UseCurrentUserQuery($id: ID) {
      user(id: $id) @rest(path: "/users/{args.id}", type: "User") {
        ...UserFields
        id
      }
    }
    ${fragment}
  `;

Thanks

/label Question

@fbartho
Copy link
Collaborator

fbartho commented Nov 18, 2022

ApolloLinkRest is not necessarily compatible with MockProvider -- I haven't spent any time with that.

If you're trying to add mocking to mock your REST server out, instead you can use one of the fetch-based mocking libraries.

Our unit tests demonstrate how to use fetchMock:

fetchMock.restore();
(as an example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants