From e5a6ea46c1ebda240d5b90368fdc03d6c880f162 Mon Sep 17 00:00:00 2001 From: Philip Walton Date: Thu, 26 Dec 2024 12:34:36 +0300 Subject: [PATCH] Add tests for FCP --- test/e2e/onFCP-test.js | 29 +++++++++++++++++++++++++++++ test/views/fcp.njk | 20 +++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/test/e2e/onFCP-test.js b/test/e2e/onFCP-test.js index 3757400b..d346d694 100644 --- a/test/e2e/onFCP-test.js +++ b/test/e2e/onFCP-test.js @@ -267,6 +267,35 @@ describe('onFCP()', async function () { assert.strictEqual(fcp.navigationType, 'restore'); }); + it('works when calling the function twice with different options', async function () { + if (!browserSupportsFCP) this.skip(); + + await navigateTo('/test/fcp?doubleCall=1&reportAllChanges2=1'); + + await beaconCountIs(1, {instance: 1}); + await beaconCountIs(1, {instance: 2}); + + const [fcp1] = await getBeacons({instance: 1}); + const [fcp2] = await getBeacons({instance: 2}); + + assert(fcp1.value >= 0); + assert(fcp1.id.match(/^v5-\d+-\d+$/)); + assert.strictEqual(fcp1.name, 'FCP'); + assert.strictEqual(fcp1.value, fcp1.delta); + assert.strictEqual(fcp1.rating, 'good'); + assert.strictEqual(fcp1.entries.length, 1); + assert.match(fcp1.navigationType, /navigate|reload/); + + assert(fcp2.id.match(/^v5-\d+-\d+$/)); + assert(fcp2.id !== fcp1.id); + assert.strictEqual(fcp2.value, fcp1.value); + assert.strictEqual(fcp2.delta, fcp1.delta); + assert.strictEqual(fcp2.name, fcp1.name); + assert.strictEqual(fcp2.rating, fcp1.rating); + assert.strictEqual(fcp2.entries.length, fcp1.entries.length); + assert.strictEqual(fcp2.navigationType, fcp1.navigationType); + }); + describe('attribution', function () { it('includes attribution data on the metric object', async function () { if (!browserSupportsFCP) this.skip(); diff --git a/test/views/fcp.njk b/test/views/fcp.njk index 3e1be0b8..5e9d3f02 100644 --- a/test/views/fcp.njk +++ b/test/views/fcp.njk @@ -31,11 +31,29 @@ const {onFCP} = await __testImport('{{ modulePath }}'); onFCP((fcp) => { + fcp.instance = 1; + // Log for easier manual testing. console.log(fcp); // Test sending the metric to an analytics endpoint. navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(fcp))); - }, {reportAllChanges: self.__reportAllChanges}); + }, { + reportAllChanges: self.__reportAllChanges, + }); + + if (self.__doubleCall) { + onFCP((fcp) => { + fcp.instance = 2; + + // Log for easier manual testing. + console.log(fcp); + + // Test sending the metric to an analytics endpoint. + navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(fcp))); + }, { + reportAllChanges: self.__reportAllChanges2, + }); + } {% endblock %}