diff --git a/src/core/toast.ts b/src/core/toast.ts index 8a71eb0..1c62d85 100644 --- a/src/core/toast.ts +++ b/src/core/toast.ts @@ -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); @@ -64,6 +64,7 @@ toast.promise = ( loading: Renderable; success: ValueOrFunction; error: ValueOrFunction; + finally?: () => void; }, opts?: DefaultToastOptions ) => { @@ -84,7 +85,8 @@ toast.promise = ( ...opts, ...opts?.error, }); - }); + }) + .finally(msgs.finally); return promise; }; diff --git a/test/toast.test.tsx b/test/toast.test.tsx index 37dfec4..2a328ee 100644 --- a/test/toast.test.tsx +++ b/test/toast.test.tsx @@ -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( + <> + + + + ); + + 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( <>