forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycles.js
More file actions
195 lines (163 loc) · 5.26 KB
/
Copy pathcycles.js
File metadata and controls
195 lines (163 loc) · 5.26 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
var m = require("../../src/mobx.ts")
test("cascading active state (form 1)", function() {
var Store = function() {
m.extendObservable(this, { _activeItem: null })
}
Store.prototype.activeItem = function(item) {
var _this = this
if (arguments.length === 0) return this._activeItem
m.transaction(function() {
if (_this._activeItem === item) return
if (_this._activeItem) _this._activeItem.isActive = false
_this._activeItem = item
if (_this._activeItem) _this._activeItem.isActive = true
})
}
var Item = function() {
m.extendObservable(this, { isActive: false })
}
var store = new Store()
var item1 = new Item(),
item2 = new Item()
expect(store.activeItem()).toBe(null)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(false)
store.activeItem(item1)
expect(store.activeItem()).toBe(item1)
expect(item1.isActive).toBe(true)
expect(item2.isActive).toBe(false)
store.activeItem(item2)
expect(store.activeItem()).toBe(item2)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(true)
store.activeItem(null)
expect(store.activeItem()).toBe(null)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(false)
})
test("cascading active state (form 2)", function() {
var Store = function() {
var _this = this
m.extendObservable(this, { activeItem: null })
m.autorun(function() {
if (_this._activeItem === _this.activeItem) return
if (_this._activeItem) _this._activeItem.isActive = false
_this._activeItem = _this.activeItem
if (_this._activeItem) _this._activeItem.isActive = true
})
}
var Item = function() {
m.extendObservable(this, { isActive: false })
}
var store = new Store()
var item1 = new Item(),
item2 = new Item()
expect(store.activeItem).toBe(null)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(false)
store.activeItem = item1
expect(store.activeItem).toBe(item1)
expect(item1.isActive).toBe(true)
expect(item2.isActive).toBe(false)
store.activeItem = item2
expect(store.activeItem).toBe(item2)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(true)
store.activeItem = null
expect(store.activeItem).toBe(null)
expect(item1.isActive).toBe(false)
expect(item2.isActive).toBe(false)
})
test("emulate rendering", function() {
var renderCount = 0
var Component = function(props) {
var _this = this
this.props = props
}
Component.prototype.destroy = function() {
if (this.handler) {
this.handler()
this.handler = null
}
}
Component.prototype.render = function() {
var _this = this
if (this.handler) {
this.handler()
this.handler = null
}
this.handler = m.autorun(function() {
if (!_this.props.data.title) _this.props.data.title = "HELLO"
renderCount++
})
}
var data = {}
m.extendObservable(data, { title: null })
var component = new Component({ data: data })
expect(renderCount).toBe(0)
component.render()
expect(renderCount).toBe(1)
data.title = "WORLD"
expect(renderCount).toBe(2)
data.title = null
// Note that this causes two invalidations
// however, the real mobx-react binding optimizes this as well
// see mobx-react #12, so maybe this ain't the best test
expect(renderCount).toBe(4)
data.title = "WORLD"
expect(renderCount).toBe(5)
component.destroy()
data.title = "HELLO"
expect(renderCount).toBe(5)
})
test("efficient selection", function() {
function Item(value) {
m.extendObservable(this, {
selected: false,
value: value
})
}
function Store() {
this.prevSelection = null
m.extendObservable(this, {
selection: null,
items: [new Item(1), new Item(2), new Item(3)]
})
m.autorun(() => {
if (this.previousSelection === this.selection) return true // converging condition
if (this.previousSelection) this.previousSelection.selected = false
if (this.selection) this.selection.selected = true
this.previousSelection = this.selection
})
}
var store = new Store()
expect(store.selection).toBe(null)
expect(
store.items.filter(function(i) {
return i.selected
}).length
).toBe(0)
store.selection = store.items[1]
expect(
store.items.filter(function(i) {
return i.selected
}).length
).toBe(1)
expect(store.selection).toBe(store.items[1])
expect(store.items[1].selected).toBe(true)
store.selection = store.items[2]
expect(
store.items.filter(function(i) {
return i.selected
}).length
).toBe(1)
expect(store.selection).toBe(store.items[2])
expect(store.items[2].selected).toBe(true)
store.selection = null
expect(
store.items.filter(function(i) {
return i.selected
}).length
).toBe(0)
expect(store.selection).toBe(null)
})