diff --git a/src/db/gpx.test.ts b/src/db/gpx.test.ts index b694f2a..e99afc7 100644 --- a/src/db/gpx.test.ts +++ b/src/db/gpx.test.ts @@ -16,10 +16,11 @@ describe('The gpx module', () => { globalThis.db = originalDb; globalThis.Date.now = originalDateNow; }); - test('db.put() a new Gpx when required', () => { - putNewGpx({ gpx: 'whatever' }); + test('db.put() a new Gpx when required', async () => { + await putNewGpx({ gpx: 'whatever' }); expect(globalThis.db.put).toBeCalledWith({ _id: 'gpx/whatever', + _rev: undefined, doc: { $: { creator: 'dyomedea version 0.000002', @@ -35,27 +36,27 @@ describe('The gpx module', () => { 'xsi:schemaLocation': 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd', }, - extensions: '', + extensions: undefined, metadata: { - author: '', - bounds: '', - copyright: '', - desc: '', - extensions: '', - keywords: '', - link: [], - name: '', + author: undefined, + bounds: undefined, + copyright: undefined, + desc: undefined, + extensions: undefined, + keywords: undefined, + link: undefined, + name: undefined, time: '1970-01-01T00:00:00.000Z', }, - rte: [], - trk: [], - wpt: [], + rte: undefined, + trk: undefined, + wpt: undefined, }, type: 'gpx', }); }); - test('db.put() generates an id if needed', () => { - const id = putNewGpx(); + test('db.put() generates an id if needed', async () => { + const id = await putNewGpx(); expect(id).toEqual({ gpx: '0' }); }); }); diff --git a/src/db/gpx.ts b/src/db/gpx.ts index f39b977..4b1af95 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -15,29 +15,29 @@ const emptyGpx: Gpx = { 'xmlns:dyo': 'http://xmlns.dyomedea.com/', }, metadata: { - name: '', - desc: '', - author: '', - copyright: '', - link: [], - time: '', - keywords: '', - bounds: '', - extensions: '', + name: undefined, + desc: undefined, + author: undefined, + copyright: undefined, + link: undefined, + time: undefined, + keywords: undefined, + bounds: undefined, + extensions: undefined, }, - wpt: [], - rte: [], - trk: [], - extensions: '', + wpt: undefined, + rte: undefined, + trk: undefined, + extensions: undefined, }; -export const putNewGpx = (id: IdGpx = { gpx: Date.now().toString() }) => { +export const putNewGpx = async (id: IdGpx = { gpx: Date.now().toString() }) => { const uri = getUri('gpx', id); - put( + await put( uri, 'gpx', (gpx) => { - gpx.metadata!.time = new Date(Date.now()).toISOString(); + (gpx.metadata ??= {}).time = new Date(Date.now()).toISOString(); return gpx; }, emptyGpx diff --git a/src/db/trk.test.ts b/src/db/trk.test.ts new file mode 100644 index 0000000..a23bfbf --- /dev/null +++ b/src/db/trk.test.ts @@ -0,0 +1,47 @@ +import { putNewTrk } from './trk'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The gpx module', () => { + beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + test('db.put() a new Gpx when required', async () => { + putNewTrk({ gpx: 'gpxId', trk: 'trkid' }); + await expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/gpxId/2trk/trkid', + _rev: undefined, + doc: { + cmt: undefined, + desc: undefined, + extensions: undefined, + link: undefined, + name: undefined, + number: 0, + src: undefined, + trkseg: undefined, + type: undefined, + }, + type: 'trk', + }); + }); + test('db.put() generates an id for the trk if needed', async () => { + const id = await putNewTrk({ gpx: 'gpxId' }); + expect(id).toEqual({ gpx: 'gpxId', trk: '00000' }); + }); + test('db.put() generates ids for both gpx and trk if needed', async () => { + const id = await putNewTrk(); + expect(id).toEqual({ gpx: '0', trk: '00000' }); + }); +}); diff --git a/src/db/trk.ts b/src/db/trk.ts index c266a97..b1b87fe 100644 --- a/src/db/trk.ts +++ b/src/db/trk.ts @@ -1,15 +1,33 @@ import getUri from '../lib/ids'; +import { putNewGpx } from './gpx'; import { put } from './lib'; const emptyTrk: Trk = { - name: '', - cmt: '', - desc: '', - src: '', - link: [], + name: undefined, + cmt: undefined, + desc: undefined, + src: undefined, + link: undefined, number: 0, - type: '', - extensions: {}, - trkseg: [], + type: undefined, + extensions: undefined, + trkseg: undefined, }; +export const putNewTrk = async (id?: IdTrk | IdGpx) => { + let finalId = { ...id }; + if (!('trk' in finalId)) { + const gpxId = await putNewGpx(id); + finalId = { ...gpxId, trk: '00000' }; + } + const uri = getUri('trk', finalId); + put( + uri, + 'trk', + (trk) => { + return trk; + }, + emptyTrk + ); + return finalId; +};