| import { randomID, waitForCondition } from "./MiscUtils"; |
|
|
| describe("randomID", () => { |
| test("should generate a string with the correct format", () => { |
| const id = randomID(); |
| expect(id).toMatch(/^id_[a-z\d]+$/); |
| }); |
|
|
| test("should generate a string with default length", () => { |
| const id = randomID(); |
| |
| expect(id.length).toBeGreaterThanOrEqual(3); |
| |
| }); |
|
|
| test("should generate a string with custom length", () => { |
| const customLength = 5; |
| const id = randomID(customLength); |
| |
| expect(id.length).toBeGreaterThanOrEqual(3 + customLength - 10); |
| expect(id.length).toBeLessThanOrEqual(3 + customLength + 2); |
| }); |
|
|
| test("should generate unique IDs when called multiple times", () => { |
| const ids = new Set(); |
| const count = 1000; |
|
|
| for (let i = 0; i < count; i++) { |
| ids.add(randomID()); |
| } |
|
|
| |
| expect(ids.size).toBe(count); |
| }); |
| }); |
|
|
| describe("waitForCondition", () => { |
| beforeEach(() => { |
| jest.useFakeTimers(); |
| }); |
|
|
| afterEach(() => { |
| jest.useRealTimers(); |
| }); |
|
|
| test("should resolve when condition becomes true", async () => { |
| let conditionValue = false; |
|
|
| const promise = waitForCondition(() => conditionValue); |
|
|
| |
| jest.advanceTimersByTime(100); |
| expect(jest.getTimerCount()).toBe(1); |
|
|
| |
| conditionValue = true; |
|
|
| |
| jest.advanceTimersByTime(100); |
|
|
| |
| jest.runAllTimers(); |
|
|
| |
| await Promise.resolve(); |
|
|
| |
| await promise; |
|
|
| |
| expect(jest.getTimerCount()).toBe(0); |
| }); |
|
|
| test("should check condition at specified frequency", async () => { |
| let checkCount = 0; |
| const customFrequency = 200; |
|
|
| const conditionFunc = jest.fn(() => { |
| checkCount++; |
| return checkCount >= 3; |
| }); |
|
|
| const promise = waitForCondition(conditionFunc, customFrequency); |
|
|
| |
| expect(conditionFunc).toHaveBeenCalledTimes(1); |
|
|
| |
| jest.advanceTimersByTime(customFrequency); |
| expect(conditionFunc).toHaveBeenCalledTimes(2); |
|
|
| |
| jest.advanceTimersByTime(customFrequency); |
|
|
| |
| jest.runAllTimers(); |
|
|
| |
| await Promise.resolve(); |
|
|
| |
| await promise; |
|
|
| expect(conditionFunc).toHaveBeenCalledTimes(3); |
| |
| expect(jest.getTimerCount()).toBe(0); |
| }); |
|
|
| test("should use default check frequency when not specified", () => { |
| const conditionFunc = jest.fn(() => false); |
|
|
| waitForCondition(conditionFunc); |
|
|
| |
| expect(conditionFunc).toHaveBeenCalledTimes(1); |
|
|
| |
| jest.advanceTimersByTime(99); |
| expect(conditionFunc).toHaveBeenCalledTimes(1); |
|
|
| |
| jest.advanceTimersByTime(1); |
| expect(conditionFunc).toHaveBeenCalledTimes(2); |
| }); |
|
|
| test("should resolve immediately if condition is initially true", async () => { |
| const conditionFunc = jest.fn(() => true); |
|
|
| const promise = waitForCondition(conditionFunc); |
|
|
| |
| |
| jest.runOnlyPendingTimers(); |
|
|
| |
| await Promise.resolve(); |
|
|
| |
| jest.runAllTimers(); |
|
|
| |
| await promise; |
|
|
| expect(conditionFunc).toHaveBeenCalledTimes(1); |
| expect(jest.getTimerCount()).toBe(0); |
| }); |
| }); |
|
|