Skip to content

Commit

Permalink
Disable mutation retries in test
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalita-lbl committed Feb 24, 2025
1 parent 96a9d5b commit b5b25b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/queries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ test("useSubmission should rollback optimistic updates if a mutation fails", asy

// Second fetch data from the individual submission query. Wait for the background fetch to
// complete before proceeding.
const { result } = renderHook(() => useSubmission(TEST_ID_1), { wrapper });
const { result } = renderHook(
() => useSubmission(TEST_ID_1, { retryUpdates: false }),
{ wrapper },
);
await waitFor(() => expect(result.current.query.isFetching).toBe(false));

// Update the submission data. There doesn't seem to be a good way to ensure that the onMutate()
Expand Down
14 changes: 13 additions & 1 deletion src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,15 @@ export function useSubmissionList() {
});
}

export function useSubmission(id: string) {
export interface UseSubmissionOptions {
retryUpdates?: boolean;
}
export function useSubmission(id: string, options: UseSubmissionOptions = {}) {
const queryClient = useQueryClient();
options = {
retryUpdates: true,
...options,
};

const updateSubmissionInQueryData = (data: SubmissionMetadata) => {
// Update the individual submission entry
Expand Down Expand Up @@ -202,6 +209,10 @@ export function useSubmission(id: string) {
},
});

const mutationDefaults = queryClient.getMutationDefaults(
submissionKeys.details(),
);

type SubmissionMetadataMutationContext = {
previousData?: SubmissionMetadata;
};
Expand All @@ -212,6 +223,7 @@ export function useSubmission(id: string) {
SubmissionMetadataMutationContext
>({
mutationKey: submissionKeys.detail(id),
retry: options.retryUpdates ? mutationDefaults?.retry : 0,
onMutate: async (updatedSubmission: SubmissionMetadata) => {
// If there are previous mutations for this submission, remove them. The latest mutation should
// contain the most up-to-date data.
Expand Down

0 comments on commit b5b25b8

Please sign in to comment.