jest监视graphql(变异)查询

izj3ouym  于 2022-12-08  发布在  Jest
关注(0)|答案(1)|浏览(211)

I have 2 mutate gql query that I need to mock them in my unit test. For unit tests I am using react test library. during my function test 2 different mutate is being called and I have to find a way to differentiate between them, so inside my test it will know which one is being called in the correct order.
here are 2 mutate gql:

export const closeMultipartUpload: any = gql`
    mutation closeMultipartUpload(
        $directoryID: String!
        $uploadID: String!
    ) {
        closeMultipartUpload(
            directoryID: $directoryID
            uploadID: $uploadID
        ) {
            versionID
        }
    }
`;

export const fileUploadMutation = gql`
    mutation createUploadUrl(
        $directoryID: String!
    ) {
        createUploadUrl(
            directoryID: $directoryID
            requesterID: $requesterID
        ) {
            url
            documentID
        }
    }
`;

to mock this APIs I have below in my test:

graphql.mutate = jest.fn().mockImplementation(() =>
  Promise.resolve({
    data: {
      'createUploadUrl': {
        'url': 'foo',
        'documentID': '123
      }
    },
  })
);

but I cannot mock the same way for the other API call, since react test library can recognize which one is which. i thought spy on can help. but it did not or at least I am not using it correctly.
I will appreciate your help

mnemlml8

mnemlml81#

Probably a good idea to have an Apollo Provider, and provide the state of the request and response as demo'd here in the docs https://www.apollographql.com/docs/react/development-testing/testing/

const mocks = [
  {
    request: {
      query: GET_DOG_QUERY,
      variables: {
        name: "Buck"
      }
    },
    result: {
      data: {
        dog: { id: "1", name: "Buck", breed: "bulldog" }
      }
    }
  }
];

相关问题