From 1b9c7fc261fa0210fdc11652e7b3acfcef61326a Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 8 Nov 2022 21:51:26 +0100 Subject: [PATCH] Waypoints --- src/db/lib.test.ts | 28 ++++++++++++++++++++++ src/db/types.d.ts | 2 +- src/db/wpt.test.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++ src/db/wpt.ts | 44 +++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/db/wpt.test.ts create mode 100644 src/db/wpt.ts diff --git a/src/db/lib.test.ts b/src/db/lib.test.ts index 230dc96..b5effa3 100644 --- a/src/db/lib.test.ts +++ b/src/db/lib.test.ts @@ -4,6 +4,7 @@ import { getFamily } from './lib'; import { putNewTrk } from './trk'; import { putNewTrkpt } from './trkpt'; import { putNewTrkseg } from './trkseg'; +import { putNewWpt } from './wpt'; declare global { var db: any; @@ -175,6 +176,33 @@ Object { ], "total_rows": 7, } +`); + }); + + test('returns two rows after a gpx and a waypoint have been inserted.', async () => { + await putNewWpt(); + 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/0wpt/00000", + "key": "gpx/0/0wpt/00000", + "value": Object { + "rev": "1-dcdc55abd413b401dba4278510d9c51b", + }, + }, + ], + "total_rows": 5, +} `); }); }); diff --git a/src/db/types.d.ts b/src/db/types.d.ts index 6d5cea3..44f1968 100644 --- a/src/db/types.d.ts +++ b/src/db/types.d.ts @@ -86,7 +86,7 @@ interface Wpt { pdop?: number; ageofdgpsdata?: number; dgpsid?: number; - extensions: Extensions; + extensions?: Extensions; } interface Trkpt_ { diff --git a/src/db/wpt.test.ts b/src/db/wpt.test.ts new file mode 100644 index 0000000..6b8dc0a --- /dev/null +++ b/src/db/wpt.test.ts @@ -0,0 +1,58 @@ +import { putNewWpt } from './wpt'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The wpt module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new wpt when required', async () => { + putNewWpt({ gpx: 'gpxId', wpt: 'wptid' }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/0wpt/wptid', + _rev: undefined, + doc: { + $: { lat: 0, lon: 0 }, + ageofdgpsdata: undefined, + cmt: undefined, + desc: undefined, + dgpsid: undefined, + ele: undefined, + extensions: undefined, + fix: undefined, + geoidheight: undefined, + hdop: undefined, + link: undefined, + magvar: undefined, + name: undefined, + pdop: undefined, + sat: undefined, + src: undefined, + sym: undefined, + time: undefined, + type: undefined, + vdop: undefined, + }, + type: 'wpt', + }); + }); + test('db.put() generates an id for the wpt if needed', async () => { + const id = await putNewWpt({ gpx: 'gpxId' }); + expect(id).toEqual({ gpx: 'gpxId', wpt: '00000' }); + }); + test('db.put() generates ids for both gpx and trk if needed', async () => { + const id = await putNewWpt(); + expect(id).toEqual({ gpx: '0', wpt: '00000' }); + }); +}); diff --git a/src/db/wpt.ts b/src/db/wpt.ts new file mode 100644 index 0000000..3a69205 --- /dev/null +++ b/src/db/wpt.ts @@ -0,0 +1,44 @@ +import getUri from '../lib/ids'; +import { putNewGpx } from './gpx'; +import { put } from './lib'; + +export const emptyWpt: Wpt = { + $: { lat: 0, lon: 0 }, + ele: undefined, + time: undefined, + magvar: undefined, + geoidheight: undefined, + name: undefined, + cmt: undefined, + desc: undefined, + src: undefined, + link: undefined, + sym: undefined, + type: undefined, + fix: undefined, + sat: undefined, + hdop: undefined, + vdop: undefined, + pdop: undefined, + ageofdgpsdata: undefined, + dgpsid: undefined, + extensions: undefined, +}; + +export const putNewWpt = async (id?: IdGpx | IdWpt) => { + let finalId = { ...id }; + if (!('wpt' in finalId)) { + const gpxId = await putNewGpx(id); + finalId = { ...gpxId, wpt: '00000' }; + } + const uri = getUri('wpt', finalId); + await put( + uri, + 'wpt', + (wpt) => { + return wpt; + }, + emptyWpt + ); + return finalId as IdWpt; +};