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.

113 lines 5.14 kB
/** * @module CircuitBreaker */ import {} from "vitest"; import {} from "../../../circuit-breaker/contracts/_module.js"; import {} from "../../../execution-context/contracts/_module.js"; import { NoOpExecutionContextAdapter } from "../../../execution-context/implementations/adapters/no-op-execution-context-adapter/_module.js"; import { ExecutionContext } from "../../../execution-context/implementations/derivables/_module.js"; import {} from "../../../utilities/_module.js"; /** * The `circuitBreakerStorageAdapterTestSuite` function simplifies the process of testing your custom implementation of {@link ICircuitBreakerStorageAdapter | `ICircuitBreakerStorageAdapter`} with `vitest`. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/test-utilities"` * @group TestUtilities * @example * ```ts * import { afterEach, beforeEach, describe, expect, test } from "vitest"; * import { circuitBreakerStorageAdapterTestSuite } from "@daiso-tech/core/circuit-breaker/test-utilities"; * import { MemoryCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/memory-circuit-breaker-storage-adapter"; * import { TimeSpan } 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: MemoryCircuitBreakerStorageAdapter", () => { * circuitBreakerStorageAdapterTestSuite({ * createAdapter: () => * new MemoryCircuitBreakerStorageAdapter(), * test, * beforeEach, * expect, * describe, * }); * }); * ``` */ export function circuitBreakerStorageAdapterTestSuite(settings) { const { expect, test, createAdapter, describe, beforeEach: beforeEach_, context = new ExecutionContext(new NoOpExecutionContextAdapter()), } = settings; let adapter; describe("ICircuitBreakerStorageAdapter tests:", () => { beforeEach_(async () => { adapter = (await createAdapter()); }); describe("method: transaction upsert", () => { test("Should add key when doesnt exists", async () => { const key = "a"; const input = "b"; await adapter.transaction(context, async (trx) => { await trx.upsert(context, key, input); }); const value = await adapter.find(context, key); expect(value).toBe(input); }); test("Should update key when exists", async () => { const key = "a"; const input1 = "b"; const input2 = "c"; await adapter.transaction(context, async (trx) => { await trx.upsert(context, key, input1); await trx.upsert(context, key, input2); }); const value = await adapter.find(context, key); expect(value).toBe(input2); }); }); describe("method: transaction find", () => { test("Should return null when key doesnt exists", async () => { const noneExistingKey = "a"; const value = await adapter.transaction(context, async (trx) => { return await trx.find(context, noneExistingKey); }); expect(value).toBeNull(); }); test("Should return the inserted value when key exists", async () => { const key = "a"; const input = "b"; const value = await adapter.transaction(context, async (trx) => { await trx.upsert(context, key, input); return await trx.find(context, key); }); expect(value).toBe(input); }); }); describe("method: find", () => { test("Should return null when key doesnt exists", async () => { const noneExistingKey = "a"; const value = await adapter.find(context, noneExistingKey); expect(value).toBeNull(); }); test("Should return the inserted value when key exists", async () => { const key = "a"; const input = "b"; await adapter.transaction(context, async (trx) => { await trx.upsert(context, key, input); }); const value = await adapter.find(context, key); expect(value).toBe(input); }); }); describe("method: remove", () => { test("Should remove key when exists", async () => { const key = "a"; await adapter.transaction(context, async (trx) => { await trx.upsert(context, key, "value"); }); await adapter.remove(context, key); const value = await adapter.find(context, key); expect(value).toBeNull(); }); }); }); } //# sourceMappingURL=circuit-breaker-storage-adapter.test-suite.js.map