Initial commit
This commit is contained in:
55
packages/core-analytics/src/with-analytics.test.ts
Normal file
55
packages/core-analytics/src/with-analytics.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { describe, it, expect, expectTypeOf } from "vitest";
|
||||
import { withAnalytics, type Analyzed } from "@/with-analytics";
|
||||
import type { IAnalytics } from "@/analytics.interface";
|
||||
import { isAnalyzed } from "@repo/core-shared/conformance";
|
||||
|
||||
function makeAnalytics(): IAnalytics {
|
||||
return {
|
||||
track: () => undefined,
|
||||
identify: () => undefined,
|
||||
pageView: () => undefined,
|
||||
flush: () => Promise.resolve(),
|
||||
};
|
||||
}
|
||||
|
||||
describe("withAnalytics", () => {
|
||||
it("returns an Analyzed<F>", () => {
|
||||
const analytics = makeAnalytics();
|
||||
const fn = async (_input: { id: string }) => ({ ok: true });
|
||||
const wrapped = withAnalytics(analytics, fn);
|
||||
expectTypeOf(wrapped).toMatchTypeOf<Analyzed<typeof fn>>();
|
||||
});
|
||||
|
||||
it("attaches __analyzed as a non-enumerable property on the wrapped function", () => {
|
||||
const analytics = makeAnalytics();
|
||||
const fn = async () => ({ ok: true });
|
||||
const wrapped = withAnalytics(analytics, fn);
|
||||
expect(isAnalyzed(wrapped)).toBe(true);
|
||||
expect(Object.keys(wrapped)).not.toContain("__analyzed");
|
||||
});
|
||||
|
||||
it("does NOT pollute the original input function with the brand", () => {
|
||||
const analytics = makeAnalytics();
|
||||
const fn = async () => ({ ok: true });
|
||||
const wrapped = withAnalytics(analytics, fn);
|
||||
expect(isAnalyzed(fn)).toBe(false);
|
||||
expect(wrapped).not.toBe(fn);
|
||||
});
|
||||
|
||||
it("passes input and output through unchanged", async () => {
|
||||
const analytics = makeAnalytics();
|
||||
const fn = async (input: { id: string }) => ({ ok: true, id: input.id });
|
||||
const wrapped = withAnalytics(analytics, fn);
|
||||
const result = await wrapped({ id: "abc" });
|
||||
expect(result).toEqual({ ok: true, id: "abc" });
|
||||
});
|
||||
|
||||
it("propagates errors", async () => {
|
||||
const analytics = makeAnalytics();
|
||||
const err = new Error("boom");
|
||||
const wrapped = withAnalytics(analytics, async () => {
|
||||
throw err;
|
||||
});
|
||||
await expect(wrapped()).rejects.toBe(err);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user