Skip to content

Commit

Permalink
add no-unload unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Jul 13, 2020
1 parent 4df59ca commit 7558d39
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lighthouse-core/audits/no-unload-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class NoUnloadListeners extends Audit {
const tableItems = unloadListeners.map(listener => {
const url = scriptIdToUrl.get(listener.scriptId);

// If we can't find a url, still show something so the user can still
// manually look for where an `unload` handler is being created.
// If we can't find a url, still show something so the user can manually
// look for where an `unload` handler is being created.
if (!url) {
return {
source: {
Expand Down
77 changes: 77 additions & 0 deletions lighthouse-core/test/audits/no-unload-listeners-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license Copyright 2020 The Lighthouse Authors. 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 NoUnloadListeners = require('../../audits/no-unload-listeners.js');

/* eslint-env jest */

const testJsUsage = {
'https://example.com/1.js': [
{scriptId: '12', url: 'https://example.com/1.js', functions: []},
{scriptId: '13', url: 'https://example.com/1.js', functions: []},
{scriptId: '16', url: 'https://example.com/1.js', functions: []},
{scriptId: '17', url: 'https://example.com/1.js', functions: []},
],
'https://example.com/2.js': [
{scriptId: '22', url: 'https://example.com/2.js', functions: []},
{scriptId: '23', url: 'https://example.com/2.js', functions: []},
{scriptId: '26', url: 'https://example.com/2.js', functions: []},
{scriptId: '27', url: 'https://example.com/2.js', functions: []},
],
};

describe('No Unload Listeners', () => {
it('passes when there were no listeners', () => {
const artifacts = {JsUsage: testJsUsage, GlobalListeners: []};
const result = NoUnloadListeners.audit(artifacts);
expect(result).toEqual({score: 1});
});

it('passes when there were no `unload` listeners', () => {
const GlobalListeners = [{
type: 'DOMContentLoaded', scriptId: '12', lineNumber: 5, columnNumber: 0,
}];
const artifacts = {JsUsage: testJsUsage, GlobalListeners};
const result = NoUnloadListeners.audit(artifacts);
expect(result).toEqual({score: 1});
});

it('fails when there are unload listeners and matches them to script locations', () => {
const GlobalListeners = [
{type: 'unload', scriptId: '16', lineNumber: 10, columnNumber: 30},
{type: 'unload', scriptId: '23', lineNumber: 0, columnNumber: 0},
];
const artifacts = {JsUsage: testJsUsage, GlobalListeners};
const result = NoUnloadListeners.audit(artifacts);
expect(result.score).toEqual(0);
expect(result.details.items).toMatchObject([
{
source: {type: 'source-location', url: 'https://example.com/1.js', urlProvider: 'network', line: 10, column: 30},
}, {
source: {type: 'source-location', url: 'https://example.com/2.js', urlProvider: 'network', line: 0, column: 0},
},
]);
});

it('fails when there are unload listeners and has a fallback if script URL is not found', () => {
const GlobalListeners = [
{type: 'DOMContentLoaded', scriptId: '12', lineNumber: 5, columnNumber: 0},
{type: 'unload', scriptId: 'notascriptid', lineNumber: 10, columnNumber: 30},
{type: 'unload', scriptId: '22', lineNumber: 1, columnNumber: 100},
];
const artifacts = {JsUsage: testJsUsage, GlobalListeners};
const result = NoUnloadListeners.audit(artifacts);
expect(result.score).toEqual(0);
expect(result.details.items).toMatchObject([
{
source: {type: 'url', value: '(unknown)'},
}, {
source: {type: 'source-location', url: 'https://example.com/2.js', urlProvider: 'network', line: 1, column: 100},
},
]);
});
});

0 comments on commit 7558d39

Please sign in to comment.