X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Ftemplate-helper.js;h=58985ccd962683f175f29a879a189d708b913335;hb=HEAD;hp=a21b141eaeec7e37c3429fa9b1ea2d268fd4bbcc;hpb=5690ad318ed98cff45bf1fd192656a7156c7045c;p=squeep-html-template-helper diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js index a21b141..c62a3c0 100644 --- a/test/lib/template-helper.js +++ b/test/lib/template-helper.js @@ -3,14 +3,12 @@ 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); -} +const { makeHtmlLint } = require('../lint-html'); +const { HtmlValidate } = require('html-validate'); +const htmlValidate = new HtmlValidate(); +const htmlLint = makeHtmlLint(stubLogger, htmlValidate); describe('Template Helper', function () { let ctx, options, pagePathLevel; @@ -21,6 +19,16 @@ describe('Template Helper', function () { options = {}; }); + describe('initContext', function () { + it('covers', function () { + th.initContext(ctx); + assert(ctx.errors); + assert(ctx.notifications); + assert(Array.isArray(ctx.errors)); + assert(Array.isArray(ctx.notifications)); + }); + }); // initContext + describe('dateOrNot', function () { let date, otherwise; beforeEach(function () { @@ -83,6 +91,26 @@ describe('Template Helper', function () { }); }); // dateFormat + describe('timeElement', function () { + it('renders a date', function () { + const when = new Date('2022-09-11T21:17:56.872Z'); + const expected = ''; + const result = th.timeElement(when); + assert.strictEqual(result, expected); + }); + it('covers title', function () { + const when = new Date('2022-09-11T21:17:56.872Z'); + const expected = ''; + const result = th.timeElement(when, { title: 'a date' }); + assert.strictEqual(result, expected); + }); + it('covers other', function () { + const expected = ''; + const result = th.timeElement(1648420085049); + assert.strictEqual(result, expected); + }); + }); // timeElement + describe('secondsToPeriod', function () { it('covers seconds', function () { const result = th.secondsToPeriod(45); @@ -142,6 +170,17 @@ describe('Template Helper', function () { }); }); // renderNavLink + describe('elementAttributes', function () { + it('covers', function () { + const attributes = { + class: 'foo bar', + disabled: '', + }; + const result = th.elementAttributes(attributes); + assert.strictEqual(result, ' class="foo bar" disabled'); + }); + }); // elementAttributes + describe('OL', function () { it('covers', function () { const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` })); @@ -171,6 +210,13 @@ describe('Template Helper', function () { }); }); // LI + describe('indented', function () { + it('covers', function () { + const result = th.indented(2, ['foo', 'bar']); + result.forEach((r) => assert(r.startsWith('\t\t'))); + }); + }); // indented + describe('htmlBody', function () { it('covers no main', function () { const result = th.htmlBody(pagePathLevel, ctx, options); @@ -227,45 +273,54 @@ describe('Template Helper', function () { describe('htmlPage', function () { let main; beforeEach(function () { - main = []; + th.initContext(ctx); + ctx.errors.push('an error'); + ctx.notifications.push('a notice'); + options.headElements = ['']; + options.pageTitle = 'Test Page'; + main = [ + th.UL(['an item', 'another item']), + th.timeElement(new Date(), { title: 'now' }), + ]; }); - it('covers', function () { + it('covers', async function () { + this.slow(1000); // First invocation of htmlLint takes some time. const result = th.htmlPage(pagePathLevel, ctx, options, main); - lintHtml(result); + await htmlLint(result); assert(result); }); - it('covers defaults', function () { + it('covers defaults', async function () { const result = th.htmlPage(pagePathLevel, ctx, options, main); - lintHtml(result); + await htmlLint(result); assert(result); }); - it('covers user', function () { + it('covers user', async function () { ctx.session = { authenticatedProfile: 'https://user.example.com/', }; const result = th.htmlPage(pagePathLevel, ctx, options, main); - lintHtml(result); + await htmlLint(result); assert(result); }); - it('covers user at root path', function () { + it('covers user at root path', async function () { ctx.session = { authenticatedIdentifier: 'user', }; pagePathLevel = 0; const result = th.htmlPage(pagePathLevel, ctx, options, main); - lintHtml(result); + await htmlLint(result); assert(result); }); - it('covers logout redirect', function () { + it('covers logout redirect', async function () { ctx.session = { authenticatedIdentifier: 'user', }; ctx.url = 'https://app.example.com/this_page'; const result = th.htmlPage(pagePathLevel, ctx, options, main); - lintHtml(result); + await htmlLint(result); assert(result); }); - it('covers existing navLinks', function () { + it('covers existing navLinks', async function () { ctx.session = { authenticatedIdentifier: 'user', }; @@ -274,7 +329,7 @@ describe('Template Helper', function () { href: 'link', }]; const result = th.htmlPage(pagePathLevel, ctx, options); - lintHtml(result); + await htmlLint(result); assert(result); }); }); // htmlPage