forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-api.js
More file actions
219 lines (182 loc) · 5.88 KB
/
Copy pathobject-api.js
File metadata and controls
219 lines (182 loc) · 5.88 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const mobx = require("../../src/mobx")
const { keys, when, set, remove, values, reaction, observable, extendObservable, has, get } = mobx
test("keys should be observable when extending", () => {
const todos = observable({})
const todoTitles = []
reaction(
() => keys(todos).map(key => `${key}: ${todos[key]}`),
titles => todoTitles.push(titles.join(","))
)
mobx.set(todos, {
lewis: "Read Lewis",
chesterton: "Be mind blown by Chesterton"
})
expect(todoTitles).toEqual(["lewis: Read Lewis,chesterton: Be mind blown by Chesterton"])
mobx.set(todos, { lewis: "Read Lewis twice" })
mobx.set(todos, { coffee: "Grab coffee" })
expect(todoTitles).toEqual([
"lewis: Read Lewis,chesterton: Be mind blown by Chesterton",
"lewis: Read Lewis twice,chesterton: Be mind blown by Chesterton",
"lewis: Read Lewis twice,chesterton: Be mind blown by Chesterton,coffee: Grab coffee"
])
})
test("toJS respects key changes", () => {
const todos = observable({})
const serialized = []
mobx.autorun(() => {
serialized.push(JSON.stringify(mobx.toJS(todos)))
})
mobx.set(todos, {
lewis: "Read Lewis",
chesterton: "Be mind blown by Chesterton"
})
mobx.set(todos, { lewis: "Read Lewis twice" })
mobx.set(todos, { coffee: "Grab coffee" })
expect(serialized).toEqual([
"{}",
'{"lewis":"Read Lewis","chesterton":"Be mind blown by Chesterton"}',
'{"lewis":"Read Lewis twice","chesterton":"Be mind blown by Chesterton"}',
'{"lewis":"Read Lewis twice","chesterton":"Be mind blown by Chesterton","coffee":"Grab coffee"}'
])
})
test("object - set, remove, values are reactive", () => {
const todos = observable({})
const snapshots = []
reaction(() => values(todos), values => snapshots.push(values))
expect(has(todos, "x")).toBe(false)
expect(get(todos, "x")).toBe(undefined)
set(todos, "x", 3)
expect(has(todos, "x")).toBe(true)
expect(get(todos, "x")).toBe(3)
remove(todos, "y")
set(todos, "z", 4)
set(todos, "x", 5)
remove(todos, "z")
expect(snapshots).toEqual([[3], [3, 4], [5, 4], [5]])
})
test("object - set, remove, keys are reactive", () => {
const todos = observable({ a: 3 })
const snapshots = []
reaction(() => keys(todos), keys => snapshots.push(keys))
set(todos, "x", 3)
remove(todos, "y")
set(todos, "z", 4)
set(todos, "x", 5)
remove(todos, "z")
remove(todos, "a")
expect(snapshots).toEqual([["a", "x"], ["a", "x", "z"], ["a", "x"], ["x"]])
})
test("map - set, remove, values are reactive", () => {
const todos = observable.map({})
const snapshots = []
reaction(() => values(todos), values => snapshots.push(values))
expect(has(todos, "x")).toBe(false)
expect(get(todos, "x")).toBe(undefined)
set(todos, "x", 3)
expect(has(todos, "x")).toBe(true)
expect(get(todos, "x")).toBe(3)
remove(todos, "y")
set(todos, "z", 4)
set(todos, "x", 5)
remove(todos, "z")
expect(snapshots).toEqual([[3], [3, 4], [5, 4], [5]])
})
test("map - set, remove, keys are reactive", () => {
const todos = observable.map({ a: 3 })
const snapshots = []
reaction(() => keys(todos), keys => snapshots.push(keys))
set(todos, "x", 3)
remove(todos, "y")
set(todos, "z", 4)
set(todos, "x", 5)
remove(todos, "z")
remove(todos, "a")
expect(snapshots).toEqual([["a", "x"], ["a", "x", "z"], ["a", "x"], ["x"]])
})
test("array - set, remove, values are reactive", () => {
const todos = observable.array()
const snapshots = []
reaction(() => values(todos), values => snapshots.push(values))
expect(has(todos, 0)).toBe(false)
expect(get(todos, 0)).toBe(undefined)
set(todos, 0, 2)
expect(has(todos, 0)).toBe(true)
expect(get(todos, 0)).toBe(2)
set(todos, "1", 4)
set(todos, 3, 4)
set(todos, 1, 3)
remove(todos, 2)
remove(todos, "0")
expect(snapshots).toEqual([
[2],
[2, 4],
[2, 4, undefined, 4],
[2, 3, undefined, 4],
[2, 3, 4],
[3, 4]
])
})
test("observe & intercept", () => {
let events = []
const todos = observable(
{
a: { title: "get coffee" }
},
{},
{ deep: false }
)
mobx.observe(todos, c => events.push({ observe: c }))
const d = mobx.intercept(todos, c => {
events.push({ intercept: c })
return null // no addition!
})
set(todos, { b: { title: "get tea" } })
remove(todos, "a")
expect(events).toMatchSnapshot()
expect(mobx.toJS(todos)).toEqual({
a: { title: "get coffee" }
})
events.splice(0)
d()
set(todos, { b: { title: "get tea" } })
remove(todos, "a")
expect(events).toMatchSnapshot()
expect(mobx.toJS(todos)).toEqual({
b: { title: "get tea" }
})
})
test("dynamically adding properties should preserve the original modifiers of an object", () => {
const todos = observable(
{
a: { title: "get coffee" }
},
{},
{ deep: false }
)
expect(mobx.isObservable(todos.a)).toBe(false)
set(todos, { b: { title: "get tea" } })
expect(mobx.isObservable(todos.b)).toBe(false)
})
test("has and get are reactive", async () => {
const todos = observable({})
const p1 = when(() => has(todos, "x"))
const p2 = when(() => get(todos, "y") === 3)
setTimeout(() => {
set(todos, { x: false, y: 3 })
}, 100)
await p1
await p2
})
test("computed props are not considered part of collections", () => {
const x = observable({
get y() {
return 3
}
})
expect(mobx.isComputedProp(x, "y")).toBe(true)
expect(x.y).toBe(3)
expect(has(x, "y")).toBe(false)
expect(get(x, "y")).toBe(undefined) // disputable?
expect(keys(x)).toEqual([])
expect(values(x)).toEqual([])
})