Skip to content

Commit

Permalink
Clean up OneByte tests
Browse files Browse the repository at this point in the history
This test-only change:

- Tests various permutations of `perByte()`
- Uses [`toBeCloseTo()`][0] (see [issue thegreenwebfoundation#98][1])

[0]: https://jestjs.io/docs/expect#tobeclosetonumber-numdigits
[1]: thegreenwebfoundation#98
  • Loading branch information
EvanHahn committed Jul 6, 2023
1 parent 98e395b commit 568b818
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/1byte.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

import OneByte from "./1byte.js";

describe("onebyte", () => {
describe("OneByte", () => {
describe("perByte", () => {
it("returns a simple average of the different networks", () => {
// we limit this to 12 figures with toFixed(12), because
// we have a recurring 333333 afterwards
// 4.88e-10 is the same as 0.000000000488
const expected_val = (0.000000000488).toFixed(12);
it("returns 0 for byte counts less than 1", () => {
const instance = new OneByte();

expect(instance.KWH_PER_BYTE_FOR_NETWORK.toFixed(12)).toBe(expected_val);
expect(instance.perByte(0)).toBe(0);
expect(instance.perByte(0.99)).toBe(0);
expect(instance.perByte(-1)).toBe(0);
});

it("returns a result for grey energy", () => {
const instance = new OneByte();

expect(instance.perByte(1000)).toBeCloseTo(0.00029, 5);
});

it("returns a result for green energy", () => {
const instance = new OneByte();

expect(instance.perByte(1000, true)).toBeCloseTo(0.00023, 5);
});
});
});

0 comments on commit 568b818

Please sign in to comment.