Files
TREK/client/tests/unit/repo/placeRepo.test.ts
T
jubnl f8fdb14627 fix: remove navigator.onLine guards and fix upsert races in all repos
navigator.onLine returns false transiently during service worker activation
(skipWaiting + clientsClaim), causing all repo refresh IIFEs to return null
immediately on first page load — leaving the UI with empty data until F5.

Fixes applied across all list repos (trip, day, place, packing, todo, budget,
reservation, accommodation, file):
- Drop navigator.onLine guard; let fetch fail naturally when truly offline
- Await all upsert calls (some were fire-and-forget, risking race conditions
  against subsequent reads and silent swallowed failures)
- Return Promise.resolve(null) instead of Promise.resolve(fresh) in the
  IDB-empty network path, so loadTrip's background refresh Promise.all
  resolves null and skips set({trip}), preventing a spurious reference change
  that was resetting the 1500ms splash timer

Tests updated: placeRepo and packingRepo "empty cache" tests now simulate
genuine network failure (HttpResponse.error) instead of relying on the
navigator.onLine guard that no longer exists; DashboardPage tests clear IDB
before each test and use a query-safe assertion after background refresh.
2026-05-05 18:04:15 +02:00

119 lines
3.8 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 and network fails', async () => {
server.use(
http.get('/api/trips/99/places', () => HttpResponse.error()),
);
const result = await placeRepo.list(99);
expect(result.places).toHaveLength(0);
});
});
describe('placeRepo.create', () => {
it('writes place optimistically to Dexie immediately', async () => {
const result = await placeRepo.create(1, { name: 'Eiffel Tower' });
expect(result.place.name).toBe('Eiffel Tower');
// tempId is negative (-(Date.now()))
expect(result.place.id).toBeLessThan(0);
const cached = await offlineDb.places.where('trip_id').equals(1).toArray();
expect(cached).toHaveLength(1);
expect(cached[0].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();
});
});