Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(treemap): add test for node coverage shading #12609

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion lighthouse-treemap/test/treemap-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
const fs = require('fs');
const puppeteer = require('puppeteer');
const {server} = require('../../lighthouse-cli/test/fixtures/static-server.js');
const portNumber = 10200;
const portNumber = 20202;
const treemapUrl = `http://localhost:${portNumber}/dist/gh-pages/treemap/index.html`;
const debugOptions = require('../app/debug.json');

Expand Down Expand Up @@ -141,4 +141,49 @@ describe('Lighthouse Treemap', () => {
expect(optionsInPage.lhr.requestedUrl).toBe(options.lhr.requestedUrl);
});
});

describe('renders correctly', () => {
it('correctly shades coverage of gtm node', async () => {
await page.goto(`${treemapUrl}?debug`, {
waitUntil: 'networkidle0',
timeout: 30000,
});

await page.click('#view-mode--unused-bytes');
await page.waitForSelector('.lh-treemap--view-mode--unused-bytes');

// Identify the JS data.
const gtmNode = await page.evaluate(() => {
const d1Nodes = window.__treemapOptions.lhr.audits['script-treemap-data'].details.nodes;
const gtmNode = d1Nodes.find(n => n.name.includes('gtm.js'));
return gtmNode;
});

expect(gtmNode.unusedBytes).toBeGreaterThan(20_000);
expect(gtmNode.resourceBytes).toBeGreaterThan(20_000);

// Identify the DOM node.
const gtmElemHandle = await page.evaluateHandle(() => {
const captionEls = Array.from(document.querySelectorAll('.webtreemap-caption'));
return captionEls.find(el => el.textContent.includes('gtm.js')).parentElement;
});

expect(await gtmElemHandle.isIntersectingViewport()).toBeTruthy();

// Determine visual red shading percentage.
const percentRed = await gtmElemHandle.evaluate(node => {
const redWidthPx = parseInt(window.getComputedStyle(node, ':before').width);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would const redWidthPx = node.clientWidth be the same?

oh, no, because pseudoelement. and no way to get a reference to that in JS.

TIL the second parameter of getComputedStyle. 0 : would make sense. 2 does too. but 1?!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would const redWidthPx = node.clientWidth be the same?
oh, no, because pseudoelement. and no way to get a reference to that in JS.

yup exactly.

TIL the second parameter of getComputedStyle. 0 : would make sense. 2 does too. but 1?!?

yeah it's weird. spec says 1 or two colons is fine. https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle

looks like chrome works with zero, one or two colons. handy.

const completeWidthPx = node.getBoundingClientRect().width;
return redWidthPx / completeWidthPx;
});

// Reminder! UNUSED == RED
const percentDataUnused = gtmNode.unusedBytes / gtmNode.resourceBytes;
expect(percentDataUnused).toBeGreaterThan(0);

// Assert 0.2520 ~= 0.2602 w/ 1 decimal place of precision.
// CSS pixels won't let us go to 2 decimal places.
expect(percentRed).toBeApproximately(percentDataUnused, 1);
});
});
});