-
Notifications
You must be signed in to change notification settings - Fork 640
/
TimeTraveller.test.ts
102 lines (84 loc) · 2.26 KB
/
TimeTraveller.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { TimeTraveller } from "../src"
import { types, clone } from "mobx-state-tree"
const TestModel = types
.model({
x: 1
})
.actions(self => ({
inc() {
self.x += 1
}
}))
test("it can time travel", () => {
const m = TestModel.create()
const tt = TimeTraveller.create({}, { targetStore: m })
expect(tt.canUndo).toBe(false)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(1)
m.inc()
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(2)
m.inc()
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(3)
tt.undo()
expect(m.x).toBe(2)
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(true)
tt.undo()
expect(m.x).toBe(1)
expect(tt.canUndo).toBe(false)
expect(tt.canRedo).toBe(true)
tt.redo()
expect(m.x).toBe(2)
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(true)
// resets 'future'
m.inc()
expect(m.x).toBe(3)
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(false)
})
test("it can time travel same store and persist state", () => {
const W = types.model({
model: types.optional(TestModel, {}),
traveler: types.optional(TimeTraveller, { targetPath: "../model" })
})
const w = W.create()
const m = w.model
const tt = w.traveler
expect(tt.canUndo).toBe(false)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(1)
m.inc()
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(2)
m.inc()
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(false)
expect(m.x).toBe(3)
tt.undo()
expect(m.x).toBe(2)
expect(tt.canUndo).toBe(true)
expect(tt.canRedo).toBe(true)
// Clone of the store should inherit the same state!
const w2 = clone(w)
const m2 = w2.model
const tt2 = w2.traveler
tt2.undo()
expect(m2.x).toBe(1)
expect(tt2.canUndo).toBe(false)
expect(tt2.canRedo).toBe(true)
tt2.redo()
expect(m2.x).toBe(2)
expect(tt2.canUndo).toBe(true)
expect(tt2.canRedo).toBe(true)
// resets 'future'
m2.inc()
expect(m2.x).toBe(3)
expect(tt2.canUndo).toBe(true)
expect(tt2.canRedo).toBe(false)
})