import { before } from 'mocha'; import * as db from '../lib/db.js'; import * as assert from 'assert/strict'; const rng = Math.random(); describe('lib/db', function () { let myDb; let dbFuncs; describe('#init()', function () { it('should fail to connect given invalid connection string', function () { return db.init('not a connection string').then(() => Promise.reject()).catch(() => Promise.resolve()); }); it('should successfully connect to mongodb', function () { return myDb = db.init(process.env.MONGO_TEST_CONN_STR); }); }); describe('#close()', function () { it('should close the connection', function () { return myDb.then(db => db.close()); }); }); describe('with a established connection ->', function () { before(function () { myDb = db.init(process.env.MONGO_TEST_CONN_STR) .then(funcs => dbFuncs = funcs); }); after(function () { dbFuncs.purge(['readWriteTest']) .then(() => myDb.then(db => db.close())); }); describe('#persist()', function () { it('should be able to write to mongodb', function () { return dbFuncs.persist('readWriteTest', {}, 'rng', rng); }); }); describe('#subscribe()', function () { it('should be able to read from mongodb', function () { return dbFuncs .subscribe('readWriteTest', {}) .then(v => { assert.equal(v.length, 1); assert.equal(v[0].rng.transientValue, rng); }); }); }); }); });