Skip to content

Instantly share code, notes, and snippets.

@davidcostadev
Created November 9, 2020 23:28
Show Gist options
  • Save davidcostadev/cb5c3d974f52b687adc4352064254e4a to your computer and use it in GitHub Desktop.
Save davidcostadev/cb5c3d974f52b687adc4352064254e4a to your computer and use it in GitHub Desktop.
Mock Apollo Mutation with Jest and typescript
import { useMutation } from "@apollo/client";
jest.mock("@apollo/client", () => {
const pkg = jest.requireActual("@apollo/client");
pkg.useMutation = jest.fn();
return pkg;
});
test("Should call the mutation as mock", async () => {
const mockedUseMutation = useMutation as jest.Mock;
const createTask = jest.fn().mockResolvedValue({
data: {
task: {
data: {
id: '123',
},
},
},
});
mockedUseMutation.mockReturnValue([createTask, { loading: false }]);
// load the component, if needed with MockedProvider and fire the button/action
expect(createTask).toBeCalledWith({
variables: {
title: 'blablabla'
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment