fix(notifications): format the tests, move the open-planner tab test into its own file, lower the exhaustive-deps baseline

This commit is contained in:
Maurice
2026-11-14 16:40:51 +01:00
parent b068e593fc
commit bfdbf4d811
5 changed files with 146 additions and 29 deletions
+1 -1
View File
@@ -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,
@@ -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');
});
@@ -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<typeof import('../../repo/accommodationRepo')>();
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 <TranslationProvider>{children}</TranslationProvider>;
}
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<TripStoreState>),
} as Partial<TripStoreState>);
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');
});
});
@@ -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')
@@ -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 () => {