initial commit
[squeep-html-template-helper] / test / lib / template-helper.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const th = require('../../lib/template-helper');
6 const lint = require('html-minifier-lint').lint; // eslint-disable-line node/no-unpublished-require
7 const stubLogger = require('../stub-logger');
8
9 function lintHtml(html) {
10 const result = lint(html);
11 stubLogger.debug('validHtml', '', { result, html });
12 assert(!result);
13 }
14
15 describe('Template Helper', function () {
16 let ctx, options, pagePathLevel;
17
18 beforeEach(function () {
19 pagePathLevel = 2;
20 ctx = {};
21 options = {};
22 });
23
24 describe('dateOrNot', function () {
25 let date, otherwise;
26 beforeEach(function () {
27 date = new Date();
28 otherwise = 'otherwise';
29 });
30 it('covers', function () {
31 const result = th.dateOrNot(date, otherwise);
32 assert.strictEqual(result, date.toString());
33 });
34 it('covers no date', function () {
35 date = undefined;
36 const result = th.dateOrNot(date, otherwise);
37 assert.strictEqual(result, otherwise);
38 });
39 it('covers ms', function () {
40 const result = th.dateOrNot(date.getTime(), otherwise);
41 assert.strictEqual(result, date.toString());
42 });
43 it('covers naught', function () {
44 const result = th.dateOrNot(0, otherwise);
45 assert.strictEqual(result, otherwise);
46 });
47 it('covers the infinite', function () {
48 const result = th.dateOrNot(-Infinity, otherwise);
49 assert.strictEqual(result, otherwise);
50 });
51 }); // dateOrNot
52
53 describe('secondsToPeriod', function () {
54 it('covers seconds', function () {
55 const result = th.secondsToPeriod(45);
56 assert.strictEqual(result, '45 seconds');
57 });
58 it('covers minutes', function () {
59 const result = th.secondsToPeriod(105);
60 assert.strictEqual(result, '1 minute 45 seconds');
61 });
62 it('covers hours', function () {
63 const result = th.secondsToPeriod(3705);
64 assert.strictEqual(result, '1 hour 1 minute 45 seconds');
65 });
66 it('covers days', function () {
67 const result = th.secondsToPeriod(90105);
68 assert.strictEqual(result, '1 day 1 hour 1 minute 45 seconds');
69 });
70 it('covers months', function () {
71 const result = th.secondsToPeriod(5274105);
72 assert.strictEqual(result, '2 months 1 day 1 hour 1 minute 45 seconds');
73 });
74 }); // secondsToPeriod
75
76 describe('htmlHead', function () {
77 it('covers', function () {
78 const result = th.htmlHead(pagePathLevel, ctx, options);
79 assert(result);
80 });
81 it('covers elements', function () {
82 options.headElements = [ '<div>foop</div>', '<div>poof</div>' ];
83 const result = th.htmlHead(pagePathLevel, ctx, options);
84 assert(result);
85 });
86 it('covers onLoad', function () {
87 options.onload = 'onLoadFn';
88 const result = th.htmlHead(pagePathLevel, ctx, options);
89 assert(result);
90 });
91 }); // htmlHead
92
93 describe('renderNavLink', function () {
94 let nav;
95 beforeEach(function () {
96 nav = {
97 href: 'https://example.com/',
98 text: 'example',
99 };
100 });
101 it('covers no class', function () {
102 const result = th.renderNavLink(nav);
103 assert(result);
104 });
105 it('covers class', function () {
106 nav.class = 'foo bar';
107 const result = th.renderNavLink(nav);
108 assert(result);
109 });
110 }); // renderNavLink
111
112 describe('OL', function () {
113 it('covers', function () {
114 const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
115 assert(result);
116 });
117 it('covers defaults', function () {
118 const result = th.OL([]);
119 assert(result);
120 });
121 }); // OL
122
123 describe('UL', function () {
124 it('covers', function () {
125 const result = th.UL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
126 assert(result);
127 });
128 it('covers defaults', function () {
129 const result = th.UL([]);
130 assert(result);
131 });
132 }); // UL
133
134 describe('LI', function () {
135 it('covers defaults', function () {
136 const result = th.LI('item');
137 assert(result);
138 });
139 }); // LI
140
141 describe('htmlBody', function () {
142 it('covers no main', function () {
143 const result = th.htmlBody(pagePathLevel, ctx, options);
144 assert(result);
145 });
146 }); // htmlBody
147
148 describe('htmlHeader', function () {
149 it('covers no links', function () {
150 const result = th.htmlHeader(pagePathLevel, ctx, options);
151 assert(result);
152 });
153 it('covers links and logo', function () {
154 options.navLinks = [
155 {
156 href: 'https://exmaple.com/',
157 text: 'example',
158 },
159 ];
160 options.logoUrl = '/static/logo.svg';
161 const result = th.htmlHeader(pagePathLevel, ctx, options);
162 assert(result);
163 });
164 }); // htmlHeader
165
166 describe('htmlFooter', function () {
167 it('covers', function () {
168 options.footerEntries = [
169 '<div>foop</div>',
170 '<div>blah</div>',
171 ]
172 const result = th.htmlFooter(ctx, options);
173 assert(result);
174 });
175 it('covers default', function () {
176 const result = th.htmlFooter(ctx, options);
177 assert(!result);
178 });
179 }); // htmlFooter
180
181 describe('htmlMessages', function () {
182 it('covers', function () {
183 ctx.errors = ['an error'];
184 ctx.notifications = ['a notification'];
185 options.errorHeading = 'Errors';
186 options.notificationHeading = 'Notices';
187 options.errorContent = ['<p>Message about errors.</p>'];
188 options.notificationContent = ['<p>Message about notifications.</p>'];
189 const result = th.htmlMessages(ctx, options);
190 assert(result);
191 });
192 }); // htmlMessages
193
194 describe('htmlPage', function () {
195 let main;
196 beforeEach(function () {
197 main = [];
198 });
199 it('covers', function () {
200 const result = th.htmlPage(pagePathLevel, ctx, options, main);
201 lintHtml(result);
202 assert(result);
203 });
204 it('covers defaults', function () {
205 const result = th.htmlPage(pagePathLevel, ctx, options, main);
206 lintHtml(result);
207 assert(result);
208 });
209 it('covers user', function () {
210 ctx.session = {
211 authenticatedProfile: 'https://user.example.com/',
212 };
213 const result = th.htmlPage(pagePathLevel, ctx, options, main);
214 lintHtml(result);
215 assert(result);
216 });
217 it('covers user at root path', function () {
218 ctx.session = {
219 authenticatedIdentifier: 'user',
220 };
221 pagePathLevel = 0;
222 const result = th.htmlPage(pagePathLevel, ctx, options, main);
223 lintHtml(result);
224 assert(result);
225 });
226 it('covers logout redirect', function () {
227 ctx.session = {
228 authenticatedIdentifier: 'user',
229 };
230 ctx.url = 'https://app.example.com/this_page';
231 const result = th.htmlPage(pagePathLevel, ctx, options, main);
232 lintHtml(result);
233 assert(result);
234 });
235 it('covers existing navLinks', function () {
236 ctx.session = {
237 authenticatedIdentifier: 'user',
238 };
239 options.navLinks = [{
240 text: 'link',
241 href: 'link',
242 }];
243 const result = th.htmlPage(pagePathLevel, ctx, options);
244 lintHtml(result);
245 assert(result);
246 });
247 }); // htmlPage
248
249 });