This repository has been archived by the owner on Jul 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreactive-array-test.coffee
300 lines (243 loc) · 6.37 KB
/
reactive-array-test.coffee
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
Tinytest.add "ReactiveArray - Mutator", (test) ->
# Empty constructor
arr = new ReactiveArray
test.equal arr.length, 0
test.equal _.size(arr), 0
# length assign
test.equal arr.length = 8, 8
arr[3] = 'abc'
test.equal arr.length, 8
test.equal _.size(arr), 8
test.equal arr.length = 4, 4
test.equal arr[3], 'abc'
test.equal arr.length = 0, 0
test.equal arr.length, 0
test.equal _.size(arr), 0
# push
test.equal arr.push('drink', 'beer', 'or', 'wine'), 4
test.equal arr.length, 4
test.equal _.size(arr), 4
test.equal arr[0], 'drink'
test.equal arr[3], 'wine'
test.isUndefined arr[4]
# pop
test.equal arr.pop(), 'wine'
test.equal arr.length, 3
test.equal _.size(arr), 3
# shift
test.equal arr.shift(), 'drink'
test.equal arr.length, 2
test.equal _.size(arr), 2
# unshift
test.equal arr.unshift('give'), 3
test.equal arr[0], 'give'
# splice
test.equal arr.splice(2), ['or']
test.equal arr.length, 2
test.equal _.size(arr), 2
# bracket assign (index must exists)
test.equal arr[0] = 'drink', 'drink'
test.equal arr.length, 2
test.equal _.size(arr), 2
test.equal arr[0], 'drink'
# sort
sortedArray = new ReactiveArray 'beer', 'drink'
test.equal arr.sort(), sortedArray
test.equal arr, sortedArray
Tinytest.add "ReactiveArray - Deps", (test) ->
arr = new ReactiveArray
# length dep
lengthX = 0
Deps.autorun () ->
l = arr.length
lengthX++
test.equal lengthX, 1
# indexOf dep
indexOfX = 0
Deps.autorun () ->
arr.indexOf 'first'
indexOfX++
test.equal indexOfX, 1
# lastIndexOf dep
lastIndexOfX = 0
Deps.autorun () ->
arr.lastIndexOf 'last'
lastIndexOfX++
test.equal lastIndexOfX, 1
# add first item
arr.push 'my'
Deps.flush()
test.equal lengthX, 2
test.equal indexOfX, 2
test.equal lastIndexOfX, 2
# bracket get dep
bracketX = 0
Deps.autorun () ->
v = arr[0]
bracketX++
test.equal bracketX, 1
# add second item
arr.push 'first'
Deps.flush()
test.equal bracketX, 1
test.equal lengthX, 3
test.equal indexOfX, 3
test.equal lastIndexOfX, 3
# add third item
arr.push 'last'
Deps.flush()
test.equal bracketX, 1
test.equal lengthX, 4
test.equal indexOfX, 3
test.equal lastIndexOfX, 4
# add fourth item
arr.push 'item'
Deps.flush()
test.equal bracketX, 1
test.equal lengthX, 5
test.equal indexOfX, 3
test.equal lastIndexOfX, 5
# change first
arr[0] = 'your'
Deps.flush()
test.equal bracketX, 2
test.equal lengthX, 5
test.equal indexOfX, 4, 'indexOf: needs to search again because something before the found index was changed'
test.equal lastIndexOfX, 5, 'lastIndexOfX: should not search again because i has a found index after the changed one'
# sort
arr.sort()
Deps.flush()
test.equal bracketX, 3
test.equal lengthX, 5
test.equal indexOfX, 5, 'sort changes all (not really but i\'m lazy)'
test.equal lastIndexOfX, 6, 'sort changes all (not really but i\'m lazy)'
# shift
arr.unshift('drink')
Deps.flush()
test.equal lengthX, 6
test.equal bracketX, 4
# unshift
test.equal arr.shift(), 'drink'
Deps.flush()
test.equal lengthX, 7
test.equal bracketX, 5
Tinytest.add "ReactiveArray - Sort/Reverse", (test) ->
arr = new ReactiveArray 3, 2, 1
bracket1X = 0
Deps.autorun () ->
v = arr[0]
bracket1X++
test.equal bracket1X, 1
bracket2X = 0
Deps.autorun () ->
v = arr[1]
bracket2X++
test.equal bracket2X, 1
arr.sort()
Deps.flush()
test.equal bracket1X, 2
test.equal bracket2X, 1
arr.reverse()
Deps.flush()
test.equal bracket1X, 3
test.equal bracket2X, 1
Tinytest.add "ReactiveArray - Every/Some", (test) ->
arr = new ReactiveArray 1, 1, 2
everyX = 0
Deps.autorun () ->
arr.every (v,i,a) ->
test.equal a, arr
test.equal v, arr._list[i]
v == 1
everyX++
test.equal everyX, 1
someX = 0
Deps.autorun () ->
arr.some (v,i,a) ->
test.equal a, arr
test.equal v, arr._list[i]
v == 2
someX++
test.equal someX, 1
arr.push 1
Deps.flush()
test.equal everyX, 1
test.equal someX, 1
arr.pop()
Deps.flush()
test.equal everyX, 1
test.equal someX, 1
arr.pop()
Deps.flush()
test.equal everyX, 2
test.equal someX, 2
arr.push 1
Deps.flush()
test.equal everyX, 3
test.equal someX, 3
Tinytest.add "ReactiveArray - Map", (test) ->
arr = new ReactiveArray 0, 1, 2
x = 0
Deps.autorun () ->
map = arr.map (v, i, array) ->
test.equal array, arr
return v+1
orgMap = arr._list.map (v) ->
return v+1
test.equal map, ReactiveArray.wrap orgMap
++x
test.equal x, 1
arr.push 5
Deps.flush()
test.equal x, 2
arr.pop()
Deps.flush()
test.equal x, 3
arr[0] = 1
Deps.flush()
test.equal x, 4
Tinytest.add "ReactiveArray - Reduce", (test) ->
arr = new ReactiveArray 0, 1, 2, 3, 4
x = 0
Deps.autorun () ->
r = arr.reduce (previousValue, currentValue, index, array) ->
test.equal array, arr
return previousValue + currentValue
orgR = arr._list.reduce (previousValue, currentValue) ->
return previousValue + currentValue
test.equal r, orgR
test.equal r, 10 if x == 0 || x == 2
test.equal r, 15 if x == 1
++x
test.equal x, 1
arr.push 5
Deps.flush()
test.equal x, 2
arr.pop()
Deps.flush()
test.equal x, 3
arr[0] = 1
Deps.flush()
test.equal x, 4
Tinytest.add "ReactiveArray - EJSON", (test) ->
arr = new ReactiveArray 'a', 'b'
dolly = arr.clone()
test.isTrue arr.equals(dolly), 'Clone should return a equal'
objJson = EJSON.fromJSONValue EJSON.toJSONValue arr
test.isTrue arr.equals(objJson), 'JSON from, to should return a equal object'
arr.push 'c'
test.isFalse arr.equals(objJson), 'Obj was changed this should not effect a JSON created version'
test.isFalse arr.equals(dolly), 'Obj was changed this should not effect a clone'
return
Tinytest.add "ReactiveArray - issue 2", (test) ->
arr = new ReactiveArray
i = 0
Deps.autorun () ->
i++
if i > 1
return
arr.push 'val'
arr.push 'val'
Deps.flush()
if i != 1
test.fail {message: 'This should only be called once'}