mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
bdd8c08dd4
The day plan wrote straight to the API, so offline a place dropped on a day snapped back with "Error assigning place", a renamed day or a new day note failed, and a moved stop, a leg's travel mode or who joins a visit were lost. Visits (put on a day, taken off, reordered, moved, travellers, leg mode), the day's name, notes text and travel mode, and the day notes (add, edit, delete, move) now go through assignmentRepo, dayRepo and dayNoteRepo and the mutation queue: the cached day changes at once, a visit or note made offline gets a temporary id that the edits after it wait for, and an online write held behind an older queued one joins the queue, since none of these carry a version token. A new order of a day that names a visit made offline is sent with its real id, or without it when its create failed. Desktop, the phone timeline and note sheet all write through the store, so both are covered. The replay reads each answer for what it names: the day answer keeps the cached notes and visits, the participants answer lands on its visit, and a visit the server refuses with 409 (a Tour already on that day) fails instead of waiting as a conflict no retry can solve. The trip's name, description, reminder and the archive flag wait in the queue too, from the planner, the phone and the dashboard. What only the server can do says so instead of failing: new dates, a day count, a currency or a cover, a new trip, a copy or a delete, members, guests, share and invite links, and a new order of the days, which moves the dates of the bookings. Refs #2601
168 lines
6.5 KiB
TypeScript
168 lines
6.5 KiB
TypeScript
// FE-REPO-DAYNOTE-OFF-001 to FE-REPO-DAYNOTE-OFF-005, FE-REPO-DAY-OFF-001 to FE-REPO-DAY-OFF-004
|
|
// Offline writes of a day and its notes (#2601).
|
|
import 'fake-indexeddb/auto';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { buildDay, buildDayNote } from '../../tests/helpers/factories';
|
|
import { server } from '../../tests/helpers/msw/server';
|
|
import type { QueuedMutation } from '../db/offlineDb';
|
|
import { clearAll, offlineDb } from '../db/offlineDb';
|
|
import { setAuthed } from '../sync/authGate';
|
|
import { mutationQueue } from '../sync/mutationQueue';
|
|
import { dayNoteRepo } from './dayNoteRepo';
|
|
import { dayRepo } from './dayRepo';
|
|
|
|
function setOnline(v: boolean): void {
|
|
Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true });
|
|
}
|
|
|
|
async function queue(): Promise<QueuedMutation[]> {
|
|
return (await offlineDb.mutationQueue.toArray()).sort((a, b) => a.createdAt - b.createdAt);
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await clearAll();
|
|
mutationQueue._resetFlushing();
|
|
setOnline(false);
|
|
await offlineDb.days.put(
|
|
buildDay({ id: 70, trip_id: 4, title: 'Old', notes_items: [buildDayNote({ id: 900, day_id: 70, text: 'Museum' })] })
|
|
);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
setAuthed(false);
|
|
setOnline(true);
|
|
});
|
|
|
|
describe('dayNoteRepo, offline', () => {
|
|
it('FE-REPO-DAYNOTE-OFF-001: a new note lands on the cached day under a temp id and a POST waits in the queue', async () => {
|
|
const { note } = await dayNoteRepo.create(4, 70, { text: 'Lunch', time: '12:00', color: null });
|
|
|
|
expect(note.id).toBeLessThan(0);
|
|
expect(note).toMatchObject({ day_id: 70, text: 'Lunch', time: '12:00' });
|
|
expect((await offlineDb.days.get(70))?.notes_items?.map((n) => n.text)).toEqual(['Museum', 'Lunch']);
|
|
const [mutation] = await queue();
|
|
expect(mutation).toMatchObject({
|
|
method: 'POST',
|
|
url: '/trips/4/days/70/notes',
|
|
resource: 'dayNotes',
|
|
tempId: note.id,
|
|
body: { text: 'Lunch', time: '12:00', color: null },
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-DAYNOTE-OFF-002: an edit and a delete of an unsynced note wait for its real id', async () => {
|
|
const { note } = await dayNoteRepo.create(4, 70, { text: 'Lunch' });
|
|
const { note: edited } = await dayNoteRepo.update(4, 70, note, { text: 'Brunch' });
|
|
expect(edited).toMatchObject({ id: note.id, text: 'Brunch' });
|
|
await dayNoteRepo.delete(4, 70, note.id);
|
|
|
|
expect((await offlineDb.days.get(70))?.notes_items?.map((n) => n.id)).toEqual([900]);
|
|
const [, edit, del] = await queue();
|
|
expect(edit).toMatchObject({
|
|
method: 'PUT',
|
|
url: '/trips/4/days/70/notes/{id}',
|
|
tempEntityId: note.id,
|
|
resource: 'dayNotes',
|
|
});
|
|
expect(del).toMatchObject({ method: 'DELETE', url: '/trips/4/days/70/notes/{id}', tempEntityId: note.id });
|
|
});
|
|
|
|
it('FE-REPO-DAYNOTE-OFF-003: an edit of a synced note shows the whole note and names it in the url', async () => {
|
|
const before = buildDayNote({ id: 900, day_id: 70, text: 'Museum', time: '10:00' });
|
|
const { note } = await dayNoteRepo.update(4, 70, before, { time: '11:00' });
|
|
|
|
expect(note).toMatchObject({ id: 900, text: 'Museum', time: '11:00' });
|
|
expect((await offlineDb.days.get(70))?.notes_items?.[0]).toMatchObject({ time: '11:00' });
|
|
expect((await queue())[0]).toMatchObject({ url: '/trips/4/days/70/notes/900', entityId: 900 });
|
|
});
|
|
|
|
it('FE-REPO-DAYNOTE-OFF-004: online, an edit joins the queue while an older write to the note waits', async () => {
|
|
await dayNoteRepo.delete(4, 70, 900);
|
|
setOnline(true);
|
|
vi.spyOn(mutationQueue, 'sendSoon').mockImplementation(() => {});
|
|
const put = vi.fn();
|
|
server.use(
|
|
http.put('/api/trips/4/days/70/notes/900', () => {
|
|
put();
|
|
return HttpResponse.json({});
|
|
})
|
|
);
|
|
|
|
await dayNoteRepo.update(4, 70, buildDayNote({ id: 900, day_id: 70 }), { text: 'x' });
|
|
|
|
expect(put).not.toHaveBeenCalled();
|
|
expect((await queue()).map((m) => m.method)).toEqual(['DELETE', 'PUT']);
|
|
});
|
|
|
|
it('FE-REPO-DAYNOTE-OFF-005: online, a new note goes to the server and onto the cached day', async () => {
|
|
setOnline(true);
|
|
server.use(
|
|
http.post('/api/trips/4/days/70/notes', () =>
|
|
HttpResponse.json({ note: buildDayNote({ id: 901, day_id: 70, text: 'Dinner' }) })
|
|
)
|
|
);
|
|
|
|
const { note } = await dayNoteRepo.create(4, 70, { text: 'Dinner' });
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
|
|
expect(note.id).toBe(901);
|
|
expect(await offlineDb.mutationQueue.count()).toBe(0);
|
|
expect((await offlineDb.days.get(70))?.notes_items?.map((n) => n.id)).toEqual([900, 901]);
|
|
});
|
|
});
|
|
|
|
describe('dayRepo, offline', () => {
|
|
it('FE-REPO-DAY-OFF-001: a new day title is cached at once and the PUT waits in the queue', async () => {
|
|
await dayRepo.update(4, 70, { title: 'Arrival' });
|
|
|
|
expect(await offlineDb.days.get(70)).toMatchObject({
|
|
title: 'Arrival',
|
|
notes_items: [expect.objectContaining({ id: 900 })],
|
|
});
|
|
expect((await queue())[0]).toMatchObject({
|
|
method: 'PUT',
|
|
url: '/trips/4/days/70',
|
|
body: { title: 'Arrival' },
|
|
resource: 'days',
|
|
entityId: 70,
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-DAY-OFF-002: the travel mode of a day waits in the queue on its own route', async () => {
|
|
await dayRepo.setTransport(4, 70, 'cycling');
|
|
|
|
expect((await offlineDb.days.get(70))?.default_transport_mode).toBe('cycling');
|
|
expect((await queue())[0]).toMatchObject({
|
|
url: '/trips/4/days/70/transport',
|
|
body: { transport_mode: 'cycling' },
|
|
resource: 'days',
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-DAY-OFF-003: a new order of the days and a new empty day need a connection', async () => {
|
|
await expect(dayRepo.reorder(4, [70])).rejects.toThrow('needs a connection');
|
|
await expect(dayRepo.insert(4, 1)).rejects.toThrow('needs a connection');
|
|
expect(await offlineDb.mutationQueue.count()).toBe(0);
|
|
});
|
|
|
|
it('FE-REPO-DAY-OFF-004: online with nothing queued, a title goes straight to the server', async () => {
|
|
setOnline(true);
|
|
const put = vi.fn();
|
|
server.use(
|
|
http.put('/api/trips/4/days/70', async ({ request }) => {
|
|
put(await request.json());
|
|
return HttpResponse.json({ day: buildDay({ id: 70, trip_id: 4, title: 'Arrival' }) });
|
|
})
|
|
);
|
|
|
|
await dayRepo.update(4, 70, { title: 'Arrival' });
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
|
|
expect(put).toHaveBeenCalledWith({ title: 'Arrival' });
|
|
expect(await offlineDb.mutationQueue.count()).toBe(0);
|
|
expect((await offlineDb.days.get(70))?.title).toBe('Arrival');
|
|
});
|
|
});
|