Skip to content
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

fix throw error in mutation #16

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
benawad marked this conversation as resolved.
Show resolved Hide resolved
addTodo({ variables: { input: { text: 'hardcoded' } } });
}}
>
Expand Down