Created
November 9, 2020 23:28
-
-
Save davidcostadev/cb5c3d974f52b687adc4352064254e4a to your computer and use it in GitHub Desktop.
Mock Apollo Mutation with Jest and typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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