Reputation: 89
I'm trying to mock out a graphql mutation for my unit tests. I'm using the MockedProvider but I'm getting the error: 'UnhandledPromiseRejectionWarning: Error: No more mocked responses for the query: mutation LoginMutation'
I've done a lot of googling, and copy pasted everything into one file so that there can be no issues with imports
const LOGIN_MUTATION = gql`
mutation LoginMutation($email: String!, $password: String!) {
login(email: $email, password: $password) {
token
user {
name
}
}
}
`;
const mocks = [
{
request: {
query: LOGIN_MUTATION,
variables: { email: '[email protected]', password: 'password' },
},
result: {
data: {
login: {
token: 'fakeToken',
user: {
name: 'Testy McTestface'
}
}
}
}
}
]
describe('LoginForm', () => {
let wrapper;
let store;
beforeEach(() => {
store = mockStore(initialState);
});
it('triggers the doLogin action creator after the graphql call', () => {
const mockDoLogin = jest.fn()
let mockedWrapper = mount(
<MockedProvider mocks={mocks} addTypename={false}>
<Provider store={store}>
<LoginForm doLogin={mockDoLogin} />
</Provider>
</MockedProvider>,
);
mockedWrapper.find('#loginButton').simulate('click')
expect(mockDoLogin).toHaveBeenCalledTimes(1)
})
});
I expect this test to pass (the tag searched for on the wrapper is the correct one) but I get the error described above, so I'm assuming the issue is with my mock but it looks good to me?
Sorry if this is something ridiculously obvious but I've been staring at it for hours now.
Upvotes: 0
Views: 2886
Reputation: 504
Would you like to give a shot to easygraphql-tester? It'll mock the query for you, but also, if you want to set some fixture you can set them too!
You might pass the schema in order to use it!
import EasyGraphQLTester from 'easygraphql-tester'
const tester = new EasyGraphQLTester(schema)
const LOGIN_MUTATION = gql`
mutation LoginMutation($email: String!, $password: String!) {
login(email: $email, password: $password) {
token
user {
name
}
}
}
`;
const mocks = [
{
request: {
query: LOGIN_MUTATION,
variables: { email: '[email protected]', password: 'password' },
},
result: tester.mock({
query: LOGIN_MUTATION,
variables: { email: '[email protected]', password: 'password' }
})
}
]
Upvotes: 1