mercredi 7 août 2019

Testing Graphql Queries in Apollo using Jest

I have no trouble with the more basic sort of test via Apollo's mockedprovider. My problem is with a query that has hardcoded variables that are graphql enum values. The query:

const gqlQuery = gql`
  query {
    books(types: [HARD_COVER, SOFT_COVER]) {
      id
      title
      author
  }
}

So no variables are passed in. When I try to write a test for this (leaving out the assertion... I get an error regardless) I try:

<MockedProvider
  mocks={[
    {
     request: {
      query: GET_BOOKS
  },
    result: {
      data: {
        books: { id: '1', title: 'Test', author: 'Test' },
      },
    },
  }
  ]}
  addTypename={false}
  >
  <MyComponent />
</MockedProvider>

Which will fail with: Network error: No more mocked responses for the query

I'm assuming it has something to do with those enums HARD_COVER, SOFT_COVER. Is that it? If so, how do I pass them with the mockedprovider? Am I missing something? Other mock queries where I do the above but pass a variable work fine... not the above.

Aucun commentaire:

Enregistrer un commentaire