Files
TREK/client/tests/unit/repo/placeRepo.test.ts
T
jubnl bcd2c8c959 fix(repo): fall back to Dexie when a network read fails (H2) (#1179)
Repos gated reads on raw navigator.onLine and the online branch had no
try/catch, so a captive portal or connected-but-no-internet (navigator.onLine
lying "true") threw a network error instead of serving the good cached copy —
blanking the trip even though Dexie held it.

- new onlineThenCache(onlineFn, cacheFn) helper: reads the cache when offline,
  and on a network-level failure (Axios error with no HTTP response). A genuine
  HTTP error (4xx/5xx — the server responded) is rethrown so callers still set
  error state / navigate, not masked by a stale cache.
- gates only on navigator.onLine, NOT the connectivity probe: the probe is a
  coarse global flag and one failed health check would otherwise divert every
  read to the (possibly empty) cache even when the request would succeed.
- every repo list/get read path routed through it (reads only — writes still
  go through the mutation queue so failures surface)
- tests: captive-portal fallback, HTTP-error rethrow, non-Axios rethrow
2026-06-15 09:25:11 +02:00

135 lines
4.3 KiB
TypeScript

/**
* placeRepo unit tests.
*
* Online path: calls REST via MSW, writes result to Dexie.
* Offline path: returns Dexie cache, skips REST.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import 'fake-indexeddb/auto';
import { server } from '../../helpers/msw/server';
import { http, HttpResponse } from 'msw';
import { placeRepo } from '../../../src/repo/placeRepo';
import { offlineDb, clearAll } from '../../../src/db/offlineDb';
import { buildPlace } from '../../helpers/factories';
beforeEach(async () => {
await clearAll();
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('placeRepo.list', () => {
it('online — fetches from REST and caches in Dexie', async () => {
const place = buildPlace({ trip_id: 1 });
server.use(
http.get('/api/trips/1/places', () => HttpResponse.json({ places: [place] })),
);
const result = await placeRepo.list(1);
expect(result.places).toHaveLength(1);
expect(result.places[0].id).toBe(place.id);
// Give fire-and-forget a tick to flush
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.where('trip_id').equals(1).toArray();
expect(cached).toHaveLength(1);
expect(cached[0].id).toBe(place.id);
});
it('offline — returns Dexie cache without REST call', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
const place = buildPlace({ trip_id: 1 });
await offlineDb.places.put(place);
let restCalled = false;
server.use(
http.get('/api/trips/1/places', () => {
restCalled = true;
return HttpResponse.json({ places: [] });
}),
);
const result = await placeRepo.list(1);
expect(result.places).toHaveLength(1);
expect(result.places[0].id).toBe(place.id);
expect(restCalled).toBe(false);
});
it('offline — returns empty array when nothing cached', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
const result = await placeRepo.list(99);
expect(result.places).toHaveLength(0);
});
it('online but request fails — falls back to Dexie cache (captive portal)', async () => {
// navigator.onLine lies "true" on a captive portal; the request throws.
const place = buildPlace({ trip_id: 1 });
await offlineDb.places.put(place);
server.use(
http.get('/api/trips/1/places', () => HttpResponse.error()),
);
const result = await placeRepo.list(1);
expect(result.places).toHaveLength(1);
expect(result.places[0].id).toBe(place.id);
});
});
describe('placeRepo.create', () => {
it('calls REST and caches created place in Dexie', async () => {
const place = buildPlace({ trip_id: 1, name: 'Eiffel Tower' });
server.use(
http.post('/api/trips/1/places', () => HttpResponse.json({ place })),
);
const result = await placeRepo.create(1, { name: 'Eiffel Tower' });
expect(result.place.name).toBe('Eiffel Tower');
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(place.id);
expect(cached).toBeDefined();
expect(cached!.name).toBe('Eiffel Tower');
});
});
describe('placeRepo.update', () => {
it('calls REST and updates Dexie cache', async () => {
const original = buildPlace({ trip_id: 1, name: 'Old Name' });
await offlineDb.places.put(original);
const updated = { ...original, name: 'New Name' };
server.use(
http.put(`/api/trips/1/places/${original.id}`, () => HttpResponse.json({ place: updated })),
);
const result = await placeRepo.update(1, original.id, { name: 'New Name' });
expect(result.place.name).toBe('New Name');
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(original.id);
expect(cached!.name).toBe('New Name');
});
});
describe('placeRepo.delete', () => {
it('calls REST and removes from Dexie', async () => {
const place = buildPlace({ trip_id: 1 });
await offlineDb.places.put(place);
server.use(
http.delete(`/api/trips/1/places/${place.id}`, () => HttpResponse.json({ success: true })),
);
await placeRepo.delete(1, place.id);
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(place.id);
expect(cached).toBeUndefined();
});
});