Skip to content

Commit

Permalink
Support multiple success callbacks in useMutationsHandler
Browse files Browse the repository at this point in the history
useMutationsHandler test cases

useMutationsHandler test and type definitions

Fix typo in useMutationsHandler test
  • Loading branch information
hervedombya committed Jan 17, 2024
1 parent 5c46473 commit cfa5ca6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
88 changes: 87 additions & 1 deletion src/lib/components/toast/useMutationsHandler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('useMutationsHandler', () => {

const messageDescriptionBuilder = jest.fn(() => 'message');

it('should call onPrimarySuccess when a primary mutation succeeds', async () => {
it('should call onMainMutationSuccess when a primary mutation succeeds', async () => {
const showToast = jest.fn();
const onMainMutationSuccess = jest.fn();

Expand Down Expand Up @@ -149,4 +149,90 @@ describe('useMutationsHandler', () => {
});
});
});

it('should call onAllSuccess when all mutations succeeds', async () => {
const showToast = jest.fn();
const otherMutations = [
{
mutation: {
isLoading: false,
isSuccess: true,
isError: false,
isIdle: false,
status: 'success',
...mutationResultMock,
},
name: 'mutation2',
},
] as const;
const onAllMutationsSuccess = jest.fn();

mockUseToast.mockImplementation(() => ({
showToast,
}));

const { waitFor } = renderHook(() =>
useMutationsHandler({
mainMutation,
dependantMutations: otherMutations,
messageDescriptionBuilder,
onAllMutationsSuccess,
}),
);

await act(async () => {
await waitFor(() => {
expect(onAllMutationsSuccess).toHaveBeenCalled();
});
});
});

it('should not call onAllSuccess when all mutations failed', async () => {
const showToast = jest.fn();
const firstMutation = {
mutation: {
isError: true,
isIdle: false,
isLoading: false,
isPaused: false,
isSuccess: false,
status: 'error',
...mutationResultMock,
},
name: 'mutation1',
};
const otherMutations = [
{
mutation: {
isLoading: false,
isSuccess: false,
isError: true,
isIdle: false,
status: 'error',
...mutationResultMock,
},
name: 'mutation2',
},
] as const;
const onAllMutationsSuccess = jest.fn();

mockUseToast.mockImplementation(() => ({
showToast,
}));

const { waitFor } = renderHook(() =>
useMutationsHandler({
mainMutation: firstMutation,
dependantMutations: otherMutations,
messageDescriptionBuilder,
onAllMutationsSuccess,
}),
);

await act(async () => {
await waitFor(() => {
expect(onAllMutationsSuccess).not.toHaveBeenCalled();
});
});
});
});
17 changes: 12 additions & 5 deletions src/lib/components/toast/useMutationsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ type Props<MainMutationType, T extends any[]> = {
? [GetDescriptionBuilder<MainMutationType>]
: [GetDescriptionBuilder<MainMutationType>, ...DescriptionBuilders<T>],
) => ReactNode;
onMainMutationSuccess?: () => void;
toastProps?: Pick<
ToastContextState,
'style' | 'autoDismiss' | 'position' | 'duration' | 'withProgressBar'
>;
};
} & (
| { onMainMutationSuccess?: () => void; onAllMutationsSuccess?: never }
| { onAllMutationsSuccess?: () => void; onMainMutationSuccess?: never }
);

type MinimalMutationResult<TData, TError> = Pick<
UseMutationResult<TData, TError, unknown, unknown>,
Expand All @@ -96,8 +98,8 @@ export const useMutationsHandler = <
mainMutation,
dependantMutations,
messageDescriptionBuilder,
onMainMutationSuccess,
toastProps,
...rest
}: Props<MainMutationType, T>) => {
const { showToast } = useToast();
const mutations = [
Expand Down Expand Up @@ -139,7 +141,9 @@ export const useMutationsHandler = <

if (loadingMutations.length === 0) {
if (errorMutations.length > 0) {
onMainMutationSuccess?.();
if (mainMutation.mutation?.isSuccess) {
'onMainMutationSuccess' in rest && rest?.onMainMutationSuccess?.();
}
showToast({
open: true,
status: 'error',
Expand All @@ -148,7 +152,10 @@ export const useMutationsHandler = <
});
return;
} else if (successMutations.length > 0) {
onMainMutationSuccess?.();
'onMainMutationSuccess' in rest && rest?.onMainMutationSuccess?.();
if (successMutations.length === mutations.length) {
'onAllMutationsSuccess' in rest && rest?.onAllMutationsSuccess?.();
}
showToast({
open: true,
status: 'success',
Expand Down

0 comments on commit cfa5ca6

Please sign in to comment.