From bfdbf4d81157bc9b74b38f960f8d34d222a5079d Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 14 Nov 2026 16:40:51 +0100 Subject: [PATCH] fix(notifications): format the tests, move the open-planner tab test into its own file, lower the exhaustive-deps baseline --- client/scripts/eslint-baseline.json | 2 +- .../useNotificationItemActions.test.ts | 46 ++++++-- .../useTripPlanner.tabLink.test.tsx | 110 ++++++++++++++++++ .../pages/tripPlanner/useTripPlanner.test.tsx | 15 --- .../unit/nest/notifications.service.test.ts | 2 +- 5 files changed, 146 insertions(+), 29 deletions(-) create mode 100644 client/src/pages/tripPlanner/useTripPlanner.tabLink.test.tsx diff --git a/client/scripts/eslint-baseline.json b/client/scripts/eslint-baseline.json index 651840beb..a6e2971fb 100644 --- a/client/scripts/eslint-baseline.json +++ b/client/scripts/eslint-baseline.json @@ -11,7 +11,7 @@ "react-hooks/refs": 553, "react-hooks/set-state-in-effect": 192, "@typescript-eslint/no-floating-promises": 163, - "react-hooks/exhaustive-deps": 114, + "react-hooks/exhaustive-deps": 113, "@typescript-eslint/no-unused-vars": 122, "react-hooks/immutability": 28, "max-lines-per-function": 128, diff --git a/client/src/components/Notifications/useNotificationItemActions.test.ts b/client/src/components/Notifications/useNotificationItemActions.test.ts index a9bf7afd3..36d5dc7c4 100644 --- a/client/src/components/Notifications/useNotificationItemActions.test.ts +++ b/client/src/components/Notifications/useNotificationItemActions.test.ts @@ -63,9 +63,14 @@ describe('useNotificationItemActions', () => { it.each(['notif.vacay_invite.title', 'notif.vacay_share.title'])( 'opens the Vacay list for %s, including stored detail links', async (title_key) => { - const { result } = renderHook(() => useNotificationItemActions(notification({ - title_key, navigate_target: '/vacay/42', - }))); + const { result } = renderHook(() => + useNotificationItemActions( + notification({ + title_key, + navigate_target: '/vacay/42', + }) + ) + ); await act(() => result.current.handleNavigate()); expect(navigate).toHaveBeenCalledWith('/vacay'); } @@ -74,24 +79,41 @@ describe('useNotificationItemActions', () => { it.each(['/trips/42', '/trips/42?tab=collab'])( 'opens Collab for both old and new message links: %s', async (navigate_target) => { - const { result } = renderHook(() => useNotificationItemActions(notification({ - title_key: 'notif.collab_message.title', navigate_target, - }))); + const { result } = renderHook(() => + useNotificationItemActions( + notification({ + title_key: 'notif.collab_message.title', + navigate_target, + }) + ) + ); await act(() => result.current.handleNavigate()); expect(navigate).toHaveBeenCalledWith('/trips/42?tab=collab'); } ); it('uses a trip-scoped message target when its link is missing, otherwise falls back to Dashboard', async () => { - const scoped = renderHook(() => useNotificationItemActions(notification({ - title_key: 'notif.collab_message.title', scope: 'trip', target: 42, navigate_target: null, - }))); + const scoped = renderHook(() => + useNotificationItemActions( + notification({ + title_key: 'notif.collab_message.title', + scope: 'trip', + target: 42, + navigate_target: null, + }) + ) + ); await act(() => scoped.result.current.handleNavigate()); expect(navigate).toHaveBeenLastCalledWith('/trips/42?tab=collab'); - const missing = renderHook(() => useNotificationItemActions(notification({ - title_key: 'notif.collab_message.title', navigate_target: '/dashboard', - }))); + const missing = renderHook(() => + useNotificationItemActions( + notification({ + title_key: 'notif.collab_message.title', + navigate_target: '/dashboard', + }) + ) + ); await act(() => missing.result.current.handleNavigate()); expect(navigate).toHaveBeenLastCalledWith('/dashboard'); }); diff --git a/client/src/pages/tripPlanner/useTripPlanner.tabLink.test.tsx b/client/src/pages/tripPlanner/useTripPlanner.tabLink.test.tsx new file mode 100644 index 000000000..21cb131e7 --- /dev/null +++ b/client/src/pages/tripPlanner/useTripPlanner.tabLink.test.tsx @@ -0,0 +1,110 @@ +// FE-TP-TABLINK-001 +import { act, renderHook, waitFor } from '@testing-library/react'; +import type { ReactNode } from 'react'; + +import { buildTrip, buildUser } from '../../../tests/helpers/factories'; +import { makeActions } from '../../../tests/helpers/plannerActions'; +import { resetAllStores, seedStore } from '../../../tests/helpers/store'; +import { addonsApi, airtrailApi, authApi, healthApi, tripsApi } from '../../api/client'; +import { TranslationProvider } from '../../i18n/TranslationContext'; +import { useAuthStore } from '../../store/authStore'; +import { useBackgroundTasksStore } from '../../store/backgroundTasksStore'; +import { usePluginStore } from '../../store/pluginStore'; +import { type TripStoreState, useTripStore } from '../../store/tripStore'; +import { useTripPlanner } from './useTripPlanner'; + +/** + * A `?tab=` that arrives while the planner is already open, the way a notification + * opens one. The planner is shared by the desktop page and the phone shell, so the + * switch has to happen here and not in a part only one of them mounts. + */ + +let searchParams = new URLSearchParams(); +const setSearchParams = vi.fn((updater: (p: URLSearchParams) => URLSearchParams) => { + const next = new URLSearchParams(searchParams); + searchParams = typeof updater === 'function' ? updater(next) : next; +}); + +vi.mock('react-router', () => ({ + useParams: () => ({ id: '42' }), + useNavigate: () => vi.fn(), + useSearchParams: () => [searchParams, setSearchParams], +})); + +vi.mock('../../hooks/useTripWebSocket', () => ({ useTripWebSocket: vi.fn() })); + +vi.mock('../../hooks/useRouteCalculation', () => ({ + useRouteCalculation: () => ({ + route: null, + routeSegments: [], + routeVias: [], + routeInfo: null, + setRoute: vi.fn(), + setRouteInfo: vi.fn(), + updateRouteForDay: vi.fn(async () => {}), + }), +})); + +vi.mock('../../hooks/useAirtrailConnection', () => ({ + useAirtrailConnection: () => ({ airtrailEnabled: false, connected: false, available: false, loading: false }), +})); + +vi.mock('../../repo/accommodationRepo', async (importOriginal) => { + const real = await importOriginal(); + return { accommodationRepo: { ...real.accommodationRepo, list: vi.fn(async () => ({ accommodations: [] })) } }; +}); + +vi.mock('../../services/photoService', () => ({ + getCached: vi.fn(() => undefined), + fetchPhoto: vi.fn(), +})); + +function wrapper({ children }: { children: ReactNode }) { + return {children}; +} + +beforeEach(() => { + vi.clearAllMocks(); + resetAllStores(); + searchParams = new URLSearchParams(); + usePluginStore.setState({ plugins: [], loaded: true }); + useBackgroundTasksStore.setState({ tasks: [] }); + seedStore(useAuthStore, { user: buildUser({ id: 1 }), isAuthenticated: true, placesPhotosEnabled: false }); + useTripStore.setState({ + trip: buildTrip({ id: 42, title: 'Kyoto' }), + isLoading: false, + days: [], + places: [], + assignments: {}, + reservations: [], + ...(makeActions() as unknown as Partial), + } as Partial); + + vi.spyOn(addonsApi, 'enabled').mockResolvedValue({ addons: [{ id: 'collab' }] }); + vi.spyOn(authApi, 'getAppConfig').mockResolvedValue({}); + vi.spyOn(healthApi, 'features').mockResolvedValue({ bookingImport: false, aiParsing: false }); + vi.spyOn(tripsApi, 'getMembers').mockResolvedValue({ owner: null, members: [] }); + vi.spyOn(airtrailApi, 'sync').mockResolvedValue({ changed: 0 }); +}); + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('useTripPlanner — ?tab= while the planner is open', () => { + it('FE-TP-TABLINK-001: a message link switches an already mounted planner to Collab and remembers the tab', async () => { + const { result, rerender } = renderHook(() => useTripPlanner(), { wrapper }); + await act(async () => { + await Promise.resolve(); + }); + await waitFor(() => expect(result.current.TRIP_TABS.map((t) => t.id)).toContain('collab')); + expect(result.current.activeTab).toBe('plan'); + + searchParams = new URLSearchParams('tab=collab'); + rerender(); + + await waitFor(() => expect(result.current.activeTab).toBe('collab')); + expect(searchParams.has('tab')).toBe(false); + expect(sessionStorage.getItem('trip-tab-42')).toBe('collab'); + }); +}); diff --git a/client/src/pages/tripPlanner/useTripPlanner.test.tsx b/client/src/pages/tripPlanner/useTripPlanner.test.tsx index 0b9b2f46b..e2a0e0a3e 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.test.tsx +++ b/client/src/pages/tripPlanner/useTripPlanner.test.tsx @@ -524,21 +524,6 @@ describe('useTripPlanner — tabs', () => { expect(result.current.activeTab).toBe('collab') }) - it('a message link switches an already mounted planner to Collab and remembers the tab', async () => { - vi.mocked(addonsApi.enabled).mockResolvedValue({ addons: [{ id: 'collab' }] }) - seedTrip() - const { result, rerender } = await renderPlanner() - await waitFor(() => expect(result.current.TRIP_TABS.map(t => t.id)).toContain('collab')) - expect(result.current.activeTab).toBe('plan') - - searchParams = new URLSearchParams('tab=collab') - rerender() - - await waitFor(() => expect(result.current.activeTab).toBe('collab')) - expect(searchParams.has('tab')).toBe(false) - expect(sessionStorage.getItem('trip-tab-42')).toBe('collab') - }) - it('FE-TP-HOOK-110: but a tab whose addon really is off still falls back to plan', async () => { vi.mocked(addonsApi.enabled).mockResolvedValue({ addons: [] }) searchParams = new URLSearchParams('tab=finanzplan') diff --git a/server/tests/unit/nest/notifications.service.test.ts b/server/tests/unit/nest/notifications.service.test.ts index 617579865..2e3409760 100644 --- a/server/tests/unit/nest/notifications.service.test.ts +++ b/server/tests/unit/nest/notifications.service.test.ts @@ -542,7 +542,7 @@ describe('send() — in-app notification content', () => { const body = JSON.parse(fetchMock.mock.calls[0][1].body); const link = new URL(body.link); expect(link.pathname + link.search).toBe(expected); - } + }, ); it('NSVC-014 — navigate_target uses /admin for version_available event', async () => {