From e8a3d8c62e98033c8f9166c463946865851bbe3a Mon Sep 17 00:00:00 2001 From: evlist Date: Thu, 9 Feb 2023 14:25:13 +0100 Subject: [PATCH] Re implementing a basic test for db/gpx.ts --- .gitignore | 3 +- src/db/gpx.test.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 89791aa..79f7004 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules/ dist/ android/ ios/ -vite.config.ts.timestamp-*.mjs \ No newline at end of file +vite.config.ts.timestamp-*.mjs +$*$/ \ No newline at end of file diff --git a/src/db/gpx.test.ts b/src/db/gpx.test.ts index 9e5ebc8..3acdde5 100644 --- a/src/db/gpx.test.ts +++ b/src/db/gpx.test.ts @@ -1,5 +1,88 @@ -import { describe, it } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { initDb } from '.'; +import { existsGpx, putNewGpx } from './gpx'; + +const test = it; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; describe('putNewGpx', () => { - it("doesn't return an error", () => {}); + beforeEach(() => { + globalThis.db = { put: vi.fn() }; + globalThis.Date.now = () => 0; + }); + afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; + }); + + it('db.put() a new Gpx when required', async () => { + await putNewGpx({ gpx: 0 }); + expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/0000000000000000', + _rev: undefined, + doc: { + $: { + creator: 'dyomedea version 0.000002', + version: '1.1', + xmlns: 'http://www.topografix.com/GPX/1/1', + 'xmlns:dyo': 'http://xmlns.dyomedea.com/', + 'xmlns:gpxtpx': + 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1', + 'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3', + 'xmlns:wptx1': + 'http://www.garmin.com/xmlschemas/WaypointExtension/v1', + 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', + '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: undefined, + metadata: { + author: undefined, + bounds: undefined, + copyright: undefined, + desc: undefined, + extensions: undefined, + keywords: undefined, + link: undefined, + name: undefined, + time: '1970-01-01T00:00:00.000Z', + }, + rte: undefined, + trk: undefined, + wpt: undefined, + }, + type: 'gpx', + }); + }); +}); + +describe('The gpx module with a real db', () => { + beforeEach(async () => { + await initDb({}); + globalThis.Date.now = () => 0; + }); + afterEach(async () => { + await db.destroy(); + db = undefined; + globalThis.Date.now = originalDateNow; + }); + + test("existsGpx returns false if the GPX doesn't exist", async () => { + const exists = await existsGpx({ gpx: 1 }); + expect(exists).toBeFalsy(); + }); + test('existsGpx returns false if the GPX exists', async () => { + const id = { gpx: 1 }; + await putNewGpx(id); + const exists = await existsGpx(id); + expect(exists).toBeTruthy(); + }); });