Testing DocURI (#4 and #15)

This commit is contained in:
Eric van der Vlist 2022-10-03 11:08:47 +02:00
parent 0a11ccbdb4
commit 0d1cdf39de
5 changed files with 138 additions and 0 deletions

11
package-lock.json generated
View File

@ -38,6 +38,7 @@
"buffer": "^6.0.3",
"cordova-plugin-geolocation": "^4.1.0",
"crypto-browserify": "^3.12.0",
"docuri": "^4.2.2",
"font-gis": "^1.0.5",
"git": "^0.1.5",
"ionicons": "^6.0.3",
@ -7866,6 +7867,11 @@
"node": ">=6.0.0"
}
},
"node_modules/docuri": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/docuri/-/docuri-4.2.2.tgz",
"integrity": "sha512-eEtIB7SQAEVO1HC286DlnPnSiddAl+wFv+EeBBQ7VdTEMRplzG4qIhrNdyIecXfVVD9cIghikVVkls1O78EQqQ=="
},
"node_modules/dom-accessibility-api": {
"version": "0.5.14",
"resolved": "https://registry.npmjs.org/dom-accessibility-api/-/dom-accessibility-api-0.5.14.tgz",
@ -25419,6 +25425,11 @@
"esutils": "^2.0.2"
}
},
"docuri": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/docuri/-/docuri-4.2.2.tgz",
"integrity": "sha512-eEtIB7SQAEVO1HC286DlnPnSiddAl+wFv+EeBBQ7VdTEMRplzG4qIhrNdyIecXfVVD9cIghikVVkls1O78EQqQ=="
},
"dom-accessibility-api": {
"version": "0.5.14",
"resolved": "https://registry.npmjs.org/dom-accessibility-api/-/dom-accessibility-api-0.5.14.tgz",

View File

@ -33,6 +33,7 @@
"buffer": "^6.0.3",
"cordova-plugin-geolocation": "^4.1.0",
"crypto-browserify": "^3.12.0",
"docuri": "^4.2.2",
"font-gis": "^1.0.5",
"git": "^0.1.5",
"ionicons": "^6.0.3",

1
src/lib/docuri.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module 'docuri';

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

@ -0,0 +1,105 @@
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/trk/:trk');
expect(gpx({ gpx: 10, trk: 0 })).toBe('gpx/10/trk/0');
});
test(', using the two levels (vive-versa)', () => {
const gpx = route('gpx/:gpx/trk/:trk');
expect(gpx('gpx/10/trk/0')).toMatchObject({ gpx: '10', trk: '0' });
});
});
describe('Checking a multilevel route with optional part', () => {
test(', using the two levels', () => {
const gpx = route('gpx/:gpx(/trk/:trk)');
expect(gpx({ gpx: 10, trk: 0 })).toBe('gpx/10/trk/0');
});
test(', using the two levels (vive-versa)', () => {
const gpx = route('gpx/:gpx(/trk/:trk)');
expect(gpx('gpx/10/trk/0')).toMatchObject({ gpx: '10', trk: '0' });
});
test(', using only one level', () => {
const gpx = route('gpx/:gpx(/trk/:trk)');
expect(gpx({ gpx: 10 })).toBe('gpx/10/trk/'); //Unfortunately !
});
test(', using only one level (vive-versa)', () => {
const gpx = route('gpx/:gpx(/trk/: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/trk/trkid');
});
test(', and versa', () => {
const rte = uri('trk', 'gpx/gpxid/trk/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/trk/trkid/trksegid');
});
test(', and versa', () => {
const rte = uri('trkseg', 'gpx/gpxid/trk/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/trk/trkid/trksegid/trkptid');
});
test(', and versa', () => {
const rte = uri('trkpt', 'gpx/gpxid/trk/trkid/trksegid/trkptid');
expect(rte).toMatchObject({
gpx: 'gpxid',
trk: 'trkid',
trkseg: 'trksegid',
trkpt: 'trkptid',
});
});
});

20
src/lib/ids.ts Normal file
View File

@ -0,0 +1,20 @@
import { route } from 'docuri';
const routes = {
config: route('config'),
gpx: route('gpx/:gpx'),
trk: route('gpx/:gpx/trk/:trk'),
trkseg: route('gpx/:gpx/trk/:trk/:trkseg'),
trkpt: route('gpx/:gpx/trk/:trk/:trkseg/:trkpt'),
wpt: route('gpx/:gpx/wpt/:wpt'),
rte: route('gpx/:gpx/rte/:rte'),
rtept: route('gpx/:gpx/rte/:rte/:rtept'),
};
type RouteKey = keyof typeof routes;
const uri = (type: RouteKey, param: any) => {
return routes[type](param);
};
export default uri;