Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up SustainableWebDesign model perByte tests #157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/co2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,6 @@ describe("co2", () => {
);
});

describe("perByte", () => {
it("returns a CO2 number for data transfer", () => {
co2.perByte(MILLION);
expect(co2.perByte(MILLION).toFixed(5)).toBe(MILLION_GREY.toFixed(5));
});

it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
expect(co2.perByte(MILLION, false).toFixed(5)).toBe(
MILLION_GREY.toFixed(5)
);

expect(co2.perByte(MILLION, true).toFixed(5)).toBe(
MILLION_GREEN.toFixed(5)
);
});
});

describe("perVisit", () => {
it("returns a CO2 number for data transfer per visit with caching assumptions from the Sustainable Web Design model", () => {
co2.perVisit(MILLION);
Expand Down
4 changes: 4 additions & 0 deletions src/sustainable-web-design.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ class SustainableWebDesign {
segmentResults = false,
options = {}
) {
if (bytes < 1) {
bytes = 0;
}

const energyBycomponent = this.energyPerByteByComponent(bytes, options);

// otherwise when faced with non numeric values throw an error
Expand Down
32 changes: 30 additions & 2 deletions src/sustainable-web-design.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SustainableWebDesign from "./sustainable-web-design.js";
import { MILLION, SWD } from "./constants/test-constants.js";

describe("sustainable web design model", () => {
const swd = new SustainableWebDesign();
Expand Down Expand Up @@ -26,8 +27,35 @@ describe("sustainable web design model", () => {
});

describe("perByte", () => {
it("should return a single number for CO2 emissions", () => {
expect(typeof swd.perByte(2257715.2)).toBe("number");
it("returns 0 for byte counts less than 1", () => {
expect(swd.perByte(0)).toBe(0);
expect(swd.perByte(0.99)).toBe(0);
expect(swd.perByte(-1)).toBe(0);

const segmented = swd.perByte(0.5, false, true);
expect(segmented.dataCenterCO2).toBe(0);
expect(segmented.consumerDeviceCO2).toBe(0);
expect(segmented.networkCO2).toBe(0);
expect(segmented.productionCO2).toBe(0);
expect(segmented.total).toBe(0);
});

it("returns a result for grey energy", () => {
expect(swd.perByte(MILLION)).toBeCloseTo(SWD.MILLION_GREY, 5);
});

it("returns a result for green energy", () => {
expect(swd.perByte(MILLION, true)).toBeCloseTo(SWD.MILLION_GREEN, 5);
});

it("can segment results", () => {
const result = swd.perByte(MILLION, false, true);

expect(result.dataCenterCO2).toBeCloseTo(0.05301, 5);
expect(result.consumerDeviceCO2).toBeCloseTo(0.18378, 5);
expect(result.networkCO2).toBeCloseTo(0.04948, 5);
expect(result.productionCO2).toBeCloseTo(0.06715, 5);
expect(result.total).toBeCloseTo(SWD.MILLION_GREY, 5);
});
});

Expand Down