Skip to content

Commit 465a76b

Browse files
ged-odoomcm-odoo
authored andcommitted
[add] add some tests for resource
1 parent 724c3c9 commit 465a76b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/resource.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { effect } from "../src";
12
import { Resource } from "../src/runtime/resource";
3+
import { waitScheduler } from "./helpers";
24

35
test("can add and get values", () => {
46
const resource = new Resource();
@@ -23,3 +25,58 @@ test("can remove values", () => {
2325
resource.remove("a").remove("d");
2426
expect(resource.items()).toEqual(["c"]);
2527
});
28+
29+
test("sequence", async () => {
30+
const resource = new Resource("r", String);
31+
32+
resource.add("a", 10);
33+
resource.add("b"); // default = 50
34+
resource.add("c", 14);
35+
resource.add("d", 100);
36+
37+
const items = resource.items;
38+
expect(items()).toEqual(["a", "c", "b", "d"]);
39+
});
40+
41+
test("items and effects", async () => {
42+
const resource: Resource<string> = new Resource();
43+
44+
resource.add("a");
45+
const items = resource.items;
46+
const steps: string[] = [];
47+
48+
effect(() => {
49+
steps.push(...items());
50+
});
51+
expect(steps).toEqual(["a"]);
52+
resource.add("b");
53+
expect(steps).toEqual(["a"]);
54+
await waitScheduler();
55+
expect(steps).toEqual(["a", "a", "b"]);
56+
});
57+
58+
test("validation schema", async () => {
59+
const resource = new Resource("test", {
60+
type: Object,
61+
shape: {
62+
blip: String,
63+
},
64+
});
65+
66+
resource.add({ blip: "asdf" });
67+
expect(() => {
68+
resource.add({ blip: 1 });
69+
}).toThrow();
70+
});
71+
72+
test("validation schema, with a class", async () => {
73+
class A {}
74+
class B {}
75+
76+
const resource = new Resource("test", { type: A });
77+
78+
resource.add(new A());
79+
expect(() => {
80+
resource.add(new B());
81+
}).toThrow();
82+
});

0 commit comments

Comments
 (0)