-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathutils-tests.js
367 lines (305 loc) · 10.2 KB
/
utils-tests.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
var Utils = require('../src/utils')
describe('Utils', () => {
describe('#isString', () => {
it('correctly identifies a non-string as not a string', () => {
var result = Utils.isString(1)
expect(result).toBe(false)
})
it('correctly identifies a string as a string', () => {
var result = Utils.isString('string')
expect(result).toBe(true)
})
})
describe('#isArray', () => {
it('correctly identifies a non-array as not an array', () => {
var result = Utils.isArray(1)
expect(result).toBe(false)
})
it('correctly identifies an array literal as an array', () => {
var result = Utils.isArray([])
expect(result).toBe(true)
})
it('correctly identifies an array instance as an array', () => {
var result = Utils.isArray([])
expect(result).toBe(true)
})
describe('when isArray is not defined', function() {
var originalIsArray
beforeEach(() => {
originalIsArray = Array.isArray
Array.isArray = undefined
})
afterEach(() => {
Array.isArray = originalIsArray
})
it('correctly identifies a non-array as not an array', () => {
var result = Utils.isArray(1)
expect(result).toBe(false)
})
it('correctly identifies an array literal as an array', () => {
var result = Utils.isArray([])
expect(result).toBe(true)
})
it('correctly identifies an array instance as an array', () => {
var result = Utils.isArray([])
expect(result).toBe(true)
})
})
})
describe('#isFunction', () => {
it('correctly identifies a non-function as not a function', () => {
var result = Utils.isFunction(1)
expect(result).toBe(false)
})
it('correctly identifies a RegEx as not a function', () => {
var result = Utils.isFunction(/something/)
expect(result).toBe(false)
})
it('correctly identifies a function decleration as a function', () => {
var result = Utils.isFunction(() => {})
expect(result).toBe(true)
})
it('correctly identifies a function expression as a function', () => {
var testCase = () => {}
var result = Utils.isFunction(testCase)
expect(result).toBe(true)
})
it('correctly identifies a method as a function', () => {
var result = Utils.isFunction(Utils.isFunction)
expect(result).toBe(true)
})
})
describe('#isObject', () => {
it('identifies an array as an Object type', () => {
expect(Utils.isObject([])).toBe(true)
})
it('identifies an object literal as an Object type', () => {
expect(Utils.isObject({})).toBe(true)
})
it('identifies the arguments object as an Object type', () => {
expect(Utils.isObject(arguments)).toBe(true)
})
it('identifies a function as an Object type', () => {
expect(Utils.isObject(() => {})).toBe(true)
})
it('identifies a regex as an Object type', () => {
expect(Utils.isObject(/something/)).toBe(true)
})
it('identifies primitives and non-objects as not of type Object', () => {
expect(Utils.isObject(1)).not.toBe(true)
expect(Utils.isObject('something')).not.toBe(true)
expect(Utils.isObject(false)).not.toBe(true)
expect(Utils.isObject(undefined)).not.toBe(true)
expect(Utils.isObject(null)).not.toBe(true)
})
it('identifies instances as Object types', () => {
/* eslint-disable no-new-wrappers */
expect(Utils.isObject(new Number(0))).toBe(true)
expect(Utils.isObject(new String(''))).toBe(true)
expect(Utils.isObject(new Boolean(''))).toBe(true)
/* eslint-enable no-new-wrappers */
})
})
describe('#extend', () => {
it('extends an object with the attributes of another', () => {
expect(Utils.extend({}, { a: 1 })).toEqual({ a: 1 })
})
it('overrides source properties with destination properties', () => {
expect(Utils.extend({ a: 1 }, { a: 2 }).a).toBe(2)
})
it('maintains destination properties not in source', () => {
expect(Utils.extend({ a: 1 }, { b: 2 }).a).toBeDefined()
})
it('can extend from multiple sources', () => {
var result = Utils.extend({ a: 1 }, { b: 2 }, { c: 3})
expect(result).toEqual({ a: 1, b: 2, c: 3})
})
it('sets property priority from right to left', () => {
var result = Utils.extend({ a: 1 }, { a: 2, b: 2 }, { b: 3 })
expect(result).toEqual({ a: 2, b: 3 })
})
it('skips over non-plain objects', () => {
var result = Utils.extend({ a: 1 }, /something/, { b: 2 })
expect(result).toEqual({ a: 1, b: 2 })
})
it('returns an empty object when arguments are not defined', () => {
expect(Utils.extend()).toEqual({})
})
it('returns the original object when only one argument is passed', () => {
var obj = {}
expect(Utils.extend(obj)).toBe(obj)
})
it('copies all properties from source', () => {
var obj = { a: 1 }
obj.b = 2
expect(Utils.extend({}, obj).a).toBe(1)
expect(Utils.extend({}, obj).b).toBe(2)
})
it('does not extend inherited properties', () => {
var F = function() {}
F.prototype = { a: 1 }
expect(Utils.extend({ a: 10 }, F).a).toEqual(10)
})
})
describe('#clone', () => {
it('clones a simple array', () => {
var arr = [1, 2, 3]
var result = Utils.clone(arr)
expect(result).toEqual(arr)
})
it('clones object literals', () => {
var obj = { a: 1, b: 2, c: 3 }
var result = Utils.clone(obj)
expect(result).toEqual(obj)
})
it('does not share shallow attributes between objects', () => {
var obj = { a: 1 }
var result = Utils.clone(obj)
result.a = 10
expect(obj.a === 1 && result.a === 10).toBe(true)
})
it('shares changes to deep attributes between objects', () => {
var obj = { a: 1, b: 2, c: [1, 2, 3] }
var result = Utils.clone(obj)
result.c.push(4)
expect(obj.c[obj.c.length - 1]).toBe(4)
})
it('does not clone non objects', () => {
expect(Utils.clone(1)).toBe(1)
expect(Utils.clone(undefined)).toBe(undefined)
expect(Utils.clone('some string')).toBe('some string')
expect(Utils.clone(null)).toBe(null)
expect(Utils.clone(true)).toBe(true)
})
})
describe('#each', () => {
var once = ['once']
describe('when iterating over an array', () => {
var arr = [1, 2, 3]
it('provides the value and iteration count to the iteratee', () => {
var values = []
var counts = []
Utils.each(arr, (val, i) => {
values.push(val)
counts.push(++i)
})
expect(values).toEqual(arr)
expect(counts).toEqual(arr)
})
it('accepts the context for the iteratee', () => {
var obj = { add: 5 }
var result = 0
Utils.each(arr, function(val, i) {
result += this.add
}, obj)
expect(result).toBe(15)
})
it('provides the collection to the iteratee', () => {
Utils.each(once, (val, i, collection) => {
expect(collection).toBe(once)
})
})
it('iterates the length of the collection', () => {
var spy = jasmine.createSpy('eachSpy')
Utils.each(arr, (val, i) => spy())
expect(spy.calls.count()).toBe(arr.length)
})
it('breaks out of iteration when `false` is returned', () => {
var spy = jasmine.createSpy('eachSpy')
Utils.each(arr, (val, i) => {
spy(val)
if (val === 2) {
return false
}
})
expect(spy.calls.count()).toBe(2)
expect(spy).toHaveBeenCalledWith(1)
expect(spy).toHaveBeenCalledWith(2)
expect(spy).not.toHaveBeenCalledWith(3)
})
})
describe('when iterating over an object', () => {
var values = []
var keys = []
var ObjProto = {
c: 3,
}
var obj = Object.create(ObjProto)
obj.a = 1
obj.b = 2
Utils.each(obj, (v, k) => {
values.push(v)
keys.push(k)
})
it('provides the value and key to the iteratee', () => {
expect(values).toEqual([1, 2])
expect(keys).toEqual(['a', 'b'])
})
it('does not iterate over the inherited properites', () => {
expect(values).not.toContain(3)
})
it('breaks out of iteration when `false` is returned', () => {
var spy = jasmine.createSpy('eachSpy')
Utils.each(obj, (val, i) => {
spy(val)
if (val === 1) {
return false
}
})
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(1)
expect(spy).not.toHaveBeenCalledWith(4)
})
})
it('is resiliant to collection property changes during iteration', () => {
var changingObj = { 0: 0, 1: 1 }
var count = 0
Utils.each(changingObj, (v, k, collection) => {
if (count < 10) {
changingObj[++count] = v + 1
}
})
expect(count).toBe(2)
expect(changingObj).toEqual({ 0: 0, 1: 1, 2: 2 })
})
it('is resiliant to collection length changes during iteration', () => {
var result = []
var count = 0
Utils.each(once, (v, i) => {
if (count < 10) {
result.push(++count)
}
})
expect(count).toBe(1)
expect(result).toEqual([1])
})
it('exits iteration when false is explicitly returned', () => {
var result = 0
Utils.each([1, 2], (v, i) => {
if (i > 0) {
return false
}
result += v
})
expect(result).toBe(1)
})
it('returns the collection', () => {
expect(Utils.each(once, () => {})).toBe(once)
})
})
describe('#partial', () => {
it('partially applies function arguments', () => {
var func = (greeting, name) => greeting + ' ' + name
var result = Utils.partial(func, 'hello')
expect(result('nuclear')).toBe('hello nuclear')
})
it('does not alter context', () => {
var obj = { name: 'nuclear' }
var func = function(greeting, mark) {
return greeting + ' ' + this.name + mark
}
obj.greet = Utils.partial(func, 'hello')
expect(obj.greet('!')).toBe('hello nuclear!')
})
})
})