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 errorPolicy on useMutation hook #5863

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/react/data/MutationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class MutationData<
update,
context: mutationContext = {},
awaitRefetchQueries = false,
fetchPolicy
fetchPolicy,
errorPolicy,
} = this.getOptions();
const mutateOptions = { ...mutationFunctionOptions };

Expand All @@ -107,6 +108,7 @@ export class MutationData<
update,
context: mutationContext,
fetchPolicy,
errorPolicy,
variables: mutateVariables,
...mutateOptions
});
Expand Down
108 changes: 107 additions & 1 deletion src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DocumentNode } from 'graphql';
import { DocumentNode, GraphQLError } from 'graphql';
import gql from 'graphql-tag';
import { render, cleanup, wait } from '@testing-library/react';

Expand Down Expand Up @@ -39,6 +39,8 @@ describe('useMutation Hook', () => {
}
};

const CREATE_TODO_ERROR = 'Failed to create item';

afterEach(cleanup);

describe('General use', () => {
Expand Down Expand Up @@ -253,6 +255,110 @@ describe('useMutation Hook', () => {
return wait();
});

describe('mutate function upon error', () => {
it(`should reject when errorPolicy is 'none'`, async () => {
const variables = {
description: 'Get milk!'
};

const mocks = [
{
request: {
query: CREATE_TODO_MUTATION,
variables
},
result: {
data: CREATE_TODO_RESULT,
errors: [new GraphQLError(CREATE_TODO_ERROR)],
},
}
];

const Component = () => {
const [createTodo] = useMutation<{ createTodo: Todo }>(
CREATE_TODO_MUTATION,
{ errorPolicy: 'none' }
);

async function doIt() {
try {
await createTodo({ variables });
} catch (error) {
expect(error.message).toEqual(
expect.stringContaining(CREATE_TODO_ERROR)
);
}
}

useEffect(() => {
doIt();
}, []);

return null;
};

render(
<MockedProvider mocks={mocks}>
<Component />
</MockedProvider>
);

return wait();
});

it(`should resolve with 'data' and 'error' properties when errorPolicy is 'all'`, async () => {
const variables = {
description: 'Get milk!'
};

const mocks = [
{
request: {
query: CREATE_TODO_MUTATION,
variables
},
result: {
data: CREATE_TODO_RESULT,
errors: [new GraphQLError(CREATE_TODO_ERROR)],
},
}
];

const Component = () => {
const [createTodo] = useMutation<{ createTodo: Todo }>(
CREATE_TODO_MUTATION,
{ errorPolicy: 'all' }
);

async function doIt() {
const { data, errors } = await createTodo({ variables });

expect(data).toEqual(CREATE_TODO_RESULT);
expect(data!.createTodo.description).toEqual(
CREATE_TODO_RESULT.createTodo.description
);
expect(errors[0].message).toEqual(
expect.stringContaining(CREATE_TODO_ERROR)
);
}

useEffect(() => {
doIt();
}, []);

return null;
};

render(
<MockedProvider mocks={mocks}>
<Component />
</MockedProvider>
);

return wait();
})
});

it('should return the current client instance in the result object', async () => {
const Component = () => {
const [, { client }] = useMutation(CREATE_TODO_MUTATION);
Expand Down