Files
OpenFrontIO/tests/client/graphics/ProgressBar.test.ts
T
Wraith 26f5d40819 build: migrate build system to Vite and test runner to Vitest & Remove depracated husky usage (#2703)
- Replace Webpack with Vite for faster client bundling and HMR.
- Migrate tests from Jest to Vitest and update configuration.
- Update Web Worker instantiation to standard ESM syntax.
- Implement Env utility in `src/core` for safe, hybrid environment
variable access (Vite vs Node).
- Refactor configuration loaders to remove direct `process.env`
dependencies in shared code.
- Update TypeScript environment definitions and project scripts for the
new toolchain.
- Remove the [depracated usage of the
husky](https://github.com/typicode/husky/releases/tag/v9.0.1).

## Description:

migrate build system to Vite and test runner to Vitest & Remove
depracated husky usage

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [ ] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

wraith4081

---------

Co-authored-by: evanpelle <evanpelle@gmail.com>
2025-12-28 22:10:26 -08:00

56 lines
1.9 KiB
TypeScript

import { ProgressBar } from "../../../src/client/graphics/ProgressBar";
describe("ProgressBar", () => {
let ctx: CanvasRenderingContext2D;
let canvas: HTMLCanvasElement;
beforeEach(() => {
canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 20;
ctx = canvas.getContext("2d")!;
});
it("should initialize and draw the background", () => {
const spyClearRect = vi.spyOn(ctx, "clearRect");
const spyFillRect = vi.spyOn(ctx, "fillRect");
const spyFillStyle = vi.spyOn(ctx, "fillStyle", "set");
const bar = new ProgressBar(["#ff0000", "#00ff00"], ctx, 2, 2, 80, 10, 0.5);
expect(spyClearRect).toHaveBeenCalledWith(0, 0, 82, 12);
expect(spyFillRect).toHaveBeenCalledWith(1, 1, 80, 10);
expect(spyFillStyle).toHaveBeenCalledWith("#00ff00");
expect(bar.getX()).toBe(2);
expect(bar.getY()).toBe(2);
});
it("should set progress and draw the progress bar", () => {
const bar = new ProgressBar(["#ff0000", "#00ff00"], ctx, 2, 2, 80, 10);
const spyFillRect = vi.spyOn(ctx, "fillRect");
bar.setProgress(0.5);
expect(bar.getProgress()).toBe(0.5);
expect(spyFillRect).toHaveBeenCalledWith(
2,
2,
Math.floor(0.5 * (80 - 2)),
8,
);
expect(ctx.fillStyle).toBe("#00ff00");
bar.setProgress(0.1);
expect(ctx.fillStyle).toBe("#ff0000");
});
it("should clamp progress between 0 and 1 on init", () => {
const bar = new ProgressBar(["#ff0000", "#00ff00"], ctx, 2, 2, 80, 10, -1);
expect(bar.getProgress()).toBe(0);
const bar2 = new ProgressBar(["#ff0000", "#00ff00"], ctx, 2, 2, 80, 10, 2);
expect(bar2.getProgress()).toBe(1);
});
it("should handle empty colors array gracefully", () => {
const bar = new ProgressBar([], ctx, 2, 2, 80, 10, 0.5);
expect(() => bar.setProgress(0.5)).not.toThrow();
expect(ctx.fillStyle).toBe("#808080");
});
});