One more test (and bug fix).

This commit is contained in:
Eric van der Vlist 2023-02-09 21:11:17 +01:00
parent c30ceecbb1
commit 796147e4a9
2 changed files with 56 additions and 1 deletions

View File

@ -131,4 +131,59 @@ describe('The rtept module with a real db', () => {
expect(await getRte({ id: idRte })).toEqual(rte2);
expect((await get(idRtept)).doc).toEqual(rtept4);
});
it('can write a rtept outside a rte with a negative index', async () => {
const idRte = 'gpx/4320836410265485/2rte/000034';
const idRtept = 'gpx/4320836410265485/2rte/000034/000035';
const rtept3: Wpt = {
$: { lat: 3, lon: 3 },
};
const rtept4: Wpt = {
$: { lat: 4, lon: 4 },
};
const rte0: Rte = {
name: 'A new route',
rtept: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
],
};
const rte1: Rte = {
name: 'A new route',
rtept: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
rtept3,
],
};
const rte2: Rte = {
name: 'A new route',
rtept: [
{
$: { lat: 1, lon: 1 },
},
{
$: { lat: 2, lon: 2 },
},
rtept4,
],
};
const rtePut = await putRte({ id: idRte, rte: rte0 });
await put(idRtept, 'rtept', () => rtept3, emptyWpt);
await putRtept({
idRte,
index: -1,
rtept: rtept4,
});
expect(await getRte({ id: idRte })).toEqual(rte2);
expect((await get(idRtept)).doc).toEqual(rtept4);
});
});

View File

@ -32,7 +32,7 @@ export const putRtept = async (params: any) => {
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
const nbRteptOutRte = docs.rows.length - 1;
const nbRtept = nbRteptInRte + nbRteptOutRte;
const positiveIndex = index >= 0 ? index : nbRtept - index;
const positiveIndex = index >= 0 ? index : nbRtept + index;
// console.log({
// caller: 'putRtept',
// nbRteptInRte,