Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Apr 8, 2017
1 parent 719ff97 commit 2cb25c4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lighthouse-core/report/v2/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ReportRenderer {
// Remove the same number of elements as there are capture groups.
// eslint-disable-next-line no-unused-vars
const [preambleText, fullMarkdownLink, linkText, linkHref] = parts.splice(0, 4);
element.appendChild(document.createTextNode(preambleText));
element.appendChild(this._document.createTextNode(preambleText));

// Append link if there are any.
if (linkText && linkHref) {
Expand Down
84 changes: 50 additions & 34 deletions lighthouse-core/test/report/v2/report-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,55 @@ const sampleResults = require('../../results/sample_v2.json');
const TEMPLATE_FILE = fs.readFileSync(__dirname + '/../../../report/v2/templates.html', 'utf8');

describe('ReportRenderer V2', () => {
describe('renderReport', () => {
const document = jsdom.jsdom(TEMPLATE_FILE);
const renderer = new ReportRenderer(document);
const document = jsdom.jsdom(TEMPLATE_FILE);
const renderer = new ReportRenderer(document);

describe('createElement', () => {
it('creates a simple element using default values', () => {
const el = renderer._createElement('div');
assert.equal(el.localName, 'div');
assert.equal(el.className, '');
assert.equal(el.className, el.attributes.length);
});

it('creates an element from parameters', () => {
const el = renderer._createElement(
'div', 'class1 class2', {title: 'title attr', tabindex: 0});
assert.equal(el.localName, 'div');
assert.equal(el.className, 'class1 class2');
assert.equal(el.getAttribute('title'), 'title attr');
assert.equal(el.getAttribute('tabindex'), '0');
});
});

describe('cloneTemplate', () => {
it('should clone a template', () => {
const clone = renderer._cloneTemplate('#tmpl-lighthouse-audit-score');
assert.ok(clone.querySelector('.lighthouse-score'));
});

it('fails when template cannot be found', () => {
assert.throws(() => renderer._cloneTemplate('#unknown-selector'));
});
});

describe('_convertMarkdownLinksToElement', () => {
it('converts links', () => {
const result = renderer._convertMarkdownLinksToElement(
'Some [link](https://example.com/foo). [Learn more](http://example.com).');
assert.equal(result.innerHTML,
'Some <a rel="noopener" target="_blank" href="https://example.com/foo">link</a>. ' +
'<a rel="noopener" target="_blank" href="http://example.com">Learn more</a>.');
});

it('ignores links that do not start with http', () => {
const text = 'Sentence with [link](/local/path).';
const result = renderer._convertMarkdownLinksToElement(text);
assert.equal(result.innerHTML, text);
});
});

describe('renderReport', () => {
it('should render a report', () => {
const output = renderer.renderReport(sampleResults);
assert.ok(output.classList.contains('lighthouse-report'));
Expand All @@ -44,35 +89,6 @@ describe('ReportRenderer V2', () => {
assert.ok(output.classList.contains('lighthouse-exception'));
});

describe('createElement', () => {
it('creates a simple element using default values', () => {
const el = renderer._createElement('div');
assert.equal(el.localName, 'div');
assert.equal(el.className, '');
assert.equal(el.className, el.attributes.length);
});

it('creates an element from parameters', () => {
const el = renderer._createElement(
'div', 'class1 class2', {title: 'title attr', tabindex: 0});
assert.equal(el.localName, 'div');
assert.equal(el.className, 'class1 class2');
assert.equal(el.getAttribute('title'), 'title attr');
assert.equal(el.getAttribute('tabindex'), '0');
});
});

describe('cloneTemplate', () => {
it('should clone a template', () => {
const clone = renderer._cloneTemplate('#tmpl-lighthouse-audit-score');
assert.ok(clone.querySelector('.lighthouse-score'));
});

it('fails when template cannot be found', () => {
assert.throws(() => renderer._cloneTemplate('#unknown-selector'));
});
});

it('renders an audit', () => {
const audit = sampleResults.reportCategories[0].audits[0];
const auditDOM = renderer._renderAudit(audit);
Expand All @@ -82,7 +98,7 @@ describe('ReportRenderer V2', () => {
const score = auditDOM.querySelector('.lighthouse-score__value');

assert.equal(title.textContent, audit.result.description);
assert.equal(description.textContent, audit.result.helpText);
assert.ok(description.querySelector('a'), 'audit help text contains coverted markdown links');
assert.equal(score.textContent, '0');
assert.ok(score.classList.contains('lighthouse-score__value--fail'));
assert.ok(score.classList.contains(`lighthouse-score__value--${audit.result.scoringMode}`));
Expand All @@ -102,7 +118,7 @@ describe('ReportRenderer V2', () => {
'category score is numeric');
assert.equal(value.textContent, Math.round(category.score), 'category score is rounded');
assert.equal(title.textContent, category.name, 'title is set');
assert.equal(description.textContent, category.description, 'description is set');
assert.ok(description.querySelector('a'), 'description contains converted markdown links');

const audits = categoryDOM.querySelectorAll('.lighthouse-category > .lighthouse-audit');
assert.equal(audits.length, category.audits.length, 'renders correct number of audits');
Expand Down

0 comments on commit 2cb25c4

Please sign in to comment.