Skip to content

Commit b1c0679

Browse files
authored
Fix to return proper results when variables with 0 value are passed into function
2 parents 0632e7d + 4148331 commit b1c0679

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/co2.test.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ describe("co2", () => {
947947
});
948948

949949
describe("Using values equal to 0", () => {
950-
const co2 = new CO2();
950+
const co2 = new CO2({ results: "segment" });
951951

952952
it("expects perByteTrace to support values equal to 0", () => {
953953
const perByteTraceResult = co2.perByteTrace(1000000, false, {
@@ -986,5 +986,77 @@ describe("co2", () => {
986986
expect(network).toBe(0);
987987
expect(device).toBe(0);
988988
});
989+
it("expects perByteTrace segments to be 0 when grid intensity is 0", () => {
990+
const perByteTraceResult = co2.perByteTrace(1000000, false, {
991+
gridIntensity: {
992+
dataCenter: 0,
993+
network: 0,
994+
device: 0,
995+
},
996+
});
997+
const co2Result = perByteTraceResult.co2;
998+
// Less than 0.1 because there's still a small production component that's calculated
999+
expect(co2Result.total).toBeLessThan(0.1);
1000+
expect(co2Result["dataCenterCO2"]).toBe(0);
1001+
expect(co2Result["networkCO2"]).toBe(0);
1002+
expect(co2Result["consumerDeviceCO2"]).toBe(0);
1003+
});
1004+
1005+
it("expects perVisitTrace segments to be 0 when grid intensity is 0", () => {
1006+
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
1007+
gridIntensity: {
1008+
dataCenter: 0,
1009+
network: 0,
1010+
device: 0,
1011+
},
1012+
});
1013+
const co2Result = perVisitTraceResult.co2;
1014+
const { dataCenter, network, device } =
1015+
perVisitTraceResult.variables.gridIntensity;
1016+
// Less than 0.1 because there's still a small production component that's calculated
1017+
expect(co2Result.total).toBeLessThan(0.1);
1018+
expect(co2Result["dataCenterCO2 - first"]).toBe(0);
1019+
expect(co2Result["networkCO2 - first"]).toBe(0);
1020+
expect(co2Result["consumerDeviceCO2 - first"]).toBe(0);
1021+
expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0);
1022+
expect(co2Result["networkCO2 - subsequent"]).toBe(0);
1023+
expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0);
1024+
});
1025+
1026+
it("expects perVisitTrace segments to be 0 when there are 0 first and returning visitors", () => {
1027+
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
1028+
firstVisitPercentage: 0,
1029+
returnVisitPercentage: 0,
1030+
});
1031+
const co2Result = perVisitTraceResult.co2;
1032+
// Less than 0.1 because there's still a small production component that's calculated
1033+
expect(co2Result.total).toBe(0);
1034+
});
1035+
1036+
it("expects perVisitTrace subsequent segments to be 0 when returning visitors is 0", () => {
1037+
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
1038+
firstVisitPercentage: 100,
1039+
returnVisitPercentage: 0,
1040+
});
1041+
const co2Result = perVisitTraceResult.co2;
1042+
expect(co2Result["dataCenterCO2 - first"]).toBeGreaterThan(0);
1043+
expect(co2Result["networkCO2 - first"]).toBeGreaterThan(0);
1044+
expect(co2Result["consumerDeviceCO2 - first"]).toBeGreaterThan(0);
1045+
expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0);
1046+
expect(co2Result["networkCO2 - subsequent"]).toBe(0);
1047+
expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0);
1048+
});
1049+
it("expects perVisitTrace subsequent segments to be 0 when data reload is 0", () => {
1050+
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
1051+
dataReloadRatio: 0,
1052+
});
1053+
const co2Result = perVisitTraceResult.co2;
1054+
expect(co2Result["dataCenterCO2 - first"]).toBeGreaterThan(0);
1055+
expect(co2Result["networkCO2 - first"]).toBeGreaterThan(0);
1056+
expect(co2Result["consumerDeviceCO2 - first"]).toBeGreaterThan(0);
1057+
expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0);
1058+
expect(co2Result["networkCO2 - subsequent"]).toBe(0);
1059+
expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0);
1060+
});
9891061
});
9901062
});

src/sustainable-web-design.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ class SustainableWebDesign {
7070
if (options?.gridIntensity) {
7171
const { device, network, dataCenter } = options.gridIntensity;
7272

73-
if (device?.value) {
73+
if (device?.value || device?.value === 0) {
7474
deviceCarbonIntensity = device.value;
7575
}
76-
if (network?.value) {
76+
if (network?.value || network?.value === 0) {
7777
networkCarbonIntensity = network.value;
7878
}
7979
// If the user has set a carbon intensity value for the datacentre, then that overrides everything and is used
80-
if (dataCenter?.value) {
80+
if (dataCenter?.value || dataCenter?.value === 0) {
8181
dataCenterCarbonIntensity = dataCenter.value;
8282
}
8383
}
@@ -245,15 +245,15 @@ class SustainableWebDesign {
245245
returnView = RETURNING_VISITOR_PERCENTAGE,
246246
dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD
247247
) {
248-
if (options.dataReloadRatio) {
248+
if (options.dataReloadRatio || options.dataReloadRatio === 0) {
249249
dataReloadRatio = options.dataReloadRatio;
250250
}
251251

252-
if (options.firstVisitPercentage) {
252+
if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {
253253
firstView = options.firstVisitPercentage;
254254
}
255255

256-
if (options.returnVisitPercentage) {
256+
if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {
257257
returnView = options.returnVisitPercentage;
258258
}
259259

0 commit comments

Comments
 (0)