|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 4 | + |
| 5 | +vi.mock('../PageNotifications/usePageNotifications'); |
| 6 | +vi.mock('../PageNotifications/usePageNotificationsRead'); |
| 7 | + |
| 8 | +import { usePageNotifications } from '../PageNotifications/usePageNotifications'; |
| 9 | +import { usePageNotificationsRead } from '../PageNotifications/usePageNotificationsRead'; |
| 10 | +import { PageNotificationsIcon } from './PageNotificationsIcon'; |
| 11 | + |
| 12 | +describe('PageNotificationsIcon', () => { |
| 13 | + const mockSetNotificationsDrawerOpen = vi.fn(); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | + vi.mocked(usePageNotifications).mockReturnValue({ |
| 18 | + setNotificationsDrawerOpen: mockSetNotificationsDrawerOpen, |
| 19 | + notificationGroups: {}, |
| 20 | + notificationsDrawerOpen: false, |
| 21 | + setNotificationGroups: vi.fn(), |
| 22 | + }); |
| 23 | + vi.mocked(usePageNotificationsRead).mockReturnValue({ |
| 24 | + isNotificationRead: vi.fn().mockReturnValue(false), |
| 25 | + markAllNotificationsRead: vi.fn(), |
| 26 | + markAllNotificationsUnread: vi.fn(), |
| 27 | + setNotificationRead: vi.fn(), |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should render the notification badge with read variant when there are no notifications', () => { |
| 32 | + render(<PageNotificationsIcon />); |
| 33 | + const badge = screen.getByTestId('notification-badge'); |
| 34 | + expect(badge).toBeInTheDocument(); |
| 35 | + expect(badge).not.toHaveClass('pf-m-unread'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('should show unread variant when there are unread notifications', () => { |
| 39 | + vi.mocked(usePageNotifications).mockReturnValue({ |
| 40 | + setNotificationsDrawerOpen: mockSetNotificationsDrawerOpen, |
| 41 | + notificationGroups: { |
| 42 | + group1: { |
| 43 | + title: 'Group 1', |
| 44 | + notifications: [ |
| 45 | + { id: 'notif-1', title: 'Notification 1', variant: 'info' }, |
| 46 | + { id: 'notif-2', title: 'Notification 2', variant: 'warning' }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + }, |
| 50 | + notificationsDrawerOpen: false, |
| 51 | + setNotificationGroups: vi.fn(), |
| 52 | + }); |
| 53 | + |
| 54 | + render(<PageNotificationsIcon />); |
| 55 | + expect(screen.getByTestId('notification-badge')).toHaveClass('pf-m-unread'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should not count already-read notifications as unread', () => { |
| 59 | + vi.mocked(usePageNotifications).mockReturnValue({ |
| 60 | + setNotificationsDrawerOpen: mockSetNotificationsDrawerOpen, |
| 61 | + notificationGroups: { |
| 62 | + group1: { |
| 63 | + title: 'Group 1', |
| 64 | + notifications: [{ id: 'notif-1', title: 'Notification 1', variant: 'info' }], |
| 65 | + }, |
| 66 | + }, |
| 67 | + notificationsDrawerOpen: false, |
| 68 | + setNotificationGroups: vi.fn(), |
| 69 | + }); |
| 70 | + vi.mocked(usePageNotificationsRead).mockReturnValue({ |
| 71 | + isNotificationRead: vi.fn().mockReturnValue(true), |
| 72 | + markAllNotificationsRead: vi.fn(), |
| 73 | + markAllNotificationsUnread: vi.fn(), |
| 74 | + setNotificationRead: vi.fn(), |
| 75 | + }); |
| 76 | + |
| 77 | + render(<PageNotificationsIcon />); |
| 78 | + expect(screen.getByTestId('notification-badge')).not.toHaveClass('pf-m-unread'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should skip notifications with non-string ids from unread count', () => { |
| 82 | + vi.mocked(usePageNotifications).mockReturnValue({ |
| 83 | + setNotificationsDrawerOpen: mockSetNotificationsDrawerOpen, |
| 84 | + notificationGroups: { |
| 85 | + group1: { |
| 86 | + title: 'Group 1', |
| 87 | + notifications: [ |
| 88 | + { id: 42 as unknown as string, title: 'Notification 1', variant: 'info' }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + }, |
| 92 | + notificationsDrawerOpen: false, |
| 93 | + setNotificationGroups: vi.fn(), |
| 94 | + }); |
| 95 | + |
| 96 | + render(<PageNotificationsIcon />); |
| 97 | + expect(screen.getByTestId('notification-badge')).not.toHaveClass('pf-m-unread'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('should toggle the notifications drawer when badge is clicked', async () => { |
| 101 | + const user = userEvent.setup(); |
| 102 | + render(<PageNotificationsIcon />); |
| 103 | + |
| 104 | + await user.click(screen.getByTestId('notification-badge')); |
| 105 | + |
| 106 | + expect(mockSetNotificationsDrawerOpen).toHaveBeenCalled(); |
| 107 | + const toggleFn = mockSetNotificationsDrawerOpen.mock.calls[0][0] as (open: boolean) => boolean; |
| 108 | + expect(toggleFn(false)).toBe(true); |
| 109 | + expect(toggleFn(true)).toBe(false); |
| 110 | + }); |
| 111 | +}); |
0 commit comments