Files
TREK/client/src/components/Trips/useTripAddonRows.test.tsx
T
Maurice 9956c58dd5 feat(client): follow and switch a trip's own addons
- The trip's addon list is read through tripAddonsRepo into
  tripAddonsStore (per account and trip, kept in Dexie version 16 for
  offline), refreshed when the connection returns and live on
  tripAddons:changed.
- The planner's tabs, dock, rail and deep links, the cost, tour and list
  readers, the PDF export, the share link options, the widgets and the
  watch follow the trip instead of the instance switch.
- Trip settings gets an Addons area for the owner, on the desktop dialog
  and the phone sheet, on one useTripAddonRows hook: each trip addon with
  its switch, marked "This trip only" while it differs from the instance.
  Switching back to the instance's value drops the trip's choice.
  Switching needs a connection.
2026-11-21 13:23:09 +01:00

85 lines
4.0 KiB
TypeScript

// FE-TRIPADDONROWS-001 to FE-TRIPADDONROWS-003: the rows of the trip settings' Addons area,
// shared by the desktop dialog and the phone sheet.
import { act, renderHook, waitFor } from '@testing-library/react';
import type { TripAddon } from '@trek/shared';
import 'fake-indexeddb/auto';
import { CarFront, ListChecks, Puzzle } from 'lucide-react';
import { http, HttpResponse } from 'msw';
import type { ReactNode } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { buildUser } from '../../../tests/helpers/factories';
import { server } from '../../../tests/helpers/msw/server';
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
import { clearAll } from '../../db/offlineDb';
import { TranslationProvider } from '../../i18n/TranslationContext';
import { useAuthStore } from '../../store/authStore';
import { useTripAddonRows } from './useTripAddonRows';
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() }));
vi.mock('../shared/Toast', () => ({ useToast: () => toast }));
const LIST: TripAddon[] = [
{ id: 'packing', name: 'Packing', icon: 'ListChecks', enabled: true, instanceEnabled: true, overridden: false },
{ id: 'roadtrip', name: 'Road trip', icon: 'Route', enabled: false, instanceEnabled: true, overridden: true },
{ id: 'someplugin', name: 'Some plugin', icon: 'Unknown', enabled: true, instanceEnabled: false, overridden: true },
];
function wrapper({ children }: { children: ReactNode }) {
return <TranslationProvider>{children}</TranslationProvider>;
}
let bodies: unknown[] = [];
beforeEach(async () => {
await clearAll();
resetAllStores();
bodies = [];
seedStore(useAuthStore, { user: buildUser({ id: 1 }), isAuthenticated: true });
server.use(
http.get('/api/trips/4/addons', () => HttpResponse.json({ tripId: 4, addons: LIST })),
http.put('/api/trips/4/addons/:addonId', async ({ request }) => {
bodies.push(await request.json());
return HttpResponse.json({ tripId: 4, addons: LIST });
})
);
});
describe('useTripAddonRows', () => {
it('FE-TRIPADDONROWS-001: names each addon from the catalog, with its icon and whether the trip chose it', async () => {
const { result } = renderHook(() => useTripAddonRows(4), { wrapper });
expect(result.current.loading).toBe(true);
await waitFor(() => expect(result.current.rows).toHaveLength(3));
const [packing, roadtrip, plugin] = result.current.rows;
expect(packing).toMatchObject({ name: 'Lists', Icon: ListChecks, enabled: true, ownChoice: false });
expect(packing.description).not.toBe('');
expect(roadtrip).toMatchObject({ Icon: CarFront, enabled: false, ownChoice: true });
// An addon the catalog does not know keeps the server's name, no description and the fallback icon.
expect(plugin).toMatchObject({ name: 'Some plugin', description: '', Icon: Puzzle });
expect(result.current.loading).toBe(false);
});
it('FE-TRIPADDONROWS-002: a switch away from the instance stores the choice, back to it drops the choice', async () => {
const { result } = renderHook(() => useTripAddonRows(4), { wrapper });
await waitFor(() => expect(result.current.rows).toHaveLength(3));
act(() => result.current.rows[0].toggle());
await waitFor(() => expect(bodies).toEqual([{ enabled: false }]));
await waitFor(() => expect(result.current.rows[0].busy).toBe(false));
act(() => result.current.rows[1].toggle());
await waitFor(() => expect(bodies).toEqual([{ enabled: false }, { enabled: null }]));
});
it('FE-TRIPADDONROWS-003: a second switch while one is saving is ignored', async () => {
const { result } = renderHook(() => useTripAddonRows(4), { wrapper });
await waitFor(() => expect(result.current.rows).toHaveLength(3));
act(() => result.current.rows[0].toggle());
expect(result.current.rows[0].busy).toBe(true);
act(() => result.current.rows[2].toggle());
await waitFor(() => expect(result.current.rows[0].busy).toBe(false));
expect(bodies).toEqual([{ enabled: false }]);
});
});