initial commit
[squeep-test-helper] / lib / stub-database.js
diff --git a/lib/stub-database.js b/lib/stub-database.js
new file mode 100644 (file)
index 0000000..41abaf7
--- /dev/null
@@ -0,0 +1,55 @@
+/* eslint-disable class-methods-use-this */
+'use strict';
+
+const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
+
+class StubDatabase {
+  constructor() {
+    this._implementation.forEach((fn) => {
+      if (!(fn in this)) {
+        this[fn] = async () => { /* */ }; // eslint-disable-line security/detect-object-injection
+      }
+    });
+  }
+
+  get _implementation() {
+    return [
+      ...this._spyFns,
+      ...this._stubFns,
+    ];
+  }
+
+  get _spyFns() {
+    return [
+      'context',
+      'transaction',
+    ];
+  }
+
+  get _stubFns() {
+    return [
+      'initialize',
+      'healthCheck',
+    ];
+  }
+
+  _reset() {
+    this._spyFns.forEach((fn) => {
+      sinon.spy(this, fn);
+    });
+    this._stubFns.forEach((fn) => {
+      sinon.stub(this, fn);
+    });
+  }
+
+  async context(fn) {
+    await fn({});
+  }
+
+  async transaction(dbCtx, fn) {
+    await fn(dbCtx);
+  }
+
+}
+
+module.exports = StubDatabase;