|
| 1 | +/** |
| 2 | + * The console's `toast` surface — the one display type `NotificationProvider` |
| 3 | + * delegates instead of rendering itself (#3014). These pin the mapping onto |
| 4 | + * sonner; which notifications ever get here is covered by |
| 5 | + * `console/__tests__/ConsoleShell.notifications.test.tsx`. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 9 | +import type { NotificationItem } from '@object-ui/react'; |
| 10 | + |
| 11 | +vi.mock('sonner', () => ({ |
| 12 | + toast: Object.assign(vi.fn(), { |
| 13 | + info: vi.fn(), success: vi.fn(), warning: vi.fn(), error: vi.fn(), |
| 14 | + }), |
| 15 | +})); |
| 16 | + |
| 17 | +import { toast } from 'sonner'; |
| 18 | +import { presentNotificationToast } from './notificationToast'; |
| 19 | + |
| 20 | +function item(overrides: Partial<NotificationItem> = {}): NotificationItem { |
| 21 | + return { |
| 22 | + id: 'n1', |
| 23 | + title: 'Saved', |
| 24 | + severity: 'success', |
| 25 | + createdAt: new Date(0), |
| 26 | + ...overrides, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe('presentNotificationToast', () => { |
| 31 | + beforeEach(() => { vi.clearAllMocks(); }); |
| 32 | + |
| 33 | + it('maps each severity to its sonner variant', () => { |
| 34 | + presentNotificationToast(item({ severity: 'info' })); |
| 35 | + presentNotificationToast(item({ severity: 'success' })); |
| 36 | + presentNotificationToast(item({ severity: 'warning' })); |
| 37 | + presentNotificationToast(item({ severity: 'error' })); |
| 38 | + |
| 39 | + expect(toast.info).toHaveBeenCalledTimes(1); |
| 40 | + expect(toast.success).toHaveBeenCalledTimes(1); |
| 41 | + expect(toast.warning).toHaveBeenCalledTimes(1); |
| 42 | + expect(toast.error).toHaveBeenCalledTimes(1); |
| 43 | + }); |
| 44 | + |
| 45 | + it('passes the message through as the toast description', () => { |
| 46 | + presentNotificationToast(item({ message: 'Record updated' })); |
| 47 | + expect(toast.success).toHaveBeenCalledWith('Saved', expect.objectContaining({ |
| 48 | + description: 'Record updated', |
| 49 | + })); |
| 50 | + }); |
| 51 | + |
| 52 | + it('turns duration 0 into a persistent toast', () => { |
| 53 | + // `0 = persistent` is the notification contract; sonner spells it Infinity. |
| 54 | + // Passing the 0 through would make the toast vanish on the next tick. |
| 55 | + presentNotificationToast(item({ duration: 0 })); |
| 56 | + expect(toast.success).toHaveBeenCalledWith('Saved', expect.objectContaining({ |
| 57 | + duration: Infinity, |
| 58 | + })); |
| 59 | + }); |
| 60 | + |
| 61 | + it('leaves an unset duration to the ConsoleToaster default', () => { |
| 62 | + presentNotificationToast(item()); |
| 63 | + expect(toast.success).toHaveBeenCalledWith('Saved', expect.objectContaining({ |
| 64 | + duration: undefined, |
| 65 | + })); |
| 66 | + }); |
| 67 | + |
| 68 | + it('forwards an explicit duration unchanged', () => { |
| 69 | + presentNotificationToast(item({ duration: 12_000 })); |
| 70 | + expect(toast.success).toHaveBeenCalledWith('Saved', expect.objectContaining({ |
| 71 | + duration: 12_000, |
| 72 | + })); |
| 73 | + }); |
| 74 | + |
| 75 | + it('maps the first action to the toast action button', () => { |
| 76 | + const onClick = vi.fn(); |
| 77 | + presentNotificationToast(item({ |
| 78 | + actions: [ |
| 79 | + { label: 'Undo', onClick }, |
| 80 | + { label: 'Ignored — a toast has one action slot', onClick: vi.fn() }, |
| 81 | + ], |
| 82 | + })); |
| 83 | + |
| 84 | + const options = vi.mocked(toast.success).mock.calls[0][1] as { |
| 85 | + action?: { label: string; onClick: () => void }; |
| 86 | + }; |
| 87 | + expect(options.action?.label).toBe('Undo'); |
| 88 | + options.action?.onClick(); |
| 89 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it('omits the action slot entirely when none is declared', () => { |
| 93 | + presentNotificationToast(item()); |
| 94 | + const options = vi.mocked(toast.success).mock.calls[0][1] as Record<string, unknown>; |
| 95 | + expect(options).not.toHaveProperty('action'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('forwards dismissible', () => { |
| 99 | + presentNotificationToast(item({ dismissible: false })); |
| 100 | + expect(toast.success).toHaveBeenCalledWith('Saved', expect.objectContaining({ |
| 101 | + dismissible: false, |
| 102 | + })); |
| 103 | + }); |
| 104 | +}); |
0 commit comments