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

feat: add optional "finally" callback after promise completion #343

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const createToast = (

const createHandler =
(type?: ToastType): ToastHandler =>
(message, options) => {
const toast = createToast(message, type, options);
dispatch({ type: ActionType.UPSERT_TOAST, toast });
return toast.id;
};
(message, options) => {
const toast = createToast(message, type, options);
dispatch({ type: ActionType.UPSERT_TOAST, toast });
return toast.id;
};

const toast = (message: Message, opts?: ToastOptions) =>
createHandler('blank')(message, opts);
Expand All @@ -64,6 +64,7 @@ toast.promise = <T>(
loading: Renderable;
success: ValueOrFunction<Renderable, T>;
error: ValueOrFunction<Renderable, any>;
finally?: () => void;
},
opts?: DefaultToastOptions
) => {
Expand All @@ -84,7 +85,8 @@ toast.promise = <T>(
...opts,
...opts?.error,
});
});
})
.finally(msgs.finally);

return promise;
};
Expand Down
61 changes: 61 additions & 0 deletions test/toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,67 @@ test('promise toast error', async () => {
});
});

test('promise toast finally', async () => {
const WAIT_DELAY = 1000;
const finallyCallback = jest.fn();
const originalPromise = jest.spyOn(toast, 'promise');


render(
<>
<button
type="button"
onClick={() => {
const sleep = new Promise((resolve) => {
setTimeout(resolve, WAIT_DELAY);
});

toast.promise(sleep, {
loading: 'Loading...',
success: 'Success!',
error: 'Error!',
finally: finallyCallback
});
}}
>
Notify!
</button>
<Toaster />
</>
);

act(() => {
fireEvent.click(screen.getByRole('button', { name: /Notify/i }));
});

await screen.findByText(/loading/i);

expect(screen.queryByText(/loading/i)).toBeInTheDocument();

waitTime(WAIT_DELAY);

await waitFor(() => {
expect(screen.queryByText(/success/i)).toBeInTheDocument();
});

// Assert that the original `toast.promise` was called with the expected arguments
expect(originalPromise).toHaveBeenCalledWith(expect.any(Promise), {
loading: 'Loading...',
success: 'Success!',
error: 'Error!',
finally: finallyCallback,
});

// Access the arguments passed to the original `toast.promise` call
const [promiseArg, optionsArg] = originalPromise.mock.calls[0];

// Resolve the promise to trigger the finally callback
await promiseArg;

// Assert that finallyCallback was called
expect(finallyCallback).toHaveBeenCalled();
});

test('error toast with custom duration', async () => {
render(
<>
Expand Down