Cleanup and starting to implements tracks.

This commit is contained in:
Eric van der Vlist 2022-11-07 22:12:50 +01:00
parent 36a3199b36
commit 26486b65b9
9 changed files with 225 additions and 26 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', params: 'gpxid' }); dispatch({ action: 'putNewGpx' });
/** /**
* *

View File

@ -7,19 +7,17 @@ declare global {
const originalDb = globalThis.db; const originalDb = globalThis.db;
const originalDateNow = globalThis.Date.now; const originalDateNow = globalThis.Date.now;
describe('The gpx module', () => {
beforeEach(() => { beforeEach(() => {
globalThis.db = { put: jest.fn() }; globalThis.db = { put: jest.fn() };
globalThis.Date.now = () => 0; globalThis.Date.now = () => 0;
}); });
afterEach(() => { afterEach(() => {
globalThis.db = originalDb; globalThis.db = originalDb;
globalThis.Date.now = originalDateNow; globalThis.Date.now = originalDateNow;
}); });
describe('The gpx module', () => {
test('db.put() a new Gpx when required', () => { test('db.put() a new Gpx when required', () => {
putNewGpx('whatever'); putNewGpx({ gpx: 'whatever' });
expect(globalThis.db.put).toBeCalledWith({ expect(globalThis.db.put).toBeCalledWith({
_id: 'gpx/whatever', _id: 'gpx/whatever',
doc: { doc: {
@ -45,7 +43,7 @@ describe('The gpx module', () => {
desc: '', desc: '',
extensions: '', extensions: '',
keywords: '', keywords: '',
link: '', link: [],
name: '', name: '',
time: '1970-01-01T00:00:00.000Z', time: '1970-01-01T00:00:00.000Z',
}, },
@ -56,4 +54,8 @@ describe('The gpx module', () => {
type: 'gpx', type: 'gpx',
}); });
}); });
test('db.put() generates an id if needed', () => {
const id = putNewGpx();
expect(id).toEqual({ gpx: '0' });
});
}); });

View File

@ -19,7 +19,7 @@ const emptyGpx: Gpx = {
desc: '', desc: '',
author: '', author: '',
copyright: '', copyright: '',
link: '', link: [],
time: '', time: '',
keywords: '', keywords: '',
bounds: '', bounds: '',
@ -31,8 +31,8 @@ const emptyGpx: Gpx = {
extensions: '', extensions: '',
}; };
export const putNewGpx = (id: string) => { export const putNewGpx = (id: IdGpx = { gpx: Date.now().toString() }) => {
const uri = getUri('gpx', { gpx: id }); const uri = getUri('gpx', id);
put( put(
uri, uri,
'gpx', 'gpx',
@ -42,4 +42,5 @@ export const putNewGpx = (id: string) => {
}, },
emptyGpx emptyGpx
); );
return id;
}; };

15
src/db/trk.ts Normal file
View File

@ -0,0 +1,15 @@
import getUri from '../lib/ids';
import { put } from './lib';
const emptyTrk: Trk = {
name: '',
cmt: '',
desc: '',
src: '',
link: [],
number: 0,
type: '',
extensions: {},
trkseg: [],
};

26
src/db/types.d.ts vendored
View File

@ -3,7 +3,7 @@ interface Gpx {
metadata?: Metadata; metadata?: Metadata;
wpt?: any[]; wpt?: any[];
rte?: any[]; rte?: any[];
trk?: any[]; trk?: Trk[];
extensions?: Extensions; extensions?: Extensions;
} }
@ -24,7 +24,7 @@ interface Metadata {
desc?: string; desc?: string;
author?: string; author?: string;
copyright?: string; copyright?: string;
link?: string; link?: Link[];
time?: string; time?: string;
keywords?: string; keywords?: string;
bounds?: string; bounds?: string;
@ -32,3 +32,25 @@ interface Metadata {
} }
interface Extensions {} interface Extensions {}
interface Trk {
name?: string;
cmt?: string;
desc?: string;
src?: string;
link?: Link[];
number?: number;
type?: string;
extensions?: Extensions;
trkseg?: any[];
}
interface Link {
$: Link_;
text?: string;
type?: string;
}
interface Link_ {
href: string;
}

116
src/lib/ids.test.ts Normal file
View File

@ -0,0 +1,116 @@
import { route } from 'docuri';
import uri from './ids';
describe('Checking some DocURI features', () => {
test(', basic route', () => {
const gpx = route('gpx/:id');
expect(gpx({ id: 10 })).toBe('gpx/10');
});
test(', basic route (vice-versa', () => {
const gpx = route('gpx/:id');
expect(gpx('gpx/10')).toMatchObject({ id: '10' });
});
});
describe('Checking a multilevel route', () => {
test(', using the two levels', () => {
const gpx = route('gpx/:gpx/2trk/:trk');
expect(gpx({ gpx: 10, trk: 0 })).toBe('gpx/10/2trk/0');
});
test(', using the two levels (vive-versa)', () => {
const gpx = route('gpx/:gpx/2trk/:trk');
expect(gpx('gpx/10/2trk/0')).toMatchObject({ gpx: '10', trk: '0' });
});
});
describe('Checking a multilevel route with optional part', () => {
test(', using the two levels', () => {
const gpx = route('gpx/:gpx(/2trk/:trk)');
expect(gpx({ gpx: 10, trk: 0 })).toBe('gpx/10/2trk/0');
});
test(', using the two levels (vive-versa)', () => {
const gpx = route('gpx/:gpx(/2trk/:trk)');
expect(gpx('gpx/10/2trk/0')).toMatchObject({ gpx: '10', trk: '0' });
});
test(', using only one level', () => {
const gpx = route('gpx/:gpx(/2trk/:trk)');
expect(gpx({ gpx: 10 })).toBe('gpx/10/2trk/'); //Unfortunately !
});
test(', using only one level (vive-versa)', () => {
const gpx = route('gpx/:gpx(/2trk/:trk)');
expect(gpx('gpx/10')).toMatchObject({ gpx: '10' });
});
});
describe('Checking gpx ids', () => {
test(', vice', () => {
const gpx = uri('gpx', { gpx: 'id' });
expect(gpx).toBe('gpx/id');
});
test(', and versa', () => {
const gpx = uri('gpx', 'gpx/id');
expect(gpx).toMatchObject({ gpx: 'id' });
});
});
describe('Checking trk ids', () => {
test(', vice', () => {
const rte = uri('trk', { gpx: 'gpxid', trk: 'trkid' });
expect(rte).toBe('gpx/gpxid/2trk/trkid');
});
test(', and versa', () => {
const rte = uri('trk', 'gpx/gpxid/2trk/trkid');
expect(rte).toMatchObject({ gpx: 'gpxid', trk: 'trkid' });
});
});
describe('Checking trkseg ids', () => {
test(', vice', () => {
const rte = uri('trkseg', {
gpx: 'gpxid',
trk: 'trkid',
trkseg: 'trksegid',
});
expect(rte).toBe('gpx/gpxid/2trk/trkid/trksegid');
});
test(', and versa', () => {
const rte = uri('trkseg', 'gpx/gpxid/2trk/trkid/trksegid');
expect(rte).toMatchObject({
gpx: 'gpxid',
trk: 'trkid',
trkseg: 'trksegid',
});
});
});
describe('Checking trkpt ids', () => {
test(', vice', () => {
const rte = uri('trkpt', {
gpx: 'gpxid',
trk: 'trkid',
trkseg: 'trksegid',
trkpt: 'trkptid',
});
expect(rte).toBe('gpx/gpxid/2trk/trkid/trksegid/trkptid');
});
test(', and versa', () => {
const rte = uri('trkpt', 'gpx/gpxid/2trk/trkid/trksegid/trkptid');
expect(rte).toMatchObject({
gpx: 'gpxid',
trk: 'trkid',
trkseg: 'trksegid',
trkpt: 'trkptid',
});
});
});
describe('Checking settings id', () => {
test(', vice', () => {
const rte = uri('settings', {});
expect(rte).toBe('settings');
});
test(', and versa', () => {
const rte = uri('settings', 'settings');
expect(rte).toMatchObject({});
});
});

View File

@ -4,12 +4,12 @@ const routes = {
dbdef: route('dbdef'), dbdef: route('dbdef'),
settings: route('settings'), settings: route('settings'),
gpx: route('gpx/:gpx'), gpx: route('gpx/:gpx'),
trk: route('gpx/:gpx/trk/:trk'), wpt: route('gpx/:gpx/0wpt/:wpt'),
trkseg: route('gpx/:gpx/trk/:trk/:trkseg'), rte: route('gpx/:gpx/1rte/:rte'),
trkpt: route('gpx/:gpx/trk/:trk/:trkseg/:trkpt'), rtept: route('gpx/:gpx/1rte/:rte/:rtept'),
wpt: route('gpx/:gpx/wpt/:wpt'), trk: route('gpx/:gpx/2trk/:trk'),
rte: route('gpx/:gpx/rte/:rte'), trkseg: route('gpx/:gpx/2trk/:trk/:trkseg'),
rtept: route('gpx/:gpx/rte/:rte/:rtept'), trkpt: route('gpx/:gpx/2trk/:trk/:trkseg/:trkpt'),
}; };
type RouteKey = keyof typeof routes; type RouteKey = keyof typeof routes;

34
src/lib/types.d.ts vendored Normal file
View File

@ -0,0 +1,34 @@
interface IdGpx {
gpx: string;
}
interface IdTrk {
gpx: string;
trk: string;
}
interface IdTrkseg {
gpx: string;
trk: string;
trkseg: string;
}
interface IdTrkpt {
gpx: string;
trk: string;
trkseg: string;
trkpt: string;
}
interface IdWpt {
gpx: string;
wpt: string;
}
interface IdRte {
gpx: string;
rte: string;
}
interface IdRtept {
gpx: string;
rte: string;
rtept: string;
}

View File

@ -1,4 +1,4 @@
import dispatch, { init, worker } from './dispatcher-main' import dispatch, { init, worker } from './dispatcher-main';
jest.mock('./get-worker', () => ({ jest.mock('./get-worker', () => ({
getWorker: () => ({ getWorker: () => ({
@ -21,8 +21,17 @@ describe('The dispatcher-main', () => {
test('should return a promise if no callback is provided', () => { test('should return a promise if no callback is provided', () => {
expect(dispatch('ping')).toBeInstanceOf(Promise); expect(dispatch('ping')).toBeInstanceOf(Promise);
}); });
test('should send back the message', () => { test('should forward the message', () => {
dispatch('ping'); dispatch('ping');
expect(worker.port.postMessage).toBeCalledWith({ id: 0, payload: 'ping' }); expect(worker.port.postMessage).toBeCalledWith({ id: 0, payload: 'ping' });
}); });
test('should return the response', () => {
var response;
const callback = (error, success) => {
response = success;
};
dispatch('ping', callback);
worker.port.onmessage({ data: { id: 0, payload: 'pong' } });
expect(response).toEqual('pong');
});
}); });