diff --git a/src/components/Toast.tsx b/src/components/Toast.tsx index b81289a7..b40913f9 100644 --- a/src/components/Toast.tsx +++ b/src/components/Toast.tsx @@ -87,6 +87,7 @@ export const Toast: React.FC = props => { {...eventHandlers} style={style} ref={toastRef} + data-testid={type} >
( }) ); +interface ToastPromiseUpdateOptions + extends Omit, 'type'> { + type?: TypeOptions | ((props: { data: T }) => TypeOptions); +} + export interface ToastPromiseParams< TData = unknown, TError = unknown, TPending = unknown > { - pending?: string | UpdateOptions; - success?: string | UpdateOptions; - error?: string | UpdateOptions; + pending?: string | ToastPromiseUpdateOptions; + success?: string | ToastPromiseUpdateOptions; + error?: string | ToastPromiseUpdateOptions; } function handlePromise( @@ -141,7 +146,7 @@ function handlePromise( const resolver = ( type: TypeOptions, - input: string | UpdateOptions | undefined, + input: string | ToastPromiseUpdateOptions | undefined, result: T ) => { // Remove the toast if the input has not been provided. This prevents the toast from hanging @@ -157,7 +162,9 @@ function handlePromise( ...options, data: result }; - const params = isStr(input) ? { render: input } : input; + const params = isStr(input) + ? { render: input } + : { ...input, type: isFn(input.type) ? input.type({ data: result }) : input.type }; // if the id is set we know that it's an update if (id) { diff --git a/test/core/toast.test.tsx b/test/core/toast.test.tsx index 6f0ab0aa..34f2d9f2 100644 --- a/test/core/toast.test.tsx +++ b/test/core/toast.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { render, act, screen } from '@testing-library/react'; +import { render, act, screen, waitFor } from '@testing-library/react'; import { triggerAnimationEnd } from '../helpers'; import { eventManager, toast, Event } from '../../src/core'; @@ -478,4 +478,28 @@ describe('toastify', () => { expect(screen.queryByText('hello')).toBe(null); }); + + it('should update the toast type when the promise resolves', async () => { + render(); + + const promise = jest.fn(() => Promise.resolve({ + ok: false + })); + + act(() => { + toast.promise(promise, { + success: { + render: 'success', + type: ({ data }) => !data.ok ? 'error' : 'success' + }, + error: { + render: 'error', + } + }); + }); + + await waitFor(() => { + expect(screen.getByTestId('error')).not.toBe(null); + }); + }); });