Skip to content
Open
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
36 changes: 36 additions & 0 deletions frontend/__tests__/elements/monkey-power.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import * as MonkeyPower from "../../src/ts/elements/monkey-power";

vi.mock("../../src/ts/config/store", () => ({
Config: { monkeyPowerLevel: "3", blindMode: false },
}));
vi.mock("../../src/ts/legacy-states/slow-timer", () => ({
get: () => false,
}));
vi.mock("../../src/ts/states/theme", () => ({
getTheme: () => ({ caret: "#fff", error: "#f00" }),
}));
vi.mock("../../src/ts/utils/debounced-animation-frame", () => ({
requestDebouncedAnimationFrame: (_id: string, cb: () => void) => cb(),
}));

describe("monkey-power", () => {
afterEach(() => {
vi.restoreAllMocks();
});

it("reset cancels the pending shake reset", async () => {
vi.spyOn(globalThis, "setTimeout").mockReturnValue(
42 as unknown as NodeJS.Timeout,
);
const clear = vi.spyOn(globalThis, "clearTimeout");
await MonkeyPower.addPower();
MonkeyPower.reset();
expect(clear).toHaveBeenCalledWith(42);
});

it("randomColor pads every channel", () => {
vi.spyOn(Math, "random").mockReturnValue(0);
expect(MonkeyPower.__testing.randomColor()).toBe("#000000");
});
});
13 changes: 8 additions & 5 deletions frontend/src/ts/elements/monkey-power.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ function render(): void {

export function reset(immediate = false): void {
if (!isSafeNumber(ctx.resetTimeOut)) return;
clearTimeout(ctx.resetTimeOut);
delete ctx.resetTimeOut;

clearTimeout(ctx.resetTimeOut);
body.setStyle({
transition: "all .25s, transform 0.8s",
transform: "translate(0,0)",
Expand All @@ -196,10 +196,11 @@ function startRender(): void {
}

function randomColor(): string {
const r = Math.floor(Math.random() * 256).toString(16);
const g = Math.floor(Math.random() * 256).toString(16);
const b = Math.floor(Math.random() * 256).toString(16);
return `#${r}${g}${b}`;
const hex = (): string =>
Math.floor(Math.random() * 256)
.toString(16)
.padStart(2, "0");
return `#${hex()}${hex()}${hex()}`;
}

/**
Expand Down Expand Up @@ -252,3 +253,5 @@ export async function addPower(good = true, extra = false): Promise<void> {
startRender();
});
}

export const __testing = { randomColor };
Loading