Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Oct 11, 2024
1 parent adc4834 commit 4b5bd67
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions test/BeaconLcp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import node_fetch from 'node-fetch';
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);
});

describe('#constructor()', function() {
Expand All @@ -30,4 +34,30 @@ 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);
});
});
});

0 comments on commit 4b5bd67

Please sign in to comment.