import { describe, it, expect, beforeEach } from 'vitest'; import userEvent from '@testing-library/user-event'; import { http, HttpResponse } from 'msw'; import { render, screen, waitFor, within } from '../../../helpers/render'; import { resetAllStores } from '../../../helpers/store'; import { server } from '../../../helpers/msw/server'; import MNotifications from '../../../../src/mobile/screens/notifications/MNotifications'; // FE-MOB-NOTIF-001 onwards beforeEach(() => { resetAllStores(); }); describe('MNotifications', () => { it('FE-MOB-NOTIF-001: loads the first page with unread badge and bulk actions', async () => { render(); // Default handler: 25 notifications (page of 20), unread_count 5. await waitFor(() => { expect(screen.getAllByText('notif.title')).toHaveLength(20); }); expect(screen.getByText('5')).toBeInTheDocument(); await userEvent.click(screen.getAllByRole('button', { name: 'More' })[0]); expect(screen.getByRole('button', { name: 'Mark all read' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Delete all' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Back' })).toBeInTheDocument(); }); it('FE-MOB-NOTIF-001b: Delete all asks for confirmation before it clears the list', async () => { const user = userEvent.setup(); render(); await waitFor(() => { expect(screen.getAllByText('notif.title')).toHaveLength(20); }); await user.click(screen.getAllByRole('button', { name: 'More' })[0]); await user.click(within(screen.getByRole('dialog', { name: 'Notifications' })).getByRole('button', { name: 'Delete all' })); expect(screen.getAllByText('notif.title')).toHaveLength(20); await user.click(screen.getByRole('button', { name: 'Cancel' })); expect(screen.getAllByText('notif.title')).toHaveLength(20); await user.click(screen.getAllByRole('button', { name: 'More' })[0]); await user.click(within(screen.getByRole('dialog', { name: 'Notifications' })).getByRole('button', { name: 'Delete all' })); await user.click(within(screen.getByRole('dialog', { name: 'Delete all notifications?' })).getByRole('button', { name: 'Delete all' })); await waitFor(() => { expect(screen.queryAllByText('notif.title')).toHaveLength(0); }); }); it('distinguishes a failed fetch from an empty inbox and retries on request', async () => { let failed = false; server.use(http.get('/api/notifications/in-app', () => { if (failed) return HttpResponse.json({ notifications: [], total: 0, unread_count: 0 }); failed = true; return new HttpResponse(null, { status: 503 }); })); render(); const retry = await screen.findByRole('button', { name: 'Try again' }); expect(screen.queryByText('No notifications')).not.toBeInTheDocument(); await userEvent.click(retry); expect(await screen.findByText('No notifications')).toBeInTheDocument(); }); it('FE-MOB-NOTIF-002: the unread segment filters out read notifications', async () => { const user = userEvent.setup(); render(); await waitFor(() => { expect(screen.getAllByText('notif.title')).toHaveLength(20); }); // First 5 rows of the default handler are unread. await user.click(screen.getByRole('tab', { name: 'Unread' })); await waitFor(() => { expect(screen.getAllByText('notif.title')).toHaveLength(5); }); await user.click(screen.getByRole('tab', { name: 'All' })); await waitFor(() => { expect(screen.getAllByText('notif.title')).toHaveLength(20); }); }); });