Skip to content

Commit

Permalink
unit more
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Dec 7, 2023
1 parent cd40c14 commit fae4882
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
8 changes: 2 additions & 6 deletions core/computed/metrics/cumulative-layout-shift.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ class CumulativeLayoutShift {
const impactByNodeId = new Map();

for (const event of layoutShiftEvents) {
if (!event || !event.impactedNodes) {
continue;
}
if (!event.impactedNodes) continue;

let totalAreaOfImpact = 0;
/** @type {Map<number, number>} */
const pixelsMovedPerNode = new Map();

for (const node of event.impactedNodes) {
if (!node.node_id || !node.old_rect || !node.new_rect) {
continue;
}
if (!node.node_id || !node.old_rect || !node.new_rect) continue;

const oldRect = RectHelpers.traceRectToLHRect(node.old_rect);
const newRect = RectHelpers.traceRectToLHRect(node.new_rect);
Expand Down
74 changes: 74 additions & 0 deletions core/test/computed/metrics/cumulative-layout-shift-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,79 @@ describe('Metrics: CLS', () => {
[25, 0.6],
]);
});

it('ignores events with no impacted nodes', () => {
const layoutShiftEvents = [
{
ts: 1_000_000,
isMainFrame: true,
weightedScore: 1,
impactedNodes: [
{
new_rect: [0, 0, 200, 200],
node_id: 60,
old_rect: [0, 0, 200, 100],
},
{
new_rect: [0, 300, 200, 200],
node_id: 25,
old_rect: [0, 100, 200, 100],
},
],
},
{
ts: 2_000_000,
isMainFrame: true,
weightedScore: 0.3,
},
];

const impactByNodeId = CumulativeLayoutShift.getImpactByNodeId(layoutShiftEvents);
expect(Array.from(impactByNodeId.entries())).toEqual([
[60, 0.4],
[25, 0.6],
]);
});

it('ignores malformed impacted nodes', () => {
const layoutShiftEvents = [
{
ts: 1_000_000,
isMainFrame: true,
weightedScore: 1,
impactedNodes: [
{
// Malformed, no old_rect
// Entire weightedScore is therefore attributed to node_id 25
new_rect: [0, 0, 200, 200],
node_id: 60,
},
{
new_rect: [0, 300, 200, 200],
node_id: 25,
old_rect: [0, 100, 200, 100],
},
],
},
{
ts: 2_000_000,
isMainFrame: true,
weightedScore: 0.3,
impactedNodes: [
{
new_rect: [0, 100, 200, 200],
node_id: 60,
old_rect: [0, 0, 200, 200],
},
],
},
];

const impactByNodeId = CumulativeLayoutShift.getImpactByNodeId(layoutShiftEvents);
expect(Array.from(impactByNodeId.entries())).toEqual([
[25, 1],
[60, 0.3],
]);
});
});
});

0 comments on commit fae4882

Please sign in to comment.