forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-api-proxy.js
More file actions
163 lines (138 loc) · 4.31 KB
/
Copy pathobject-api-proxy.js
File metadata and controls
163 lines (138 loc) · 4.31 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
const mobx = require("../../src/mobx")
const {
when,
set,
remove,
values,
runInAction,
entries,
reaction,
observable,
extendObservable,
$mobx
} = mobx
test("keys should be observable when extending", () => {
const todos = observable({})
const todoTitles = []
reaction(
() => Object.keys(todos).map(key => `${key}: ${todos[key]}`),
titles => todoTitles.push(titles.join(","))
)
runInAction(() => {
Object.assign(todos, {
lewis: "Read Lewis",
chesterton: "Be mind blown by Chesterton"
})
})
expect(todoTitles).toEqual(["lewis: Read Lewis,chesterton: Be mind blown by Chesterton"])
Object.assign(todos, { lewis: "Read Lewis twice" })
Object.assign(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)))
})
runInAction(() => {
Object.assign(todos, {
lewis: "Read Lewis",
chesterton: "Be mind blown by Chesterton"
})
})
Object.assign(todos, { lewis: "Read Lewis twice" })
Object.assign(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("x" in todos).toBe(false)
expect(todos.x).toBe(undefined)
todos.x = 3
expect("x" in todos).toBe(true)
expect(todos.x).toBe(3)
delete todos.y
todos.z = 4
todos.x = 5
delete todos.z
expect(snapshots).toEqual([[3], [3, 4], [5, 4], [5]])
})
test("object - set, remove, entries are reactive", () => {
const todos = observable({})
const snapshots = []
reaction(() => entries(todos), entries => snapshots.push(entries))
expect("x" in todos).toBe(false)
expect(todos.x).toBe(undefined)
todos.x = 3
expect("x" in todos).toBe(true)
expect(todos.x).toBe(3)
delete todos.y
todos.z = 4
todos.x = 5
delete todos.z
expect(snapshots).toEqual([[["x", 3]], [["x", 3], ["z", 4]], [["x", 5], ["z", 4]], [["x", 5]]])
})
test("object - set, remove, keys are reactive", () => {
const todos = observable({ a: 3 })
const snapshots = []
reaction(() => Object.keys(todos), keys => snapshots.push(keys))
todos.x = 3
delete todos.y
todos.z = 4
todos.x = 5
delete todos.z
delete todos.a
expect(snapshots).toEqual([["a", "x"], ["a", "x", "z"], ["a", "x"], ["x"]])
})
test("dynamically adding properties should preserve the original modifiers of an object", () => {
const todos = observable.object(
{
a: { title: "get coffee" }
},
{},
{ deep: false }
)
expect(mobx.isObservable(todos.a)).toBe(false)
Object.assign(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(() => {
debugger
return "x" in todos
})
const p2 = when(() => {
return todos.y === 3
})
setTimeout(() => {
Object.assign(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("y" in x).toBe(true) // `in` also checks proto type, so should return true!
expect(Object.keys(x)).toEqual([])
expect(values(x)).toEqual([])
expect(entries(x)).toEqual([])
})