|
| 1 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import type { ReactNode } from 'react'; |
| 3 | + |
| 4 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 5 | + |
| 6 | +import { mockGitHubCloudAccount } from '../../__mocks__/account-mocks'; |
| 7 | +import { |
| 8 | + mockGitifyNotification, |
| 9 | + mockSingleAccountNotifications, |
| 10 | +} from '../../__mocks__/notifications-mocks'; |
| 11 | + |
| 12 | +import { useAccountsStore } from '../../stores'; |
| 13 | + |
| 14 | +import { getAdapter } from '../../utils/forges/registry'; |
| 15 | +import * as notificationsUtils from '../../utils/notifications/notifications'; |
| 16 | +import { useNotifications } from '../useNotifications'; |
| 17 | +import { useNotificationActions } from './useNotificationActions'; |
| 18 | + |
| 19 | +// Exercise the real hooks (globally mocked in vitest.setup) |
| 20 | +vi.unmock('./useNotificationActions'); |
| 21 | +vi.unmock('../useNotifications'); |
| 22 | + |
| 23 | +vi.mock('../../utils/notifications/notifications', async () => { |
| 24 | + const actual = await vi.importActual<typeof import('../../utils/notifications/notifications')>( |
| 25 | + '../../utils/notifications/notifications', |
| 26 | + ); |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + getAllNotifications: vi.fn(), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +const getAllNotificationsMock = vi.mocked(notificationsUtils.getAllNotifications); |
| 34 | + |
| 35 | +const createWrapper = () => { |
| 36 | + const queryClient = new QueryClient({ |
| 37 | + defaultOptions: { |
| 38 | + queries: { |
| 39 | + retry: false, |
| 40 | + refetchOnWindowFocus: false, |
| 41 | + refetchInterval: false, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + return { |
| 47 | + queryClient, |
| 48 | + wrapper: ({ children }: { children: ReactNode }) => ( |
| 49 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 50 | + ), |
| 51 | + }; |
| 52 | +}; |
| 53 | + |
| 54 | +describe('renderer/hooks/notifications/useNotificationActions.ts', () => { |
| 55 | + const githubAdapter = getAdapter('github'); |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + getAllNotificationsMock.mockReset(); |
| 59 | + useAccountsStore.setState({ accounts: [mockGitHubCloudAccount] }); |
| 60 | + mockGitifyNotification.unread = true; |
| 61 | + }); |
| 62 | + |
| 63 | + it('exposes the notification actions without fetching or creating a query', () => { |
| 64 | + const { queryClient, wrapper } = createWrapper(); |
| 65 | + |
| 66 | + const { result } = renderHook(() => useNotificationActions(), { wrapper }); |
| 67 | + |
| 68 | + expect(result.current.markNotificationsAsRead).toBeTypeOf('function'); |
| 69 | + expect(result.current.markNotificationsAsDone).toBeTypeOf('function'); |
| 70 | + expect(result.current.unsubscribeNotification).toBeTypeOf('function'); |
| 71 | + expect(result.current.removeAccountNotifications).toBeTypeOf('function'); |
| 72 | + expect(result.current.refetchNotifications).toBeTypeOf('function'); |
| 73 | + |
| 74 | + expect(getAllNotificationsMock).not.toHaveBeenCalled(); |
| 75 | + expect(queryClient.getQueryCache().getAll()).toHaveLength(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not add a notifications query observer', async () => { |
| 79 | + getAllNotificationsMock.mockResolvedValue(mockSingleAccountNotifications); |
| 80 | + const { queryClient, wrapper } = createWrapper(); |
| 81 | + |
| 82 | + renderHook(() => useNotificationActions(), { wrapper }); |
| 83 | + expect(queryClient.getQueryCache().getAll()).toHaveLength(0); |
| 84 | + |
| 85 | + renderHook(() => useNotifications(), { wrapper }); |
| 86 | + await waitFor(() => expect(getAllNotificationsMock).toHaveBeenCalledTimes(1)); |
| 87 | + |
| 88 | + // Only the reader subscribed; the action-only consumer added no observer. |
| 89 | + const queries = queryClient.getQueryCache().getAll(); |
| 90 | + const observers = queries.flatMap((query) => query.observers); |
| 91 | + |
| 92 | + expect(queries).toHaveLength(1); |
| 93 | + expect(observers).toHaveLength(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('refetchNotifications refetches the shared notifications query', async () => { |
| 97 | + getAllNotificationsMock.mockResolvedValue(mockSingleAccountNotifications); |
| 98 | + const { wrapper } = createWrapper(); |
| 99 | + |
| 100 | + renderHook(() => useNotifications(), { wrapper }); |
| 101 | + await waitFor(() => expect(getAllNotificationsMock).toHaveBeenCalledTimes(1)); |
| 102 | + |
| 103 | + const actions = renderHook(() => useNotificationActions(), { wrapper }); |
| 104 | + await act(async () => { |
| 105 | + await actions.result.current.refetchNotifications(); |
| 106 | + }); |
| 107 | + |
| 108 | + expect(getAllNotificationsMock).toHaveBeenCalledTimes(2); |
| 109 | + }); |
| 110 | + |
| 111 | + it('removeAccountNotifications prunes the account from the shared cache', async () => { |
| 112 | + getAllNotificationsMock.mockResolvedValue(mockSingleAccountNotifications); |
| 113 | + const { wrapper } = createWrapper(); |
| 114 | + |
| 115 | + const reader = renderHook(() => useNotifications(), { wrapper }); |
| 116 | + await waitFor(() => expect(reader.result.current.hasNotifications).toBe(true)); |
| 117 | + |
| 118 | + const actions = renderHook(() => useNotificationActions(), { wrapper }); |
| 119 | + act(() => { |
| 120 | + actions.result.current.removeAccountNotifications(mockGitHubCloudAccount); |
| 121 | + }); |
| 122 | + |
| 123 | + await waitFor(() => expect(reader.result.current.notificationCount).toBe(0)); |
| 124 | + }); |
| 125 | + |
| 126 | + it('markNotificationsAsRead reaches the shared cache so a reader reflects it', async () => { |
| 127 | + vi.spyOn(githubAdapter.accountOps, 'markThreadAsRead').mockResolvedValue(undefined); |
| 128 | + getAllNotificationsMock.mockResolvedValue(mockSingleAccountNotifications); |
| 129 | + const { wrapper } = createWrapper(); |
| 130 | + |
| 131 | + const reader = renderHook(() => useNotifications(), { wrapper }); |
| 132 | + await waitFor(() => expect(reader.result.current.hasNotifications).toBe(true)); |
| 133 | + |
| 134 | + const actions = renderHook(() => useNotificationActions(), { wrapper }); |
| 135 | + await act(async () => { |
| 136 | + await actions.result.current.markNotificationsAsRead([mockGitifyNotification]); |
| 137 | + }); |
| 138 | + |
| 139 | + await waitFor(() => expect(reader.result.current.notificationCount).toBe(0)); |
| 140 | + }); |
| 141 | +}); |
0 commit comments