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

Closes #6728 Largest element with a background-image that is not kept #33

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/BeaconLcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class BeaconLcp {
element_info.bg_set = matches.map(m => m[1] ? { src: m[1].trim() } : {});
}

if (element_info.bg_set.length <= 0) {
return null;
}

if (element_info.bg_set.length > 0) {
element_info.src = element_info.bg_set[0].src;
if (element_info.type === "bg-img-set") {
Expand All @@ -165,7 +169,9 @@ class BeaconLcp {
}

_initWithFirstElementWithInfo(elements) {
const firstElementWithInfo = elements.find(item => item.elementInfo !== null);
const firstElementWithInfo = elements.find(item => {
return item.elementInfo !== null && (item.elementInfo.src || item.elementInfo.srcset);
});

if (!firstElementWithInfo) {
this.logger.logMessage("No LCP candidate found.");
Expand Down
70 changes: 68 additions & 2 deletions test/BeaconLcp.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import assert from 'assert';
import BeaconLcp from '../src/BeaconLcp.js';
import node_fetch from 'node-fetch';
import sinon from 'sinon';
global.fetch = node_fetch;

describe('BeaconManager', function() {
let beacon;
let beacon,
mockLogger;

const config = { nonce: 'test', url: 'http://example.com', is_mobile: false };
beforeEach(function() {
beacon = new BeaconLcp(config);
mockLogger = { logMessage: function(message) {} };

beacon = new BeaconLcp(config, mockLogger);

global.window = {};
global.document = {};

global.window.getComputedStyle = sinon.stub().returns({
getPropertyValue: sinon.stub().returns('none'),
});

global.getComputedStyle = (element, pseudoElement) => {
return {
getPropertyValue: (prop) => {
if (prop === "background-image") {
return "none";
}
return "";
}
};
};
});

afterEach(function () {
sinon.restore();
delete global.window;
});

describe('#constructor()', function() {
Expand All @@ -30,4 +58,42 @@ describe('BeaconManager', function() {
});
});

describe('#_initWithFirstElementWithInfo()', function() {
it('should initialize performanceImages with the first valid element info', function() {
const elements = [
{ element: { nodeName: 'div' }, elementInfo: null }, // invalid, no elementInfo
{ element: { nodeName: 'img', src: 'http://example.com/image1.jpg' }, elementInfo: { type: 'img', src: 'http://example.com/image1.jpg' } },
{ element: { nodeName: 'img', src: 'http://example.com/image2.jpg' }, elementInfo: { type: 'img', src: 'http://example.com/image2.jpg' } },
];

beacon._initWithFirstElementWithInfo(elements);

assert.strictEqual(beacon.performanceImages.length, 1);
assert.strictEqual(beacon.performanceImages[0].src, 'http://example.com/image1.jpg');
assert.strictEqual(beacon.performanceImages[0].label, 'lcp');
});

it('should not initialize performanceImages if no valid element info is found', function() {
const elements = [
{ element: { nodeName: 'div' }, elementInfo: null },
{ element: { nodeName: 'div' }, elementInfo: null },
];

beacon._initWithFirstElementWithInfo(elements);

assert.strictEqual(beacon.performanceImages.length, 0);
});
});

describe('#_getElementInfo()', function() {
it('should return null when there are no valid background images', function() {
const element = {
nodeName: 'div'
};

const elementInfo = beacon._getElementInfo(element);

assert.strictEqual(elementInfo, null);
});
});
});
Loading