breaking: bring your own sinon instead of bundling, to resolve leaks when versions...
[squeep-test-helper] / lib / stub-database.js
1 /* eslint-disable class-methods-use-this */
2 'use strict';
3
4 class StubDatabase {
5 constructor(sinon) {
6 this._sinon = sinon;
7 this._implementation.forEach((fn) => {
8 if (!(fn in this)) {
9 this[fn] = async () => undefined; // eslint-disable-line security/detect-object-injection
10 }
11 });
12 }
13
14 get _implementation() {
15 return [
16 ...this._spyFns,
17 ...this._stubFns,
18 ];
19 }
20
21 get _spyFns() {
22 return [
23 'context',
24 'transaction',
25 ];
26 }
27
28 get _stubFns() {
29 return [
30 'initialize',
31 'healthCheck',
32 ];
33 }
34
35 _reset() {
36 this._spyFns.forEach((fn) => {
37 this._sinon.spy(this, fn);
38 });
39 this._stubFns.forEach((fn) => {
40 this._sinon.stub(this, fn);
41 });
42 }
43
44 async context(fn) {
45 await fn({});
46 }
47
48 async transaction(dbCtx, fn) {
49 await fn(dbCtx);
50 }
51
52 }
53
54 module.exports = StubDatabase;