mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 14:21:46 +00:00
69620e7276
All create/update/delete repo methods now write to IndexedDB optimistically and fire mutationQueue.flush() as fire-and-forget, returning immediately without waiting for the network. This eliminates the 8-second UX freeze previously seen when the API was unreachable but navigator.onLine was true. - Repos rewritten: trip, day, place, packing, todo, budget, accommodation, reservation, file — write methods never throw, always return optimistic data - mutationQueue.flush() changed to iterative (one item per loop iteration) so mutations enqueued mid-flush (e.g. bulk check-all) are picked up - fileRepo.toggleStar skips the IDB put when the file is not cached locally - DayDetailPanel passes place_name into accommodationRepo.create so the optimistic accommodation renders the correct hotel label immediately - Test suite updated throughout to reflect optimistic-first semantics: no more rollback assertions, IDB cleared in component test beforeEach hooks, FileManager tests switched from filesApi spy to MSW endpoint assertions
117 lines
3.8 KiB
TypeScript
117 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', async () => {
|
|
Object.defineProperty(navigator, 'onLine', { value: false });
|
|
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();
|
|
});
|
|
});
|