|
| 1 | +import { render, screen, fireEvent, within } from '@testing-library/react'; |
| 2 | +import { NotificationCenter } from '@/components/layout/NotificationCenter'; |
| 3 | +import { NotificationProvider } from '@/contexts/NotificationContext'; |
| 4 | +import { useNotificationContext } from '@/contexts/NotificationContext'; |
| 5 | +import { NOTIFICATIONS_STORAGE_KEY } from '@/hooks/useNotifications'; |
| 6 | + |
| 7 | +// Test harness — lets us add notifications from outside the component tree. |
| 8 | +function Harness({ children }: { children: React.ReactNode }) { |
| 9 | + return <NotificationProvider>{children}</NotificationProvider>; |
| 10 | +} |
| 11 | + |
| 12 | +function Adder({ buttonId, payload }: { buttonId: string; payload: { type: 'batch.completed' | 'blocker.created' | 'gate.run.failed'; message: string } }) { |
| 13 | + const { addNotification } = useNotificationContext(); |
| 14 | + return ( |
| 15 | + <button data-testid={buttonId} onClick={() => addNotification(payload)}> |
| 16 | + add |
| 17 | + </button> |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + localStorage.clear(); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('NotificationCenter', () => { |
| 26 | + it('renders a bell button with no badge when there are no unread notifications', () => { |
| 27 | + render( |
| 28 | + <Harness> |
| 29 | + <NotificationCenter /> |
| 30 | + </Harness> |
| 31 | + ); |
| 32 | + const bell = screen.getByRole('button', { name: /notifications/i }); |
| 33 | + expect(bell).toBeInTheDocument(); |
| 34 | + expect(screen.queryByTestId('notification-badge')).not.toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('shows an unread badge when there are unread notifications', () => { |
| 38 | + render( |
| 39 | + <Harness> |
| 40 | + <Adder buttonId="add-1" payload={{ type: 'batch.completed', message: 'Batch 1 done' }} /> |
| 41 | + <NotificationCenter /> |
| 42 | + </Harness> |
| 43 | + ); |
| 44 | + fireEvent.click(screen.getByTestId('add-1')); |
| 45 | + expect(screen.getByTestId('notification-badge')).toHaveTextContent('1'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('opens the dropdown and lists notifications when the bell is clicked', () => { |
| 49 | + render( |
| 50 | + <Harness> |
| 51 | + <Adder buttonId="add-1" payload={{ type: 'batch.completed', message: 'Batch 1 done' }} /> |
| 52 | + <Adder buttonId="add-2" payload={{ type: 'blocker.created', message: 'Blocked: task 7' }} /> |
| 53 | + <NotificationCenter /> |
| 54 | + </Harness> |
| 55 | + ); |
| 56 | + |
| 57 | + fireEvent.click(screen.getByTestId('add-1')); |
| 58 | + fireEvent.click(screen.getByTestId('add-2')); |
| 59 | + |
| 60 | + expect(screen.queryByText(/Batch 1 done/)).not.toBeInTheDocument(); |
| 61 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 62 | + |
| 63 | + expect(screen.getByText(/Batch 1 done/)).toBeInTheDocument(); |
| 64 | + expect(screen.getByText(/Blocked: task 7/)).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('shows empty state when there are no notifications', () => { |
| 68 | + render( |
| 69 | + <Harness> |
| 70 | + <NotificationCenter /> |
| 71 | + </Harness> |
| 72 | + ); |
| 73 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 74 | + expect(screen.getByText(/no notifications/i)).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('marks a single notification read via its X button', () => { |
| 78 | + render( |
| 79 | + <Harness> |
| 80 | + <Adder buttonId="add-1" payload={{ type: 'batch.completed', message: 'msg-x' }} /> |
| 81 | + <NotificationCenter /> |
| 82 | + </Harness> |
| 83 | + ); |
| 84 | + fireEvent.click(screen.getByTestId('add-1')); |
| 85 | + expect(screen.getByTestId('notification-badge')).toHaveTextContent('1'); |
| 86 | + |
| 87 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 88 | + const item = screen.getByText(/msg-x/).closest('[data-testid="notification-item"]'); |
| 89 | + expect(item).toBeTruthy(); |
| 90 | + const markBtn = within(item as HTMLElement).getByRole('button', { name: /mark as read/i }); |
| 91 | + fireEvent.click(markBtn); |
| 92 | + |
| 93 | + expect(screen.queryByTestId('notification-badge')).not.toBeInTheDocument(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('marks all notifications read', () => { |
| 97 | + render( |
| 98 | + <Harness> |
| 99 | + <Adder buttonId="add-1" payload={{ type: 'batch.completed', message: 'a' }} /> |
| 100 | + <Adder buttonId="add-2" payload={{ type: 'blocker.created', message: 'b' }} /> |
| 101 | + <NotificationCenter /> |
| 102 | + </Harness> |
| 103 | + ); |
| 104 | + fireEvent.click(screen.getByTestId('add-1')); |
| 105 | + fireEvent.click(screen.getByTestId('add-2')); |
| 106 | + expect(screen.getByTestId('notification-badge')).toHaveTextContent('2'); |
| 107 | + |
| 108 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 109 | + fireEvent.click(screen.getByRole('button', { name: /mark all read/i })); |
| 110 | + |
| 111 | + expect(screen.queryByTestId('notification-badge')).not.toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('clears all notifications', () => { |
| 115 | + render( |
| 116 | + <Harness> |
| 117 | + <Adder buttonId="add-1" payload={{ type: 'batch.completed', message: 'a' }} /> |
| 118 | + <NotificationCenter /> |
| 119 | + </Harness> |
| 120 | + ); |
| 121 | + fireEvent.click(screen.getByTestId('add-1')); |
| 122 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 123 | + fireEvent.click(screen.getByRole('button', { name: /clear all/i })); |
| 124 | + |
| 125 | + expect(screen.queryByText(/^a$/)).not.toBeInTheDocument(); |
| 126 | + expect(screen.getByText(/no notifications/i)).toBeInTheDocument(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('does not render a green checkmark for a FAILED batch notification', () => { |
| 130 | + // Regression for codex review finding: FAILED/CANCELLED must not look like success. |
| 131 | + const stored = [ |
| 132 | + { |
| 133 | + id: '1', |
| 134 | + type: 'batch.completed', |
| 135 | + batchStatus: 'FAILED', |
| 136 | + message: 'Batch X failed — 2/5 tasks completed before failure', |
| 137 | + timestamp: new Date().toISOString(), |
| 138 | + read: false, |
| 139 | + }, |
| 140 | + ]; |
| 141 | + localStorage.setItem(NOTIFICATIONS_STORAGE_KEY, JSON.stringify(stored)); |
| 142 | + |
| 143 | + render( |
| 144 | + <Harness> |
| 145 | + <NotificationCenter /> |
| 146 | + </Harness> |
| 147 | + ); |
| 148 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 149 | + const item = screen.getByText(/Batch X failed/).closest('[data-testid="notification-item"]'); |
| 150 | + expect(item).toBeTruthy(); |
| 151 | + // The success icon must not be present in a failed-batch row |
| 152 | + expect(within(item as HTMLElement).queryByTestId('icon-CheckmarkCircle01Icon')).toBeNull(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('closes the dropdown when Escape is pressed', () => { |
| 156 | + render( |
| 157 | + <Harness> |
| 158 | + <NotificationCenter /> |
| 159 | + </Harness> |
| 160 | + ); |
| 161 | + const bell = screen.getByRole('button', { name: /notifications/i }); |
| 162 | + fireEvent.click(bell); |
| 163 | + expect(bell).toHaveAttribute('aria-expanded', 'true'); |
| 164 | + |
| 165 | + fireEvent.keyDown(document, { key: 'Escape' }); |
| 166 | + expect(bell).toHaveAttribute('aria-expanded', 'false'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('exposes aria-expanded and aria-controls on the bell button', () => { |
| 170 | + render( |
| 171 | + <Harness> |
| 172 | + <NotificationCenter /> |
| 173 | + </Harness> |
| 174 | + ); |
| 175 | + const bell = screen.getByRole('button', { name: /notifications/i }); |
| 176 | + expect(bell).toHaveAttribute('aria-expanded', 'false'); |
| 177 | + expect(bell).toHaveAttribute('aria-controls', 'notification-popover'); |
| 178 | + |
| 179 | + fireEvent.click(bell); |
| 180 | + expect(bell).toHaveAttribute('aria-expanded', 'true'); |
| 181 | + expect(document.getElementById('notification-popover')).toBeInTheDocument(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('renders notifications from existing localStorage state on mount', () => { |
| 185 | + const stored = [ |
| 186 | + { |
| 187 | + id: '1', |
| 188 | + type: 'gate.run.failed', |
| 189 | + message: 'gate failed: unit', |
| 190 | + timestamp: new Date().toISOString(), |
| 191 | + read: false, |
| 192 | + }, |
| 193 | + ]; |
| 194 | + localStorage.setItem(NOTIFICATIONS_STORAGE_KEY, JSON.stringify(stored)); |
| 195 | + |
| 196 | + render( |
| 197 | + <Harness> |
| 198 | + <NotificationCenter /> |
| 199 | + </Harness> |
| 200 | + ); |
| 201 | + expect(screen.getByTestId('notification-badge')).toHaveTextContent('1'); |
| 202 | + fireEvent.click(screen.getByRole('button', { name: /notifications/i })); |
| 203 | + expect(screen.getByText(/gate failed: unit/)).toBeInTheDocument(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments