]> git.r.bdr.sh - rbdr/cologne/blobdiff - test/loggers/file.js
Update the code.
[rbdr/cologne] / test / loggers / file.js
diff --git a/test/loggers/file.js b/test/loggers/file.js
new file mode 100644 (file)
index 0000000..5e73d82
--- /dev/null
@@ -0,0 +1,106 @@
+'use strict';
+
+const Fs = require('fs');
+
+const Tap = require('tap');
+
+const FileLogger = require('../../lib/loggers/file');
+
+// Prepare the test
+
+const rawFile = './raw.log';
+const formatterFile = './formatter.log';
+const formatterString = 'example';
+
+const logObjectA = {
+  _timestamp: Date.now() + .134,
+  _cologneLog: '1.0.0',
+  _from: 'Dummy Logger',
+  _level: 6,
+  _levelString: 'info',
+  message: 'MessageOne'
+};
+
+const logObjectB = {
+  _timestamp: Date.now() + .134,
+  _cologneLog: '1.0.0',
+  _from: 'Dummy Logger',
+  _level: 6,
+  _levelString: 'info',
+  message: 'MessageTwo'
+};
+
+const logs = [logObjectA, logObjectB];
+
+
+const dummyFormatter = {
+  values: [],
+  format: function (logObject) {
+
+    this.values.push(logObject);
+    return formatterString;
+  }
+};
+
+const rawLogger = new FileLogger({
+  file: rawFile
+});
+const formatterLogger = new FileLogger({
+  file: formatterFile,
+  formatter: dummyFormatter
+});
+
+/**
+ * TEST: #log() - regular
+ */
+
+rawLogger.log(...logs);
+
+setTimeout(() => {
+
+  Tap.test('raw file', (t) => {
+
+    Fs.readFile(rawFile, { encoding: 'utf8' }, (_, contents) => {
+
+      const lines = contents.trim().split('\n');
+
+      Tap.equal(lines.length, logs.length,
+        'it should send all params to the file');
+
+      Tap.equal(JSON.stringify(logObjectA), lines[0],
+        'it should log the raw json object');
+
+      Fs.unlink(rawFile, () => {
+
+        t.end();
+      });
+    });
+  });
+}, 10); // allow for flush
+/**
+ * TEST: #log() - formatter
+ */
+
+formatterLogger.log(...logs);
+
+setTimeout(() => {
+
+  Tap.test('formatted file', (t) => {
+
+    Fs.readFile(formatterFile, { encoding: 'utf8' }, (_, contents) => {
+
+      const lines = contents.trim().split('\n');
+
+      Tap.equal(lines.length, logs.length,
+        'it should send all params to the file');
+
+      Tap.equal(formatterString, lines[0],
+        'it should log the formatted object');
+
+      Fs.unlink(formatterFile, () => {
+
+        t.end();
+      });
+    });
+  });
+}, 10);