Skip to content

Commit

Permalink
test: add test for state helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Sep 27, 2023
1 parent 17d25fc commit b6014bc
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/hono-do/tests/helper/state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Updater, defineState } from "../../src/helper/state";

describe("defineState", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let storage: any;

beforeEach(() => {
storage = {
get: vi.fn(),
put: vi.fn(),
};
});

afterEach(() => {
vi.resetAllMocks();
});

it("set infered type correctly", async () => {
const [get, set] = await defineState(storage, "key", "value");

expectTypeOf(get).toEqualTypeOf<() => Promise<string>>();
expectTypeOf(set).toEqualTypeOf<
(value: string | Updater<string>) => Promise<string>
>();
});

it("set complex type correctly", async () => {
type ComplexType =
| {
status: "success";
data: {
foo: string;
bar: number;
};
}
| {
status: "error";
message: string;
};

const [get, set] = await defineState<ComplexType>(storage, "key", {
status: "success",
data: {
foo: "foo",
bar: 1,
},
});

expectTypeOf(get).toEqualTypeOf<() => Promise<ComplexType>>();
expectTypeOf(set).toEqualTypeOf<
(value: ComplexType | Updater<ComplexType>) => Promise<ComplexType>
>();

await set({
status: "error",
message: "error",
});
});

it("should work geter", async () => {
const [get] = await defineState(storage, "key", "value");

expect(await get()).toBe("value");
expect(storage.get).toBeCalledWith("key");
expect(storage.put).toBeCalledWith("key", "value");
});

it("should work setter (value)", async () => {
const [_, set] = await defineState(storage, "key", "value");

await set("new value");
expect(storage.put).toBeCalledWith("key", "new value");
});

it("should work setter (updater)", async () => {
const [_, set] = await defineState(storage, "key", "value");

await set((prev) => `${prev} + ${prev}`);
expect(storage.put).toBeCalledWith("key", "value + value");
});
});

0 comments on commit b6014bc

Please sign in to comment.