Skip to content

Commit

Permalink
Merge commit '14cdaf973bea202d3c3b7e12e95c8287152ca754' into enhancem…
Browse files Browse the repository at this point in the history
…ent/automated-pr-wprocket-and-remove-dist
  • Loading branch information
MathieuLamiot committed Aug 16, 2024
2 parents 17ad7e2 + 14cdaf9 commit 7332ce4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 89 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-rocket-scripts",
"version": "1.0.3",
"version": "1.0.4",
"description": "Rocket main scripts packages",
"type": "module",
"main": "index.js",
Expand Down
9 changes: 8 additions & 1 deletion src/BeaconManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BeaconManager {
}, 10000);

const isGeneratedBefore = await this._getGeneratedBefore();
let shouldSaveResultsIntoDB = false;

// OCI / LCP / ATF
const shouldGenerateLcp = (
Expand All @@ -33,11 +34,17 @@ class BeaconManager {
if (shouldGenerateLcp) {
this.lcpBeacon = new BeaconLcp(this.config, this.logger);
await this.lcpBeacon.run();
shouldSaveResultsIntoDB = true;
} else {
this.logger.logMessage('Not running BeaconLcp because data is already available');
}

this._saveFinalResultIntoDB();
if (shouldSaveResultsIntoDB) {
this._saveFinalResultIntoDB();
} else {
this.logger.logMessage("Not saving results into DB as no beacon features ran.");
this._finalize();
}
}

async _isValidPreconditions() {
Expand Down
34 changes: 34 additions & 0 deletions test/BeaconLcp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'assert';
import sinon from 'sinon';
import BeaconLcp from '../src/BeaconLcp.js';
import node_fetch from 'node-fetch';
global.fetch = node_fetch;

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

describe('#constructor()', function() {
it('should initialize with the given config', function() {
assert.deepStrictEqual(beacon.config, config);
});
});

describe('#_isDuplicateImage()', function() {
it('should return true for a duplicate image', function() {
beacon.performanceImages = [{ src: 'http://example.com/image.jpg', nodeName:'img', type:'img' }];
const image = { src: 'http://example.com/image.jpg', nodeName:'img', type:'img' };
assert.strictEqual(beacon._isDuplicateImage(image), true);
});

it('should return false for a unique image', function() {
beacon.performanceImages = [{ src: 'http://example.com/image.jpg', nodeName:'img', type:'img' }];
const image = { src: 'http://example.com/unique.jpg', nodeName:'img', type:'img' };
assert.strictEqual(beacon._isDuplicateImage(image), false);
});
});

});
97 changes: 10 additions & 87 deletions test/LcpBeacon.test.js → test/BeaconManager.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import assert from 'assert';
import sinon from 'sinon';
import LcpBeacon from '../src/LcpBeacon.js';
import BeaconLcp from '../src/BeaconLcp.js';
import BeaconManager from '../src/BeaconManager.js'
import node_fetch from 'node-fetch';
global.fetch = node_fetch;


describe('LcpBeacon', function() {
describe('BeaconManager', function() {
let beacon;
const config = { nonce: 'test', url: 'http://example.com', is_mobile: false };
beforeEach(function() {
beacon = new LcpBeacon(config);
beacon = new BeaconManager(config);
});

describe('#constructor()', function() {
Expand All @@ -32,27 +32,10 @@ describe('LcpBeacon', function() {
}
};

// Mock _isPageCached and _isGeneratedBefore to return false to simulate valid preconditions
beacon._isPageCached = () => false;
beacon._isGeneratedBefore = async () => false;

const result = await beacon._isValidPreconditions();
assert.strictEqual(result, true);
});
});
describe('#_isDuplicateImage()', function() {
it('should return true for a duplicate image', function() {
beacon.performanceImages = [{ src: 'http://example.com/image.jpg', nodeName:'img', type:'img' }];
const image = { src: 'http://example.com/image.jpg', nodeName:'img', type:'img' };
assert.strictEqual(beacon._isDuplicateImage(image), true);
});

it('should return false for a unique image', function() {
beacon.performanceImages = [{ src: 'http://example.com/image.jpg', nodeName:'img', type:'img' }];
const image = { src: 'http://example.com/unique.jpg', nodeName:'img', type:'img' };
assert.strictEqual(beacon._isDuplicateImage(image), false);
});
});

describe('#_getFinalStatus()', function() {
it('should return error code if set', function() {
Expand Down Expand Up @@ -135,7 +118,8 @@ describe('LcpBeacon', function() {
// Spy on setAttribute to ensure it's called with the correct arguments
setAttributeSpy = sinon.spy(global.document.querySelector('[data-name="wpr-lcp-beacon"]'), 'setAttribute');
// Prepare performanceImages or any other data needed by _saveFinalResultIntoDB
beacon.performanceImages = [{ src: 'http://example.com/image.jpg', label: 'lcp' }];
beacon.lcpBeacon = new BeaconLcp(beacon.config);
beacon.lcpBeacon.performanceImages = [{ src: 'http://example.com/image.jpg', label: 'lcp' }];
beacon.errorCode = '';
beacon.scriptTimer = new Date();
finalizeSpy = sinon.spy(beacon, '_finalize');
Expand All @@ -161,80 +145,19 @@ describe('LcpBeacon', function() {
const sentDataObject = formDataToObject(sentFormData);

// Now you can assert the values in sentDataObject
assert.strictEqual(sentDataObject['action'], 'rocket_lcp');
assert.strictEqual(sentDataObject['rocket_lcp_nonce'], config.nonce);
assert.strictEqual(sentDataObject['action'], 'rocket_beacon');
assert.strictEqual(sentDataObject['rocket_beacon_nonce'], config.nonce);
assert.strictEqual(sentDataObject['url'], config.url);
assert.strictEqual(sentDataObject['is_mobile'], config.is_mobile.toString());
// For complex types like arrays or objects, you might need to parse them before assertion
assert.deepStrictEqual(JSON.parse(sentDataObject['images']), beacon.performanceImages);
const expectedResults = JSON.parse(JSON.stringify({lcp : beacon.lcpBeacon.performanceImages}));
assert.deepStrictEqual(JSON.parse(sentDataObject['results']), expectedResults);
assert.strictEqual(sentDataObject['status'], beacon._getFinalStatus());
sinon.assert.calledOnce(finalizeSpy);
sinon.assert.calledWith(setAttributeSpy, 'beacon-completed', 'true');
});
});

describe('#_isIntersecting', function() {
beforeEach(function () {
// Mock viewport size
global.window = {
innerWidth: 1024,
innerHeight: 768
};
});

it('should return true for a rectangle fully within the viewport', function () {
const rect = {top: 100, left: 100, bottom: 200, right: 200};
assert.strictEqual(beacon._isIntersecting(rect), true);
});

it('should return false for a rectangle entirely above the viewport', function () {
const rect = {top: -500, left: 100, bottom: -400, right: 200};
assert.strictEqual(beacon._isIntersecting(rect), false);
});

it('should return false for a rectangle entirely below the viewport', function () {
const rect = {top: 800, left: 100, bottom: 900, right: 200};
assert.strictEqual(beacon._isIntersecting(rect), false);
});

it('should return false for a rectangle entirely to the left of the viewport', function () {
const rect = {top: 100, left: -500, bottom: 200, right: -400};
assert.strictEqual(beacon._isIntersecting(rect), false);
});

it('should return false for a rectangle entirely to the right of the viewport', function () {
const rect = {top: 100, left: 1100, bottom: 200, right: 1200};
assert.strictEqual(beacon._isIntersecting(rect), false);
});
});

describe('#_isPageCached', function() {

it('should return true when the page is cached', function() {

global.document ={
documentElement: {
nextSibling: {
data:'<!--Debug: cached-->'
}
}
};

assert.strictEqual(beacon._isPageCached(), true);
});

it('should return false when the page is not cached', function() {
global.document ={
documentElement: {
nextSibling: {
data:'test'
}
}
};
assert.strictEqual(beacon._isPageCached(), false);
});
});

describe('#_handleInfiniteLoop()', function() {
let saveFinalResultIntoDBSpy;

Expand Down
71 changes: 71 additions & 0 deletions test/Utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import assert from 'assert';
import sinon from 'sinon';
import BeaconUtils from '../src/Utils.js';
import node_fetch from 'node-fetch';
global.fetch = node_fetch;

describe('BeaconManager', function() {

describe('#isIntersecting', function() {
beforeEach(function () {
// Mock viewport size
global.window = {
innerWidth: 1024,
innerHeight: 768
};
});

it('should return true for a rectangle fully within the viewport', function () {
const rect = {top: 100, left: 100, bottom: 200, right: 200};
assert.strictEqual(BeaconUtils.isIntersecting(rect), true);
});

it('should return false for a rectangle entirely above the viewport', function () {
const rect = {top: -500, left: 100, bottom: -400, right: 200};
assert.strictEqual(BeaconUtils.isIntersecting(rect), false);
});

it('should return false for a rectangle entirely below the viewport', function () {
const rect = {top: 800, left: 100, bottom: 900, right: 200};
assert.strictEqual(BeaconUtils.isIntersecting(rect), false);
});

it('should return false for a rectangle entirely to the left of the viewport', function () {
const rect = {top: 100, left: -500, bottom: 200, right: -400};
assert.strictEqual(BeaconUtils.isIntersecting(rect), false);
});

it('should return false for a rectangle entirely to the right of the viewport', function () {
const rect = {top: 100, left: 1100, bottom: 200, right: 1200};
assert.strictEqual(BeaconUtils.isIntersecting(rect), false);
});
});

describe('#isPageCached', function() {

it('should return true when the page is cached', function() {

global.document ={
documentElement: {
nextSibling: {
data:'<!--Debug: cached-->'
}
}
};

assert.strictEqual(BeaconUtils.isPageCached(), true);
});

it('should return false when the page is not cached', function() {
global.document ={
documentElement: {
nextSibling: {
data:'test'
}
}
};
assert.strictEqual(BeaconUtils.isPageCached(), false);
});
});

});

0 comments on commit 7332ce4

Please sign in to comment.