add dateFormat
[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('dateFormat', function () {
54 it('renders otherwise', function () {
55 const expected = 'otherwise';
56 const result = th.dateFormat(undefined, undefined, undefined, expected);
57 assert.strictEqual(result, expected);
58 });
59 it('renders Infinity', function () {
60 const expected = 'end of time';
61 const result = th.dateFormat(Infinity, expected);
62 assert.strictEqual(result, expected);
63 });
64 it('renders -Infinity', function () {
65 const expected = 'beginning of time';
66 const result = th.dateFormat(-Infinity, undefined, expected);
67 assert.strictEqual(result, expected);
68 });
69 it('renders a Date', function () {
70 const expected = 'Mar 27, 2022, 3:28:05 PM PDT';
71 const result = th.dateFormat(new Date('2022-03-27T22:28:05.049Z'));
72 assert.strictEqual(result, expected);
73 });
74 it('renders a timestamp', function () {
75 const expected = 'Mar 27, 2022, 3:28:05 PM PDT';
76 const result = th.dateFormat(1648420085049);
77 assert.strictEqual(result, expected);
78 });
79 }); // dateFormat
80
81 describe('secondsToPeriod', function () {
82 it('covers seconds', function () {
83 const result = th.secondsToPeriod(45);
84 assert.strictEqual(result, '45 seconds');
85 });
86 it('covers minutes', function () {
87 const result = th.secondsToPeriod(105);
88 assert.strictEqual(result, '1 minute 45 seconds');
89 });
90 it('covers hours', function () {
91 const result = th.secondsToPeriod(3705);
92 assert.strictEqual(result, '1 hour 1 minute 45 seconds');
93 });
94 it('covers days', function () {
95 const result = th.secondsToPeriod(90105);
96 assert.strictEqual(result, '1 day 1 hour 1 minute 45 seconds');
97 });
98 it('covers months', function () {
99 const result = th.secondsToPeriod(5274105);
100 assert.strictEqual(result, '2 months 1 day 1 hour 1 minute 45 seconds');
101 });
102 }); // secondsToPeriod
103
104 describe('htmlHead', function () {
105 it('covers', function () {
106 const result = th.htmlHead(pagePathLevel, ctx, options);
107 assert(result);
108 });
109 it('covers elements', function () {
110 options.headElements = [ '<div>foop</div>', '<div>poof</div>' ];
111 const result = th.htmlHead(pagePathLevel, ctx, options);
112 assert(result);
113 });
114 it('covers onLoad', function () {
115 options.onload = 'onLoadFn';
116 const result = th.htmlHead(pagePathLevel, ctx, options);
117 assert(result);
118 });
119 }); // htmlHead
120
121 describe('renderNavLink', function () {
122 let nav;
123 beforeEach(function () {
124 nav = {
125 href: 'https://example.com/',
126 text: 'example',
127 };
128 });
129 it('covers no class', function () {
130 const result = th.renderNavLink(nav);
131 assert(result);
132 });
133 it('covers class', function () {
134 nav.class = 'foo bar';
135 const result = th.renderNavLink(nav);
136 assert(result);
137 });
138 }); // renderNavLink
139
140 describe('OL', function () {
141 it('covers', function () {
142 const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
143 assert(result);
144 });
145 it('covers defaults', function () {
146 const result = th.OL([]);
147 assert(result);
148 });
149 }); // OL
150
151 describe('UL', function () {
152 it('covers', function () {
153 const result = th.UL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
154 assert(result);
155 });
156 it('covers defaults', function () {
157 const result = th.UL([]);
158 assert(result);
159 });
160 }); // UL
161
162 describe('LI', function () {
163 it('covers defaults', function () {
164 const result = th.LI('item');
165 assert(result);
166 });
167 }); // LI
168
169 describe('htmlBody', function () {
170 it('covers no main', function () {
171 const result = th.htmlBody(pagePathLevel, ctx, options);
172 assert(result);
173 });
174 }); // htmlBody
175
176 describe('htmlHeader', function () {
177 it('covers no links', function () {
178 const result = th.htmlHeader(pagePathLevel, ctx, options);
179 assert(result);
180 });
181 it('covers links and logo', function () {
182 options.navLinks = [
183 {
184 href: 'https://exmaple.com/',
185 text: 'example',
186 },
187 ];
188 options.logoUrl = '/static/logo.svg';
189 const result = th.htmlHeader(pagePathLevel, ctx, options);
190 assert(result);
191 });
192 }); // htmlHeader
193
194 describe('htmlFooter', function () {
195 it('covers', function () {
196 options.footerEntries = [
197 '<div>foop</div>',
198 '<div>blah</div>',
199 ]
200 const result = th.htmlFooter(ctx, options);
201 assert(result);
202 });
203 it('covers default', function () {
204 const result = th.htmlFooter(ctx, options);
205 assert(!result);
206 });
207 }); // htmlFooter
208
209 describe('htmlMessages', function () {
210 it('covers', function () {
211 ctx.errors = ['an error'];
212 ctx.notifications = ['a notification'];
213 options.errorHeading = 'Errors';
214 options.notificationHeading = 'Notices';
215 options.errorContent = ['<p>Message about errors.</p>'];
216 options.notificationContent = ['<p>Message about notifications.</p>'];
217 const result = th.htmlMessages(ctx, options);
218 assert(result);
219 });
220 }); // htmlMessages
221
222 describe('htmlPage', function () {
223 let main;
224 beforeEach(function () {
225 main = [];
226 });
227 it('covers', function () {
228 const result = th.htmlPage(pagePathLevel, ctx, options, main);
229 lintHtml(result);
230 assert(result);
231 });
232 it('covers defaults', function () {
233 const result = th.htmlPage(pagePathLevel, ctx, options, main);
234 lintHtml(result);
235 assert(result);
236 });
237 it('covers user', function () {
238 ctx.session = {
239 authenticatedProfile: 'https://user.example.com/',
240 };
241 const result = th.htmlPage(pagePathLevel, ctx, options, main);
242 lintHtml(result);
243 assert(result);
244 });
245 it('covers user at root path', function () {
246 ctx.session = {
247 authenticatedIdentifier: 'user',
248 };
249 pagePathLevel = 0;
250 const result = th.htmlPage(pagePathLevel, ctx, options, main);
251 lintHtml(result);
252 assert(result);
253 });
254 it('covers logout redirect', function () {
255 ctx.session = {
256 authenticatedIdentifier: 'user',
257 };
258 ctx.url = 'https://app.example.com/this_page';
259 const result = th.htmlPage(pagePathLevel, ctx, options, main);
260 lintHtml(result);
261 assert(result);
262 });
263 it('covers existing navLinks', function () {
264 ctx.session = {
265 authenticatedIdentifier: 'user',
266 };
267 options.navLinks = [{
268 text: 'link',
269 href: 'link',
270 }];
271 const result = th.htmlPage(pagePathLevel, ctx, options);
272 lintHtml(result);
273 assert(result);
274 });
275 }); // htmlPage
276
277 });