Skip to content

Commit

Permalink
Add tests for FCP
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Dec 26, 2024
1 parent bec4727 commit e5a6ea4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
29 changes: 29 additions & 0 deletions test/e2e/onFCP-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 19 additions & 1 deletion test/views/fcp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
</script>
{% endblock %}

0 comments on commit e5a6ea4

Please sign in to comment.