Skip to content

Commit

Permalink
Merge pull request #16 from netweng/xj/fix-mutation
Browse files Browse the repository at this point in the history
fix throw error in mutation
  • Loading branch information
benawad authored Apr 14, 2020
2 parents bae9d06 + 9827b69 commit fad3b3c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/createApolloMockedProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const createApolloMockedProvider = (
const client = new ApolloClient({
link: ApolloLink.from([onError(() => {}), new SchemaLink({ schema })]),
cache: cache || globalCache || new InMemoryCache(),
defaultOptions: {
mutate: { errorPolicy: 'all' },
},
});

const Provider = provider ? provider : ApolloProvider;
Expand Down
41 changes: 39 additions & 2 deletions test/createApolloMockedProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react';
import { createApolloMockedProvider } from '../src';
import { readFileSync } from 'fs';
import { render, wait, waitForDomChange } from '@testing-library/react';
import {
render,
wait,
waitForDomChange,
fireEvent,
} from '@testing-library/react';
import {
GET_TODO_QUERY,
GET_TODOS_QUERY,
Expand Down Expand Up @@ -59,7 +64,7 @@ test('works with custom resolvers', async () => {
expect(getByText('Second Todo')).toBeTruthy();
});

test('allows throwing errors within resolvers to mock API errors', async () => {
test('allows throwing errors within resolvers to mock Query API errors', async () => {
const MockedProvider = createApolloMockedProvider(typeDefs);
const { container } = render(
<MockedProvider
Expand Down Expand Up @@ -100,6 +105,38 @@ test('allows throwing errors within resolvers to mock API errors', async () => {
expect(container.textContent).toMatch(/GraphQL error: Boom/);
});

test('allows throwing errors within resolvers to mock Mutation API errors', async () => {
const MockedProvider = createApolloMockedProvider(typeDefs);
const { container, getByText } = render(
<MockedProvider
customResolvers={{
Query: () => ({
todos: () => [
{
text: 'First Todo',
},
{
text: 'Second Todo',
},
],
}),
Mutation: () => ({
addTodo: () => {
throw new Error('Boom');
},
}),
}}
>
<Todo />
</MockedProvider>
);

await waitForDomChange();
fireEvent.click(getByText('Add todo'));
await waitForDomChange();
expect(container.textContent).toMatch(/GraphQL error: Boom/);
});

describe('caching', () => {
test('allows users to provide a global cache', async () => {
const cache = new InMemoryCache();
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/Todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const Todo = () => (
)}
<button
onClick={() => {
console.log('add');
addTodo({ variables: { input: { text: 'hardcoded' } } });
}}
>
Expand Down

0 comments on commit fad3b3c

Please sign in to comment.