-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_test.ts
83 lines (72 loc) · 2.18 KB
/
create_test.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { createMachine } from "./create.ts";
function getTestMachine() {
const machine = createMachine({
type: "D",
initial: "OFF",
states: {
ON: {
on: {
toggle: () => "OFF",
},
},
OFF: {
on: {
toggle: () => ({ target: "ON" }),
},
},
},
});
return machine;
}
function getCounterMachine() {
return createMachine({
type: "ND",
context: {
count: 0,
},
initial: "empty",
states: {
empty: {
on: {
inc: ({ context }) => {
context.count++;
return "not_empty";
},
},
},
not_empty: {
on: {
inc: ({ context }) => {
context.count++;
return { target: "not_empty" };
},
dec: ({ context }) => {
context.count--;
return context.count === 0 ? "empty" : { target: "not_empty" };
},
},
},
},
});
}
Deno.test("create a machine with some initial state", () => {
const machine = getTestMachine();
assertEquals(machine.state(), "OFF");
});
Deno.test("should create a machine that can receive events to modify its state", () => {
const machine = getTestMachine();
machine.send({ event: "toggle" });
assertEquals(machine.state(), "ON");
machine.send({ event: "toggle" });
assertEquals(machine.state(), "OFF");
});
Deno.test("it should support internal context", () => {
const counterMachine = getCounterMachine();
counterMachine.send({ event: "inc" });
counterMachine.send({ event: "inc" });
counterMachine.send({ event: "dec" });
assertEquals(counterMachine.state(), "not_empty");
counterMachine.send({ event: "dec" });
assertEquals(counterMachine.state(), "empty");
});