Files
TREK/client/tests/unit/stores/addonStore.test.ts
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

148 lines
5.8 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest';
import { http, HttpResponse } from 'msw';
import { server } from '../../helpers/msw/server';
import { instanceAddonOn, useAddonStore } from '../../../src/store/addonStore';
import { resetAllStores } from '../../helpers/store';
beforeEach(() => {
resetAllStores();
});
describe('addonStore', () => {
describe('FE-ADDON-001: loadAddons()', () => {
it('fetches and stores enabled addons', async () => {
await useAddonStore.getState().loadAddons();
const state = useAddonStore.getState();
expect(state.loaded).toBe(true);
expect(state.addons.length).toBeGreaterThan(0);
expect(state.addons[0]).toHaveProperty('id');
expect(state.addons[0]).toHaveProperty('enabled', true);
expect(state.bagTracking).toBe(false);
});
it('captures the global bagTracking flag from the response', async () => {
server.use(
http.get('/api/addons', () =>
HttpResponse.json({ bagTracking: true, addons: [] })
)
);
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().bagTracking).toBe(true);
});
it('captures whether wanderer may be used under Tours', async () => {
server.use(
http.get('/api/addons', () => HttpResponse.json({ toursWanderer: true, toursHikingMap: true, addons: [] }))
);
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().toursWanderer).toBe(true);
server.use(http.get('/api/addons', () => HttpResponse.json({ addons: [] })));
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().toursWanderer).toBe(false);
});
it('keeps the collab chat on unless the response turns it off', async () => {
server.use(http.get('/api/addons', () => HttpResponse.json({ addons: [] })));
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().collabChat).toBe(true);
server.use(http.get('/api/addons', () => HttpResponse.json({ addons: [], collabFeatures: { chat: true } })));
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().collabChat).toBe(true);
server.use(http.get('/api/addons', () => HttpResponse.json({ addons: [], collabFeatures: { chat: false } })));
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().collabChat).toBe(false);
});
});
describe('FE-ADDON-002: isEnabled returns true for known addon', () => {
it('returns true when addon is in the list and enabled', async () => {
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().isEnabled('vacay')).toBe(true);
});
});
describe('FE-ADDON-003: isEnabled returns false for unknown addon', () => {
it('returns false when addon is not in the list', async () => {
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().isEnabled('nonexistent')).toBe(false);
});
});
describe('FE-ADDON-004: API failure', () => {
it('sets loaded: true and keeps addons empty on API error', async () => {
server.use(
http.get('/api/addons', () =>
HttpResponse.json({ error: 'Server error' }, { status: 500 })
)
);
await useAddonStore.getState().loadAddons();
const state = useAddonStore.getState();
expect(state.loaded).toBe(true);
expect(state.addons).toEqual([]);
expect(state.failed).toBe(true);
// The empty list says nothing, so the caller's guess stands.
expect(instanceAddonOn(state, 'packing', true)).toBe(true);
});
it('a failed refresh after a good read keeps that read as what the instance has', async () => {
server.use(http.get('/api/addons', () => HttpResponse.json({ addons: [{ id: 'packing', enabled: true }] })));
await useAddonStore.getState().loadAddons();
server.use(http.get('/api/addons', () => HttpResponse.json({ error: 'down' }, { status: 500 })));
await useAddonStore.getState().loadAddons();
const state = useAddonStore.getState();
expect(state.failed).toBe(false);
expect(instanceAddonOn(state, 'packing', false)).toBe(true);
expect(instanceAddonOn(state, 'budget', true)).toBe(false);
});
});
describe('FE-ADDON-005: the Tours options and the collab features', () => {
it('keeps the Tours options apart from the combined flags', async () => {
server.use(
http.get('/api/addons', () =>
HttpResponse.json({
toursWanderer: false,
toursHikingMap: false,
toursCycleMap: false,
toursOptions: { wanderer: true, hikingMap: true, cycleMap: false },
addons: [],
})
)
);
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().toursOptions).toEqual({ wanderer: true, hikingMap: true, cycleMap: false });
expect(useAddonStore.getState().toursWanderer).toBe(false);
});
it('reads the options from the combined flags of a server that does not list them apart', async () => {
server.use(
http.get('/api/addons', () => HttpResponse.json({ toursWanderer: true, toursCycleMap: true, addons: [] }))
);
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().toursOptions).toEqual({ wanderer: true, hikingMap: false, cycleMap: true });
});
it('keeps every collab feature, on unless the response turns it off', async () => {
server.use(
http.get('/api/addons', () => HttpResponse.json({ addons: [], collabFeatures: { polls: false } }))
);
await useAddonStore.getState().loadAddons();
expect(useAddonStore.getState().collabFeatures).toEqual({
chat: true,
notes: true,
links: true,
polls: false,
whatsnext: true,
});
});
});
});