-
Notifications
You must be signed in to change notification settings - Fork 7
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
Ability to tell if a mock was called on MockedProvider #84
Comments
I've run into this same situation where a component updates optimistically. The UI will change whether or not the mutation was actually called. |
The `result` property of a `MockedResponse` is currently restricted to being a pre-configured fixed value. Allowing results to be calculated/returned from a function instead of being pre-configured, opens up new test verification paths, that can be useful when using `MockedProvider`. This commit adjusts `MockedResponse`’s `result` such that if a function is set for its value, that function will be fired and its result will be passed to the Observable matching the incoming mocked `request`. Fixes apollographql/apollo-feature-requests#84.
The `result` property of a `MockedResponse` is currently restricted to being a pre-configured fixed value. Allowing results to be calculated/returned from a function instead of being pre-configured, opens up new test verification paths, that can be useful when using `MockedProvider`. This commit adjusts `MockedResponse`’s `result` such that if a function is set for its value, that function will be fired and its result will be passed to the Observable matching the incoming mocked `request`. Fixes apollographql/apollo-feature-requests#84.
Thanks for the suggestion @noaelad - great idea! This change has been implemented and merged, and will be coming in the next |
Nice @hwillson thanks for implementing this! Looking forward to the next release to start using it =] |
Is this documented anywhere folks? https://jkettmann.com/testing-apollo-how-to-test-if-a-mutation-was-called-with-mockedprovider indicates using newData but looking at the PR and doing a local test myself using a jest.fn() on |
When trying to write tests for a react component that contains a mutation, I'd like to verify that the mutation is actually called (and with the right variables). Currently the documentation on this topic suggests mocking the mutation result and testing that the component produces some sort of inspectable change as a result of the mutation.
https://www.apollographql.com/docs/react/recipes/testing.html#Testing-mutation-components
However this poses two problems:
One possible solution would be to allow more flexibility in how mocked responses are specified. If for example it were possible to pass in a method to the mock instead of a static result, then this could be utilized for this purpose, and perhaps other purposes as well:
Another option that provides a bit less flexibility but would still solve this issue is to have some way to allow querying whether or not a specific mocked request was called, and\or how many times it was called.
Thanks for reading! Looking forward to your response.
The text was updated successfully, but these errors were encountered: