/
test.array.js
259 lines (197 loc) · 4.87 KB
/
test.array.js
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
expect = global.expect || require('expect.js')
o_O = global.o_O || require('../o_O')
describe('an array', function() {
var col
beforeEach(function() {
col = o_O.array()
})
it('has a count of 0', function() {
expect(col.count()).to.be(0)
})
describe('adding/removing', function() {
var obj
beforeEach(function() {
obj = {}
col.push(obj)
})
it('has a count of 1', function() {
expect(col.count()).to.be(1)
})
it('handles the removal of an item it doesn\'t have gracefully', function() {
col.remove({foo: 'bar'})
expect(col.count()).to.be(1)
})
it('increases count after add', function() {
expect(col.count()).to.be(1)
})
it('triggers the *add* event after add', function() {
var eventTriggered = false
col.on('add', function(){
eventTriggered = true
})
var obj2 = {}
col.push(obj2)
expect(eventTriggered).to.be(true)
// cleanup
col.remove(obj2)
})
it('decreases count after remove', function() {
col.remove(obj)
expect(col.count()).to.be(0)
})
it('triggers the *remove* event after remove', function() {
var eventTriggered = false
col.on('remove', function(){
eventTriggered = true
})
col.remove(obj)
expect(eventTriggered).to.be(true)
})
it('triggers the *remove* event after remove (on model)', function() {
var M = o_O.model({age: 1})
var m = new M({age:2})
var eventTriggered = false
col.on('remove', function(){
eventTriggered = true
})
col.push(m)
col.remove(m)
expect(eventTriggered).to.be(true)
})
it('can add the same model more than once', function() {
var M = o_O.model({age: 1})
var m = new M({age: 2})
col.pop()
col.push(m)
col.push(m)
col.push(m)
col.push(m)
col.push(m)
expect(col.count()).to.be(5)
})
it('returns the removed item', function() {
var removed = col.remove(obj)
expect(removed).to.be(obj)
})
})
describe('shift', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.push(one)
col.push(two)
col.push(three)
})
it('decrements the count', function() {
col.shift()
expect(col.count()).to.be(2)
})
it('removes an item from the front of the array', function() {
var removed = col.shift()
expect(removed).to.be(one)
})
})
describe('pop', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.push(one)
col.push(two)
col.push(three)
})
it('decrements the count', function() {
col.pop()
expect(col.count()).to.be(2)
})
it('removes an item from the end of the array', function() {
var removed = col.pop()
expect(removed).to.be(three)
})
})
describe('push', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.push(one)
col.push(two)
col.push(three)
})
it('increments the count', function() {
expect(col.count()).to.be(3)
})
it('adds an item to the end of the array', function() {
expect(col.at(0)).to.be(one)
expect(col.at(2)).to.be(three)
})
})
describe('unshift', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.unshift(one)
col.unshift(two)
col.unshift(three)
})
it('increments the count', function() {
expect(col.count()).to.be(3)
})
it('adds an item to the start of the array', function() {
expect(col.at(0)).to.be(three)
expect(col.at(2)).to.be(one)
})
})
describe('insertAt', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.insert(one, 0)
col.insert(two, 1)
})
it('increments the count', function() {
expect(col.count()).to.be(2)
})
it('adds an item to the start of the array', function() {
expect(col.at(0)).to.be(one)
expect(col.at(1)).to.be(two)
})
it('adds an item to the end of the array', function() {
col.insert(three, 2)
expect(col.at(2)).to.be(three)
})
it('fails if inserting to invalid place', function() {
expect(col.insert({}, -1)).to.be(false)
expect(col.insert({}, 4)).to.be(false)
expect(col.count()).to.be(2)
})
})
describe('removeAt', function(){
var one = {one: 1}
var two = {two: 2}
var three = {three: 3}
beforeEach(function() {
col.insert(one, 0)
col.insert(two, 1)
col.insert(three, 2)
})
it('decrements the count', function() {
col.removeAt(0)
expect(col.count()).to.be(2)
})
it('removes the item to the start of the array', function() {
col.removeAt(0)
expect(col.at(0)).to.be(two)
})
it('removes the item to the start of the array', function() {
col.removeAt(1)
expect(col.at(1)).to.be(three)
})
it('fails if removing from invalid place', function() {
expect(col.removeAt(-1)).to.be(false)
})
})
})