Skip to content
Merged
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
32 changes: 32 additions & 0 deletions test/limit-number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// app/store/config.ts participates in an import cycle, so a static import can
// hit a temporal-dead-zone error. Load it dynamically in beforeAll instead.
let limitNumber: (x: number, min: number, max: number, def: number) => number;

beforeAll(async () => {
// Warm up the import cycle in a working order before loading the target.
await import("../app/client/api");
({ limitNumber } = await import("../app/store/config"));
});

describe("limitNumber", () => {
test("returns the default value for NaN input", () => {
expect(limitNumber(NaN, 0, 10, 5)).toBe(5);
});

test("clamps values below the minimum up to the minimum", () => {
expect(limitNumber(-3, 0, 10, 5)).toBe(0);
});

test("clamps values above the maximum down to the maximum", () => {
expect(limitNumber(99, 0, 10, 5)).toBe(10);
});

test("returns the value unchanged when it is within range", () => {
expect(limitNumber(7, 0, 10, 5)).toBe(7);
});

test("treats the min and max boundaries as inclusive", () => {
expect(limitNumber(0, 0, 10, 5)).toBe(0);
expect(limitNumber(10, 0, 10, 5)).toBe(10);
});
});
Loading