initial commit
[squeep-html-template-helper] / test / lib / template-helper.js
diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js
new file mode 100644 (file)
index 0000000..29878cd
--- /dev/null
@@ -0,0 +1,249 @@
+/* eslint-env mocha */
+'use strict';
+
+const assert = require('assert');
+const th = require('../../lib/template-helper');
+const lint = require('html-minifier-lint').lint; // eslint-disable-line node/no-unpublished-require
+const stubLogger = require('../stub-logger');
+
+function lintHtml(html) {
+  const result = lint(html);
+  stubLogger.debug('validHtml', '', { result, html });
+  assert(!result);
+}
+
+describe('Template Helper', function () {
+  let ctx, options, pagePathLevel;
+
+  beforeEach(function () {
+    pagePathLevel = 2;
+    ctx = {};
+    options = {};
+  });
+
+  describe('dateOrNot', function () {
+    let date, otherwise;
+    beforeEach(function () {
+      date = new Date();
+      otherwise = 'otherwise';
+    });
+    it('covers', function () {
+      const result = th.dateOrNot(date, otherwise);
+      assert.strictEqual(result, date.toString());
+    });
+    it('covers no date', function () {
+      date = undefined;
+      const result = th.dateOrNot(date, otherwise);
+      assert.strictEqual(result, otherwise);
+    });
+    it('covers ms', function () {
+      const result = th.dateOrNot(date.getTime(), otherwise);
+      assert.strictEqual(result, date.toString());
+    });
+    it('covers naught', function () {
+      const result = th.dateOrNot(0, otherwise);
+      assert.strictEqual(result, otherwise);
+    });
+    it('covers the infinite', function () {
+      const result = th.dateOrNot(-Infinity, otherwise);
+      assert.strictEqual(result, otherwise);
+    });
+  }); // dateOrNot
+
+  describe('secondsToPeriod', function () {
+    it('covers seconds', function () {
+      const result = th.secondsToPeriod(45);
+      assert.strictEqual(result, '45 seconds');
+    });
+    it('covers minutes', function () {
+      const result = th.secondsToPeriod(105);
+      assert.strictEqual(result, '1 minute 45 seconds');
+    });
+    it('covers hours', function () {
+      const result = th.secondsToPeriod(3705);
+      assert.strictEqual(result, '1 hour 1 minute 45 seconds');
+    });
+    it('covers days', function () {
+      const result = th.secondsToPeriod(90105);
+      assert.strictEqual(result, '1 day 1 hour 1 minute 45 seconds');
+    });
+    it('covers months', function () {
+      const result = th.secondsToPeriod(5274105);
+      assert.strictEqual(result, '2 months 1 day 1 hour 1 minute 45 seconds');
+    });
+  }); // secondsToPeriod
+
+  describe('htmlHead', function () {
+    it('covers', function () {
+      const result = th.htmlHead(pagePathLevel, ctx, options);
+      assert(result);
+    });
+    it('covers elements', function () {
+      options.headElements = [ '<div>foop</div>', '<div>poof</div>' ];
+      const result = th.htmlHead(pagePathLevel, ctx, options);
+      assert(result);
+    });
+    it('covers onLoad', function () {
+      options.onload = 'onLoadFn';
+      const result = th.htmlHead(pagePathLevel, ctx, options);
+      assert(result);
+    });
+  }); // htmlHead
+
+  describe('renderNavLink', function () {
+    let nav;
+    beforeEach(function () {
+      nav = {
+        href: 'https://example.com/',
+        text: 'example',
+      };
+    });
+    it('covers no class', function () {
+      const result = th.renderNavLink(nav);
+      assert(result);
+    });
+    it('covers class', function () {
+      nav.class = 'foo bar';
+      const result = th.renderNavLink(nav);
+      assert(result);
+    });
+  }); // renderNavLink
+
+  describe('OL', function () {
+    it('covers', function () {
+      const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
+      assert(result);
+    });
+    it('covers defaults', function () {
+      const result = th.OL([]);
+      assert(result);
+    });
+  }); // OL
+
+  describe('UL', function () {
+    it('covers', function () {
+      const result = th.UL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
+      assert(result);
+    });
+    it('covers defaults', function () {
+      const result = th.UL([]);
+      assert(result);
+    });
+  }); // UL
+
+  describe('LI', function () {
+    it('covers defaults', function () {
+      const result = th.LI('item');
+      assert(result);
+    });
+  }); // LI
+
+  describe('htmlBody', function () {
+    it('covers no main', function () {
+      const result = th.htmlBody(pagePathLevel, ctx, options);
+      assert(result);
+    });
+  }); // htmlBody
+
+  describe('htmlHeader', function () {
+    it('covers no links', function () {
+      const result = th.htmlHeader(pagePathLevel, ctx, options);
+      assert(result);
+    });
+    it('covers links and logo', function () {
+      options.navLinks = [
+        {
+          href: 'https://exmaple.com/',
+          text: 'example',
+        },
+      ];
+      options.logoUrl = '/static/logo.svg';
+      const result = th.htmlHeader(pagePathLevel, ctx, options);
+      assert(result);
+    });
+  }); // htmlHeader
+
+  describe('htmlFooter', function () {
+    it('covers', function () {
+      options.footerEntries = [
+        '<div>foop</div>',
+        '<div>blah</div>',
+      ]
+      const result = th.htmlFooter(ctx, options);
+      assert(result);
+    });
+    it('covers default', function () {
+      const result = th.htmlFooter(ctx, options);
+      assert(!result);
+    });
+  }); // htmlFooter
+
+  describe('htmlMessages', function () {
+    it('covers', function () {
+      ctx.errors = ['an error'];
+      ctx.notifications = ['a notification'];
+      options.errorHeading = 'Errors';
+      options.notificationHeading = 'Notices';
+      options.errorContent = ['<p>Message about errors.</p>'];
+      options.notificationContent = ['<p>Message about notifications.</p>'];
+      const result = th.htmlMessages(ctx, options);
+      assert(result);
+    });
+  }); // htmlMessages
+
+  describe('htmlPage', function () {
+    let main;
+    beforeEach(function () {
+      main = [];
+    });
+    it('covers', function () {
+      const result = th.htmlPage(pagePathLevel, ctx, options, main);
+      lintHtml(result);
+      assert(result);
+    });
+    it('covers defaults', function () {
+      const result = th.htmlPage(pagePathLevel, ctx, options, main);
+      lintHtml(result);
+      assert(result);
+    });
+    it('covers user', function () {
+      ctx.session = {
+        authenticatedProfile: 'https://user.example.com/',
+      };
+      const result = th.htmlPage(pagePathLevel, ctx, options, main);
+      lintHtml(result);
+      assert(result);
+    });
+    it('covers user at root path', function () {
+      ctx.session = {
+        authenticatedIdentifier: 'user',
+      };
+      pagePathLevel = 0;
+      const result = th.htmlPage(pagePathLevel, ctx, options, main);
+      lintHtml(result);
+      assert(result);
+    });
+    it('covers logout redirect', function () {
+      ctx.session = {
+        authenticatedIdentifier: 'user',
+      };
+      ctx.url = 'https://app.example.com/this_page';
+      const result = th.htmlPage(pagePathLevel, ctx, options, main);
+      lintHtml(result);
+      assert(result);
+    });
+    it('covers existing navLinks', function () {
+      ctx.session = {
+        authenticatedIdentifier: 'user',
+      };
+      options.navLinks = [{
+        text: 'link',
+        href: 'link',
+      }];
+      const result = th.htmlPage(pagePathLevel, ctx, options);
+      lintHtml(result);
+      assert(result);
+    });
+  }); // htmlPage
+
+});