Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdzwinel committed Oct 11, 2017
1 parent a73c4b5 commit 1b24a42
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lighthouse-core/audits/seo/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class FontSize extends Audit {
helpText: 'Font sizes less than 16px are too small to be legible and require mobile ' +
'visitors to “pinch to zoom” in order to read. ' +
'[Learn more](https://developers.google.com/speed/docs/insights/UseLegibleFontSizes).',
requiredArtifacts: ['FontSize', 'Styles', 'URL'],
requiredArtifacts: ['FontSize', 'Styles', 'URL', 'Viewport'],
};
}

Expand All @@ -171,7 +171,6 @@ class FontSize extends Audit {
if (totalTextLenght === 0) {
return {
rawValue: true,
debugString: 'Page contains no text',
};
}

Expand Down
111 changes: 111 additions & 0 deletions lighthouse-core/test/audits/seo/font-size-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const FontSizeAudit = require('../../../audits/seo/font-size.js');
const assert = require('assert');
const CSSStyleDeclaration = require('../../../lib/web-inspector').CSSStyleDeclaration;

const URL = 'https://example.com';
const Styles = [];
const validViewport = 'width=device-width';

/* eslint-env mocha */

describe('SEO: Font size audit', () => {
it('fails when viewport is not set', () => {
const artifacts = {
URL,
Viewport: null,
Styles,
FontSize: [],
};

const auditResult = FontSizeAudit.audit(artifacts);
assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.debugString.includes('missing viewport'));
});

it('fails when less than 75% of text is legible', () => {
const artifacts = {
URL,
Viewport: validViewport,
Styles,
FontSize: [
{textLength: 1, fontSize: 15, node: {nodeId: 1, localName: 'p', attributes: []}},
{textLength: 2, fontSize: 16, node: {nodeId: 2, localName: 'p', attributes: []}},
],
};

const auditResult = FontSizeAudit.audit(artifacts);
assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.debugString.includes('33.33%'));
});

it('passes when there is no text', () => {
const artifacts = {
URL,
Viewport: validViewport,
Styles,
FontSize: [
{textLength: 0},
{textLength: 0},
],
};

const auditResult = FontSizeAudit.audit(artifacts);
assert.equal(auditResult.rawValue, true);
});

it('passes when more than 75% of text is legible', () => {
const artifacts = {
URL,
Viewport: validViewport,
Styles,
FontSize: [
{textLength: 1, fontSize: 15, node: {nodeId: 1, localName: 'p', attributes: []}},
{textLength: 2, fontSize: 16, node: {nodeId: 2, localName: 'p', attributes: []}},
{textLength: 2, fontSize: 24, node: {nodeId: 3, localName: 'p', attributes: []}},
],
};
const auditResult = FontSizeAudit.audit(artifacts);
assert.equal(auditResult.rawValue, true);
});

it('groups entries with same source, sorts them by coverage', () => {
const style1 = {
styleSheetId: 1,
type: CSSStyleDeclaration.Type.Regular,
range: {
startLine: 123,
startColumn: 10,
},
};
const style2 = {
styleSheetId: 1,
type: CSSStyleDeclaration.Type.Regular,
range: {
startLine: 0,
startColumn: 10,
},
};
const artifacts = {
URL,
Viewport: validViewport,
Styles,
FontSize: [
{textLength: 3, fontSize: 15, node: {nodeId: 1}, cssRule: style1},
{textLength: 2, fontSize: 14, node: {nodeId: 2}, cssRule: style2},
{textLength: 2, fontSize: 14, node: {nodeId: 3}, cssRule: style2},
],
};
const auditResult = FontSizeAudit.audit(artifacts);

assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.details.items.length, 2);
assert.equal(auditResult.details.items[0][2].text, '57.14%');
});
});

0 comments on commit 1b24a42

Please sign in to comment.