Skip to content

Commit

Permalink
Add failing test for useMutation with error thrown in onCompleted
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 5, 2024
1 parent 9b8bca6 commit 3849806
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,61 @@ describe("useMutation Hook", () => {
expect.objectContaining({ variables })
);
});

// https://github.com/apollographql/apollo-client/issues/12008
it("does not call onError if errors are thrown in the onCompleted callback", async () => {
const CREATE_TODO_DATA = {
createTodo: {
id: 1,
priority: "Low",
description: "Get milk!",
__typename: "Todo",
},
};

const variables = {
priority: "Low",
description: "Get milk2.",
};

const mocks = [
{
request: {
query: CREATE_TODO_MUTATION,
variables,
},
result: {
data: CREATE_TODO_DATA,
},
},
];

const onError = jest.fn();

using _disabledAct = disableActEnvironment();
const { takeSnapshot } = await renderHookToSnapshotStream(
() =>
useMutation(CREATE_TODO_MUTATION, {
onCompleted: () => {
throw new Error("Oops");
},
onError,
}),
{
wrapper: ({ children }) => (
<MockedProvider mocks={mocks}>{children}</MockedProvider>
),
}
);

const [createTodo] = await takeSnapshot();

await expect(createTodo({ variables })).rejects.toEqual(
new Error("Oops")
);

expect(onError).not.toHaveBeenCalled();
});
});

describe("ROOT_MUTATION cache data", () => {
Expand Down

0 comments on commit 3849806

Please sign in to comment.