From 962dcd7baafd3dddebfb19616634620d5345250c Mon Sep 17 00:00:00 2001 From: Joan Leon Date: Mon, 9 Dec 2024 20:27:34 +0100 Subject: [PATCH] Add unit test to bindReporter --- src/lib/bindReporter.ts | 2 +- test/unit/bindReporter-test.js | 267 +++++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 test/unit/bindReporter-test.js diff --git a/src/lib/bindReporter.ts b/src/lib/bindReporter.ts index fa73db3c..1e05d721 100644 --- a/src/lib/bindReporter.ts +++ b/src/lib/bindReporter.ts @@ -48,7 +48,7 @@ export const bindReporter = ( // See: https://github.com/GoogleChrome/web-vitals/issues/14 if (delta || prevValue === undefined) { prevValue = metric.value; - metric.delta = delta; + metric.delta = Number(delta.toFixed(2)); metric.rating = getRating(metric.value, thresholds); callback(metric); } diff --git a/test/unit/bindReporter-test.js b/test/unit/bindReporter-test.js new file mode 100644 index 00000000..a15baf92 --- /dev/null +++ b/test/unit/bindReporter-test.js @@ -0,0 +1,267 @@ +import {describe, it} from 'node:test'; +import assert from 'assert'; +import {bindReporter} from '../../dist/modules/lib/bindReporter.js'; + +describe('bindReporter', () => { + describe('rating classification', () => { + const thresholds = [0.1, 0.25]; // CLS thresholds as example + + it('should return "good" for values below first threshold', () => { + const metric = { + name: 'CLS', + value: 0.05, + entries: [], + }; + + const reporter = bindReporter(() => {}, metric, thresholds, true); + + reporter(true); + assert.equal(metric.rating, 'good'); + }); + + it('should return "needs-improvement" for values between thresholds', () => { + const metric = { + name: 'CLS', + value: 0.15, + entries: [], + }; + + const reporter = bindReporter(() => {}, metric, thresholds, true); + + reporter(true); + assert.equal(metric.rating, 'needs-improvement'); + }); + + it('should return "poor" for values above second threshold', () => { + const metric = { + name: 'CLS', + value: 0.3, + entries: [], + }; + + const reporter = bindReporter(() => {}, metric, thresholds, true); + + reporter(true); + assert.equal(metric.rating, 'poor'); + }); + + it('should handle boundary values correctly', () => { + const metricAt1stThreshold = { + name: 'CLS', + value: 0.101, + entries: [], + }; + + const reporter1 = bindReporter( + () => {}, + metricAt1stThreshold, + thresholds, + true, + ); + + reporter1(true); + assert.equal(metricAt1stThreshold.rating, 'needs-improvement'); + + const metricAt2ndThreshold = { + name: 'CLS', + value: 0.251, + entries: [], + }; + + const reporter2 = bindReporter( + () => {}, + metricAt2ndThreshold, + thresholds, + true, + ); + + reporter2(true); + assert.equal(metricAt2ndThreshold.rating, 'poor'); + }); + }); + + describe('state management', () => { + it('should calculate delta correctly between reports', () => { + const metric = { + name: 'CLS', + value: 0.1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(true); + assert.equal(reports[0].delta, 0.1); + + metric.value = 0.15; + reporter(true); + assert.equal(reports[1].delta, 0.05); + }); + + describe('reportAllChanges behavior', () => { + it('should report all changes when reportAllChanges is true', () => { + const metric = { + name: 'CLS', + value: 0.1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(); + metric.value = 0.15; + reporter(); + metric.value = 0.2; + reporter(); + + assert.equal(reports.length, 3); + assert.equal(reports[0].value, 0.1); + assert.equal(reports[1].value, 0.15); + assert.equal(reports[2].value, 0.2); + }); + + it('should not report when value does not change', () => { + const metric = { + name: 'CLS', + value: 0.1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(); + reporter(); + reporter(); + + assert.equal(reports.length, 1); + assert.equal(reports[0].value, 0.1); + }); + + it('should only report on forceReport when reportAllChanges is false', () => { + const metric = { + name: 'CLS', + value: 0.1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + false, + ); + + reporter(); + metric.value = 0.15; + reporter(); + reporter(true); + + assert.equal(reports.length, 1); + assert.equal(reports[0].value, 0.15); + }); + }); + }); + + describe('special values handling', () => { + it('should not report negative values', () => { + const metric = { + name: 'CLS', + value: -1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(true); + assert.equal(reports.length, 0); + }); + + it('should handle zero values correctly', () => { + const metric = { + name: 'CLS', + value: 0, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(true); + assert.equal(reports.length, 1); + assert.equal(reports[0].value, 0); + assert.equal(reports[0].delta, 0); + assert.equal(reports[0].rating, 'good'); + }); + + describe('first report behavior', () => { + it('should always report first value even with zero delta', () => { + const metric = { + name: 'CLS', + value: 0, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + false, + ); + + reporter(true); + assert.equal(reports.length, 1); + assert.equal(reports[0].delta, 0); + }); + + it('should calculate correct delta for first non-zero value', () => { + const metric = { + name: 'CLS', + value: 0.1, + entries: [], + }; + const reports = []; + + const reporter = bindReporter( + (m) => reports.push({...m}), + metric, + [0.1, 0.25], + true, + ); + + reporter(true); + assert.equal(reports[0].delta, 0.1); + }); + }); + }); +});