UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

102 lines 4.38 kB
/** * @module RateLimiter */ import {} from "vitest"; import {} from "../../../rate-limiter/contracts/_module.js"; import { TimeSpan } from "../../../time-span/implementations/time-span.js"; import {} from "../../../utilities/_module.js"; /** * The `rateLimiterStorageAdapterTestSuite` function simplifies the process of testing your custom implementation of {@link IRateLimiterStorageAdapter | `IRateLimiterStorageAdapter`} with `vitest`. * * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/test-utilities"` * @group TestUtilities * @example * ```ts * import { afterEach, beforeEach, describe, expect, test } from "vitest"; * import { rateLimiterStorageAdapterTestSuite } from "@daiso-tech/core/rate-limiter/test-utilities"; * import { MemoryRateLimiterStorageAdapter } from "@daiso-tech/core/rate-limiter/memory-rate-limiter-storage-adapter"; * import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * * describe("class: MemoryRateLimiterStorageAdapter", () => { * rateLimiterStorageAdapterTestSuite({ * createAdapter: () => * new MemoryRateLimiterStorageAdapter(), * test, * beforeEach, * expect, * describe, * }); * }); * ``` */ export function rateLimiterStorageAdapterTestSuite(settings) { const { expect, test, createAdapter, describe, beforeEach } = settings; let adapter; describe("Reusable tests:", () => { beforeEach(async () => { adapter = (await createAdapter()); }); describe("method: transaction upsert / find", () => { test("Should insert item when key doesnt exists", async () => { const key = "a"; const value = "b"; const expiration = TimeSpan.fromMinutes(5).toEndDate(new Date("2026-01-01")); const data = await adapter.transaction(async (trx) => { await trx.upsert(key, value, expiration); return await trx.find(key); }); expect(data).toEqual({ state: value, expiration, }); }); test("Should update item when key exists", async () => { const key = "d"; const value = "f"; const expiration = TimeSpan.fromHours(5).toEndDate(new Date("2026-01-01")); const data = await adapter.transaction(async (trx) => { await trx.upsert("a", "b", TimeSpan.fromMinutes(5).toEndDate(new Date("2026-01-01"))); await trx.upsert(key, value, expiration); return await trx.find(key); }); expect(data).toEqual({ state: value, expiration, }); }); }); describe("method: transaction find", () => { test("Should return null when key doesnt exists", async () => { const noneExistingKey = "a"; const data = await adapter.transaction(async (trx) => { return await trx.find(noneExistingKey); }); expect(data).toBeNull(); }); }); describe("method: find", () => { test("Should return null when key doesnt exists", async () => { const noneExistingKey = "a"; const data = await adapter.find(noneExistingKey); expect(data).toBeNull(); }); }); describe("method: remove", () => { test("Should remove item when key exists", async () => { const key = "a"; const value = "b"; const expiration = TimeSpan.fromMinutes(5).toEndDate(new Date("2026-01-01")); await adapter.transaction(async (trx) => { await trx.upsert(key, value, expiration); }); await adapter.remove(key); const item = await adapter.find(key); expect(item).toBeNull(); }); }); }); } //# sourceMappingURL=rate-limiter-storage-adapter.test-suite.js.map