|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 4 | +import type { ReactNode } from 'react'; |
| 5 | +import { useReconcileDeletedMonitors } from '../useReconcileDeletedMonitors'; |
| 6 | +import { useSettingsStore, DEFAULT_MONTAGE_GROUP_LAYOUT } from '../../stores/settings'; |
| 7 | +import { useDashboardStore } from '../../stores/dashboard'; |
| 8 | +import { getMonitors } from '../../api/monitors'; |
| 9 | + |
| 10 | +vi.mock('../../api/monitors', () => ({ getMonitors: vi.fn() })); |
| 11 | +vi.mock('../useCurrentProfile', () => ({ |
| 12 | + useCurrentProfile: () => ({ currentProfile: { id: 'p1' }, settings: {} }), |
| 13 | +})); |
| 14 | +vi.mock('../../stores/auth', () => ({ |
| 15 | + useAuthStore: (selector: (s: { isAuthenticated: boolean }) => unknown) => |
| 16 | + selector({ isAuthenticated: true }), |
| 17 | +})); |
| 18 | + |
| 19 | +const mockGetMonitors = vi.mocked(getMonitors); |
| 20 | + |
| 21 | +function monitorList(ids: string[]) { |
| 22 | + return { monitors: ids.map((Id) => ({ Monitor: { Id }, Monitor_Status: undefined })) } as never; |
| 23 | +} |
| 24 | + |
| 25 | +function wrapper({ children }: { children: ReactNode }) { |
| 26 | + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); |
| 27 | + return <QueryClientProvider client={client}>{children}</QueryClientProvider>; |
| 28 | +} |
| 29 | + |
| 30 | +function seedProfile() { |
| 31 | + useSettingsStore.setState({ |
| 32 | + profileSettings: { |
| 33 | + p1: { |
| 34 | + excludedMonitorIds: ['1', '99'], |
| 35 | + montageByGroup: { |
| 36 | + all: { ...DEFAULT_MONTAGE_GROUP_LAYOUT, hiddenMonitorIds: ['99'] }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } as never, |
| 40 | + }); |
| 41 | + useDashboardStore.setState({ |
| 42 | + widgets: { |
| 43 | + p1: [ |
| 44 | + { |
| 45 | + id: 'w1', |
| 46 | + type: 'monitor', |
| 47 | + settings: { monitorIds: ['1', '99'] }, |
| 48 | + layout: { i: 'w1', x: 0, y: 0, w: 4, h: 4 }, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function storedState() { |
| 56 | + return { |
| 57 | + excluded: useSettingsStore.getState().getProfileSettings('p1').excludedMonitorIds, |
| 58 | + montageHidden: |
| 59 | + useSettingsStore.getState().getProfileSettings('p1').montageByGroup?.all?.hiddenMonitorIds, |
| 60 | + widgetIds: useDashboardStore.getState().widgets.p1?.[0].settings.monitorIds, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +describe('useReconcileDeletedMonitors', () => { |
| 65 | + beforeEach(() => { |
| 66 | + mockGetMonitors.mockReset(); |
| 67 | + seedProfile(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('drops ids for monitors ZoneMinder no longer has, everywhere they are stored', async () => { |
| 71 | + mockGetMonitors.mockResolvedValue(monitorList(['1'])); |
| 72 | + |
| 73 | + renderHook(() => useReconcileDeletedMonitors(), { wrapper }); |
| 74 | + |
| 75 | + await waitFor(() => expect(storedState().excluded).toEqual(['1'])); |
| 76 | + expect(storedState().montageHidden).toEqual([]); |
| 77 | + expect(storedState().widgetIds).toEqual(['1']); |
| 78 | + }); |
| 79 | + |
| 80 | + it('keeps a hidden monitor that still exists, which the ordinary monitor list omits', async () => { |
| 81 | + // '99' is hidden, so getMonitors() without includeExcluded would not return |
| 82 | + // it. Reconciling against that list would delete the user's own hidden |
| 83 | + // entry, so the hook must ask for the list that includes it. |
| 84 | + mockGetMonitors.mockResolvedValue(monitorList(['1', '99'])); |
| 85 | + |
| 86 | + renderHook(() => useReconcileDeletedMonitors(), { wrapper }); |
| 87 | + |
| 88 | + await waitFor(() => expect(mockGetMonitors).toHaveBeenCalled()); |
| 89 | + expect(mockGetMonitors).toHaveBeenCalledWith({ includeExcluded: true }); |
| 90 | + expect(storedState().excluded).toEqual(['1', '99']); |
| 91 | + expect(storedState().widgetIds).toEqual(['1', '99']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('changes nothing when the fetch fails: an error is not proof of deletion', async () => { |
| 95 | + mockGetMonitors.mockRejectedValue(new Error('offline')); |
| 96 | + |
| 97 | + renderHook(() => useReconcileDeletedMonitors(), { wrapper }); |
| 98 | + |
| 99 | + await waitFor(() => expect(mockGetMonitors).toHaveBeenCalled()); |
| 100 | + expect(storedState().excluded).toEqual(['1', '99']); |
| 101 | + expect(storedState().widgetIds).toEqual(['1', '99']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('changes nothing when the server returns no monitors at all', async () => { |
| 105 | + mockGetMonitors.mockResolvedValue(monitorList([])); |
| 106 | + |
| 107 | + renderHook(() => useReconcileDeletedMonitors(), { wrapper }); |
| 108 | + |
| 109 | + await waitFor(() => expect(mockGetMonitors).toHaveBeenCalled()); |
| 110 | + expect(storedState().excluded).toEqual(['1', '99']); |
| 111 | + expect(storedState().montageHidden).toEqual(['99']); |
| 112 | + expect(storedState().widgetIds).toEqual(['1', '99']); |
| 113 | + }); |
| 114 | +}); |
0 commit comments