diff --git a/jest.config.js b/jest.config.js index 6d422a3..cf6470b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,5 +2,6 @@ module.exports = { testEnvironment: "jsdom", moduleNameMapper: { "\\.(css)$": "/src/__mocks__/styleMock.js", + "^wasm-lib$": "/src/__mocks__/wasm-lib.js", }, }; diff --git a/src/__mocks__/wasm-lib.js b/src/__mocks__/wasm-lib.js new file mode 100644 index 0000000..d8df88f --- /dev/null +++ b/src/__mocks__/wasm-lib.js @@ -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 }; diff --git a/src/utils/colour.test.ts b/src/utils/colour.test.ts new file mode 100644 index 0000000..78f6a0f --- /dev/null +++ b/src/utils/colour.test.ts @@ -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"); + }); +}); diff --git a/src/utils/conversions.test.ts b/src/utils/conversions.test.ts new file mode 100644 index 0000000..c426d31 --- /dev/null +++ b/src/utils/conversions.test.ts @@ -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 }); + }); +}); diff --git a/src/utils/typeguard.test.ts b/src/utils/typeguard.test.ts new file mode 100644 index 0000000..b9f3cf6 --- /dev/null +++ b/src/utils/typeguard.test.ts @@ -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" })); + }); +});