forked from freshframework/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine_test.ts
More file actions
45 lines (39 loc) · 1.09 KB
/
define_test.ts
File metadata and controls
45 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { expect } from "@std/expect";
import { createDefine } from "./define.ts";
import { page } from "./handlers.ts";
Deno.test.ignore("createDefine", () => {
const define = createDefine<{ foo: number }>();
// Testing the types
const handlerFn = define.handlers((ctx) => {
ctx.state.foo satisfies number;
return page({ bar: true });
});
const handlerObj = define.handlers({
GET(ctx) {
ctx.state.foo satisfies number;
return page({ bar: [1, 2, 3] });
},
POST: () => {
return page({ baz: "hello" });
},
});
define.page<typeof handlerFn>(({ data }) => {
data.bar satisfies boolean;
return "page";
});
define.page<typeof handlerObj>(({ data }) => {
if ("baz" in data) {
data.baz satisfies string;
} else {
data.bar satisfies number[];
}
return "page";
});
define.middleware((ctx) => {
ctx.state.foo satisfies number;
return new Response("Hello");
});
expect(typeof define.page).toBe("function");
expect(typeof define.handlers).toBe("function");
expect(typeof define.middleware).toBe("function");
});