Adding more tests.

This commit is contained in:
Eric van der Vlist 2022-11-08 18:57:37 +01:00
parent a53c0210ea
commit 9cc4c404b0
6 changed files with 56 additions and 4 deletions

View File

@ -95,7 +95,7 @@ export const setCenterAtom = atom(null, (get, set, center: geoPoint) => {
// initDb(db); // initDb(db);
dispatch({ action: 'initDb' }); dispatch({ action: 'initDb' });
dispatch({ action: 'putNewGpx' }); dispatch({ action: 'putNewTrk' });
/** /**
* *

47
src/db/lib.test.ts Normal file
View File

@ -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,
}
`);
});
});

View File

@ -30,3 +30,7 @@ export const put = async (
} }
} }
}; };
export const getFamily = async (key: string) => {
return await db.allDocs({ startkey: key, endkey: key + '\ufff0' });
};

View File

@ -17,7 +17,7 @@ describe('The gpx module', () => {
globalThis.db = originalDb; globalThis.db = originalDb;
globalThis.Date.now = originalDateNow; 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' }); putNewTrk({ gpx: 'gpxId', trk: 'trkid' });
await expect(globalThis.db.put).toBeCalledWith({ await expect(globalThis.db.put).toBeCalledWith({
_id: 'gpx/gpxId/2trk/trkid', _id: 'gpx/gpxId/2trk/trkid',

View File

@ -21,7 +21,7 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => {
finalId = { ...gpxId, trk: '00000' }; finalId = { ...gpxId, trk: '00000' };
} }
const uri = getUri('trk', finalId); const uri = getUri('trk', finalId);
put( await put(
uri, uri,
'trk', 'trk',
(trk) => { (trk) => {

View File

@ -1,9 +1,10 @@
import { initDb } from '../db'; import { initDb } from '../db';
import { putNewGpx } from '../db/gpx'; import { putNewGpx } from '../db/gpx';
import { putNewTrk } from '../db/trk';
const self = globalThis as unknown as SharedWorkerGlobalScope; const self = globalThis as unknown as SharedWorkerGlobalScope;
const actions = { initDb, putNewGpx }; const actions = { initDb, putNewGpx, putNewTrk };
self.onconnect = function (e) { self.onconnect = function (e) {
var port = e.ports[0]; var port = e.ports[0];