From 9cc4c404b04031469abab78f097f7f154b4d4a89 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 8 Nov 2022 18:57:37 +0100 Subject: [PATCH] Adding more tests. --- src/App.tsx | 2 +- src/db/lib.test.ts | 47 ++++++++++++++++++++++++++++++++ src/db/lib.ts | 4 +++ src/db/trk.test.ts | 2 +- src/db/trk.ts | 2 +- src/workers/dispatcher-worker.ts | 3 +- 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/db/lib.test.ts diff --git a/src/App.tsx b/src/App.tsx index 83675b2..73751a3 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -95,7 +95,7 @@ export const setCenterAtom = atom(null, (get, set, center: geoPoint) => { // initDb(db); dispatch({ action: 'initDb' }); -dispatch({ action: 'putNewGpx' }); +dispatch({ action: 'putNewTrk' }); /** * diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts new file mode 100644 index 0000000..89392f6 --- /dev/null +++ b/src/db/lib.test.ts @@ -0,0 +1,47 @@ +import { initDb } from '.'; +import uri from '../lib/ids'; +import { getFamily } from './lib'; +import { putNewTrk } from './trk'; + +declare global { + var db: any; +} + +const originalDateNow = globalThis.Date.now; + +describe('getFamily', () => { + beforeEach(async () => { + await initDb({}); + globalThis.Date.now = () => 0; + }); + afterEach(async () => { + await db.destroy(); + globalThis.Date.now = originalDateNow; + }); + test('returns two rows after a gpx and a track have been inserted.', async () => { + await putNewTrk(); + const allDocs: any = await getFamily(uri('gpx', { gpx: '0' })); + expect(allDocs).toMatchInlineSnapshot(` +Object { + "offset": 0, + "rows": Array [ + Object { + "id": "gpx/0", + "key": "gpx/0", + "value": Object { + "rev": "1-98d42c99fcf471a500b0506a60e77559", + }, + }, + Object { + "id": "gpx/0/2trk/00000", + "key": "gpx/0/2trk/00000", + "value": Object { + "rev": "1-1e197ef8965a436730f80cabaaaa6f70", + }, + }, + ], + "total_rows": 5, +} +`); + }); +}); diff --git a/src/db/lib.ts b/src/db/lib.ts index 5dc1a27..e7f2279 100644 --- a/src/db/lib.ts +++ b/src/db/lib.ts @@ -30,3 +30,7 @@ export const put = async ( } } }; + +export const getFamily = async (key: string) => { + return await db.allDocs({ startkey: key, endkey: key + '\ufff0' }); +}; diff --git a/src/db/trk.test.ts b/src/db/trk.test.ts index a23bfbf..b7183c5 100644 --- a/src/db/trk.test.ts +++ b/src/db/trk.test.ts @@ -17,7 +17,7 @@ describe('The gpx module', () => { globalThis.db = originalDb; globalThis.Date.now = originalDateNow; }); - test('db.put() a new Gpx when required', async () => { + test('db.put() a new trk when required', async () => { putNewTrk({ gpx: 'gpxId', trk: 'trkid' }); await expect(globalThis.db.put).toBeCalledWith({ _id: 'gpx/gpxId/2trk/trkid', diff --git a/src/db/trk.ts b/src/db/trk.ts index b1b87fe..0997d6d 100644 --- a/src/db/trk.ts +++ b/src/db/trk.ts @@ -21,7 +21,7 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => { finalId = { ...gpxId, trk: '00000' }; } const uri = getUri('trk', finalId); - put( + await put( uri, 'trk', (trk) => { diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 7259d05..523129c 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -1,9 +1,10 @@ import { initDb } from '../db'; import { putNewGpx } from '../db/gpx'; +import { putNewTrk } from '../db/trk'; const self = globalThis as unknown as SharedWorkerGlobalScope; -const actions = { initDb, putNewGpx }; +const actions = { initDb, putNewGpx, putNewTrk }; self.onconnect = function (e) { var port = e.ports[0];