102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import { initDb } from '.';
|
|
import PouchDB from 'pouchdb';
|
|
import { appendTrkpt, deleteCurrent, saveCurrent } from './gpx';
|
|
|
|
import PouchDBFind from 'pouchdb-find';
|
|
import { mkdtempSync, rmdirSync } from 'fs';
|
|
import { join } from 'path';
|
|
import { tmpdir } from 'os';
|
|
PouchDB.plugin(PouchDBFind);
|
|
//PouchDB.plugin(require('pouchdb-find'));
|
|
|
|
// export class PouchService {
|
|
// constructor() {
|
|
// PouchDB.plugin(PouchDBFind);
|
|
// }
|
|
// }
|
|
|
|
// jest.useFakeTimers();
|
|
|
|
const tmpDir = mkdtempSync(join(tmpdir(), 'dyomedea-testDB'));
|
|
|
|
var db: PouchDB.Database;
|
|
|
|
beforeEach(async () => {
|
|
db = new PouchDB(tmpDir);
|
|
await initDb(db, () => {});
|
|
});
|
|
|
|
afterEach(async () => {
|
|
// await db.close();
|
|
await db.destroy();
|
|
});
|
|
|
|
describe('Checking that trkpts are beeing inserted', () => {
|
|
test('and that we have two documents after the first call', async () => {
|
|
await appendTrkpt(db, {});
|
|
const results = await db.find({ selector: {} });
|
|
const docs = results.docs;
|
|
// console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(2);
|
|
});
|
|
test('and that we have three documents after the second call', async () => {
|
|
await appendTrkpt(db, {});
|
|
await appendTrkpt(db, {});
|
|
const results = await db.find({ selector: {} });
|
|
const docs = results.docs;
|
|
console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('Checking that saveCurrent() is working as expected', () => {
|
|
test(', that we still have two documents after saving.', async () => {
|
|
await appendTrkpt(db, {});
|
|
await saveCurrent(db);
|
|
const results = await db.find({
|
|
selector: {},
|
|
});
|
|
const docs = results.docs;
|
|
// console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(2);
|
|
});
|
|
test("and that we don't have current tracks after saving.", async () => {
|
|
await appendTrkpt(db, {});
|
|
await saveCurrent(db);
|
|
const results = await db.find({
|
|
selector: {
|
|
type: 'gpx',
|
|
subtype: 'current',
|
|
},
|
|
});
|
|
const docs = results.docs;
|
|
// console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('Checking that deleteCurrent() is working as expected', () => {
|
|
test(', that we have no more documents after deleting.', async () => {
|
|
await appendTrkpt(db, { time: '' });
|
|
await deleteCurrent(db);
|
|
const results = await db.find({
|
|
selector: {},
|
|
});
|
|
const docs = results.docs;
|
|
// console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(0);
|
|
});
|
|
test(', that we have two documents from a saved track after deleting the current one.', async () => {
|
|
await appendTrkpt(db, { time: '' });
|
|
await saveCurrent(db);
|
|
await appendTrkpt(db, { time: '' });
|
|
await deleteCurrent(db);
|
|
const results = await db.find({
|
|
selector: {},
|
|
});
|
|
const docs = results.docs;
|
|
// console.log(`docs: ${JSON.stringify(docs)}`);
|
|
expect(docs.length).toBe(2);
|
|
});
|
|
});
|