3 changed files with 178 additions and 0 deletions
@ -0,0 +1,64 @@ |
|||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
||||
|
import { debounce } from "./debounce"; |
||||
|
|
||||
|
describe("debounce", () => { |
||||
|
beforeEach(() => { |
||||
|
vi.useFakeTimers(); |
||||
|
}); |
||||
|
|
||||
|
afterEach(() => { |
||||
|
vi.restoreAllMocks(); |
||||
|
}); |
||||
|
|
||||
|
it("delays executing the callback until after wait time has elapsed", () => { |
||||
|
const mockCallback = vi.fn(); |
||||
|
const debouncedFunction = debounce(mockCallback, 500); |
||||
|
|
||||
|
debouncedFunction(); |
||||
|
expect(mockCallback).not.toHaveBeenCalled(); |
||||
|
|
||||
|
vi.advanceTimersByTime(300); |
||||
|
expect(mockCallback).not.toHaveBeenCalled(); |
||||
|
|
||||
|
vi.advanceTimersByTime(200); |
||||
|
expect(mockCallback).toHaveBeenCalledTimes(1); |
||||
|
}); |
||||
|
|
||||
|
it("only executes the callback once if called multiple times within wait period", () => { |
||||
|
const mockCallback = vi.fn(); |
||||
|
const debouncedFunction = debounce(mockCallback, 500); |
||||
|
|
||||
|
debouncedFunction(); |
||||
|
debouncedFunction(); |
||||
|
debouncedFunction(); |
||||
|
|
||||
|
vi.advanceTimersByTime(500); |
||||
|
expect(mockCallback).toHaveBeenCalledTimes(1); |
||||
|
}); |
||||
|
|
||||
|
it("resets the timer when called again during wait period", () => { |
||||
|
const mockCallback = vi.fn(); |
||||
|
const debouncedFunction = debounce(mockCallback, 500); |
||||
|
|
||||
|
debouncedFunction(); |
||||
|
|
||||
|
vi.advanceTimersByTime(300); |
||||
|
debouncedFunction(); |
||||
|
|
||||
|
vi.advanceTimersByTime(300); |
||||
|
expect(mockCallback).not.toHaveBeenCalled(); |
||||
|
|
||||
|
vi.advanceTimersByTime(200); |
||||
|
expect(mockCallback).toHaveBeenCalledTimes(1); |
||||
|
}); |
||||
|
|
||||
|
it("passes arguments to the callback function", () => { |
||||
|
const mockCallback = vi.fn(); |
||||
|
const debouncedFunction = debounce(mockCallback, 500); |
||||
|
|
||||
|
debouncedFunction("test", 123); |
||||
|
|
||||
|
vi.advanceTimersByTime(500); |
||||
|
expect(mockCallback).toHaveBeenCalledWith("test", 123); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,70 @@ |
|||||
|
import { describe, expect, it } from "vitest"; |
||||
|
import { convertIntToIpAddress, convertIpAddressToInt } from "./ip"; |
||||
|
|
||||
|
describe("IP Address Conversion Functions", () => { |
||||
|
describe("convertIntToIpAddress", () => { |
||||
|
it("converts 0 to 0.0.0.0", () => { |
||||
|
expect(convertIntToIpAddress(0)).toBe("0.0.0.0"); |
||||
|
}); |
||||
|
|
||||
|
it("converts 16_777_343 to 127.0.0.1", () => { |
||||
|
expect(convertIntToIpAddress(16_777_343)).toBe("127.0.0.1"); |
||||
|
}); |
||||
|
|
||||
|
it("converts 16_820_416 to 192.168.0.1", () => { |
||||
|
expect(convertIntToIpAddress(16_820_416)).toBe("192.168.0.1"); |
||||
|
}); |
||||
|
|
||||
|
it("converts 4_294_967_295 to 255.255.255.255", () => { |
||||
|
expect(convertIntToIpAddress(4_294_967_295)).toBe("255.255.255.255"); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe("convertIpAddressToInt", () => { |
||||
|
it("converts 0.0.0.0 to 0", () => { |
||||
|
expect(convertIpAddressToInt("0.0.0.0")).toBe(0); |
||||
|
}); |
||||
|
|
||||
|
it("converts 127.0.0.1 to 16_777_343", () => { |
||||
|
expect(convertIpAddressToInt("127.0.0.1")).toBe(16_777_343); |
||||
|
}); |
||||
|
|
||||
|
it("converts 192.168.0.1 to 16_820_416", () => { |
||||
|
expect(convertIpAddressToInt("192.168.0.1")).toBe(16_820_416); |
||||
|
}); |
||||
|
|
||||
|
it("converts 255.255.255.255 to 4_294_967_295", () => { |
||||
|
expect(convertIpAddressToInt("255.255.255.255")).toBe(4_294_967_295); |
||||
|
}); |
||||
|
|
||||
|
it("handles non-standard formats", () => { |
||||
|
expect(convertIpAddressToInt("1.2.3.4")).toBe(67_305_985); |
||||
|
}); |
||||
|
|
||||
|
it("handles invalid IP addresses gracefully", () => { |
||||
|
expect(convertIpAddressToInt("300.1.2.3")).not.toBeNull(); |
||||
|
expect(typeof convertIpAddressToInt("300.1.2.3")).toBe("number"); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe("bidirectional conversion", () => { |
||||
|
it("can convert back and forth", () => { |
||||
|
const testIps = [ |
||||
|
"0.0.0.0", |
||||
|
"127.0.0.1", |
||||
|
"192.168.1.1", |
||||
|
"10.0.0.1", |
||||
|
"255.255.255.255", |
||||
|
]; |
||||
|
|
||||
|
for (const ip of testIps) { |
||||
|
const int = convertIpAddressToInt(ip); |
||||
|
expect(int).not.toBeNull(); |
||||
|
if (int !== null) { |
||||
|
const convertedBack = convertIntToIpAddress(int); |
||||
|
expect(convertedBack).toBe(ip); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,44 @@ |
|||||
|
import { beforeEach, describe, expect, it, vi } from "vitest"; |
||||
|
import { randId } from "./randId"; |
||||
|
|
||||
|
describe("randId", () => { |
||||
|
beforeEach(() => { |
||||
|
vi.restoreAllMocks(); |
||||
|
}); |
||||
|
|
||||
|
it("returns a number", () => { |
||||
|
const result = randId(); |
||||
|
expect(typeof result).toBe("number"); |
||||
|
}); |
||||
|
|
||||
|
it("returns an integer", () => { |
||||
|
const result = randId(); |
||||
|
expect(Number.isInteger(result)).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it("uses Math.random to generate the number", () => { |
||||
|
const mockRandom = vi.spyOn(Math, "random").mockReturnValue(0.5); |
||||
|
const result = randId(); |
||||
|
|
||||
|
expect(mockRandom).toHaveBeenCalled(); |
||||
|
expect(result).toBe(Math.floor(0.5 * 1e9)); |
||||
|
}); |
||||
|
|
||||
|
it("returns a value between 0 and 1e9 (exclusive)", () => { |
||||
|
const result = randId(); |
||||
|
expect(result).toBeGreaterThanOrEqual(0); |
||||
|
expect(result).toBeLessThan(1e9); |
||||
|
}); |
||||
|
|
||||
|
it("returns different values on subsequent calls", () => { |
||||
|
vi.spyOn(Math, "random").mockRestore(); |
||||
|
|
||||
|
const results = new Set(); |
||||
|
|
||||
|
for (let i = 0; i < 100; i++) { |
||||
|
results.add(randId()); |
||||
|
} |
||||
|
|
||||
|
expect(results.size).toBeGreaterThan(95); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue