Skip to content
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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
testEnvironment: "jsdom",
moduleNameMapper: {
"\\.(css)$": "<rootDir>/src/__mocks__/styleMock.js",
"^wasm-lib$": "<rootDir>/src/__mocks__/wasm-lib.js",
},
};
17 changes: 17 additions & 0 deletions src/__mocks__/wasm-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Vec3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}

class Rgb {
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
}

module.exports = { Vec3, Rgb };
38 changes: 38 additions & 0 deletions src/utils/colour.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { rgb, rgbToVec3, vec3ToRGB, RGBToHex } from "./colour";
import { vec3 } from "./math";

describe("rgb", () => {
it("creates an RGB value with the given components", () => {
expect(rgb(255, 128, 0)).toEqual({ r: 255, g: 128, b: 0 });
});
});

describe("rgbToVec3", () => {
it("converts RGB to a vec3", () => {
expect(rgbToVec3(rgb(1, 2, 3))).toEqual(vec3(1, 2, 3));
});
});

describe("vec3ToRGB", () => {
it("converts a vec3 to RGB", () => {
expect(vec3ToRGB(vec3(1, 2, 3))).toEqual(rgb(1, 2, 3));
});
});

describe("RGBToHex", () => {
it("converts black", () => {
expect(RGBToHex(rgb(0, 0, 0))).toBe("#000000");
});

it("converts white", () => {
expect(RGBToHex(rgb(255, 255, 255))).toBe("#ffffff");
});

it("converts red", () => {
expect(RGBToHex(rgb(255, 0, 0))).toBe("#ff0000");
});

it("converts a mixed colour", () => {
expect(RGBToHex(rgb(18, 52, 86))).toBe("#123456");
});
});
17 changes: 17 additions & 0 deletions src/utils/conversions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { wasmVec3, wasmRGB } from "./conversions";
import { vec3 } from "./math";
import { rgb } from "./colour";

describe("wasmVec3", () => {
it("passes the correct components to the WASM constructor", () => {
const result = wasmVec3(vec3(1, 2, 3));
expect(result).toMatchObject({ x: 1, y: 2, z: 3 });
});
});

describe("wasmRGB", () => {
it("passes the correct components to the WASM constructor", () => {
const result = wasmRGB(rgb(255, 128, 0));
expect(result).toMatchObject({ r: 255, g: 128, b: 0 });
});
});
11 changes: 11 additions & 0 deletions src/utils/typeguard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { exhaustiveMatchGuard } from "./typeguard";

describe("exhaustiveMatchGuard", () => {
it("throws an error with the unexpected value", () => {
expect(() => exhaustiveMatchGuard("unexpected" as never)).toThrow("Unexpected value");
});

it("includes the value in the error message", () => {
expect(() => exhaustiveMatchGuard({ foo: "bar" } as never)).toThrow(JSON.stringify({ foo: "bar" }));
});
});
Loading