-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.js
320 lines (272 loc) · 8.47 KB
/
test.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
'use strict'
const mewt = require('./lib')
describe('mewt', () => {
const assertMewt = input => {
expect(typeof input.$set).toBe('function')
expect(typeof input.$unset).toBe('function')
}
describe('generic', () => {
it('should export a function', () => {
expect(typeof mewt).toBe('function')
})
})
describe('should throw without object or array type', () => {
it('should throw when undefined given', () => {
expect(() => mewt()).toThrowError(/expect arr|object/)
})
it('should throw when null given', () => {
expect(() => mewt(null)).toThrowError(/expect arr|object/)
})
it('should throw when string given', () => {
expect(() => mewt('foo')).toThrowError(/expect arr|object/)
})
it('should throw when number given', () => {
expect(() => mewt(123)).toThrowError(/expect arr|object/)
})
it('should throw when boolean given', () => {
expect(() => mewt(true)).toThrowError(/expect arr|object/)
expect(() => mewt(false)).toThrowError(/expect arr|object/)
})
it('should throw when function given', () => {
expect(() => mewt(() => {})).toThrowError(/expect arr|object/)
})
it('should throw when Symbol given', () => {
expect(() => mewt(Symbol('foo'))).toThrowError(/expect arr|object/)
})
})
describe('array', () => {
it('should return a new array', () => {
const a = []
const n = mewt(a)
expect(n).not.toBe(a)
expect(n).toEqual(a)
})
it('should handle null, undefined, and array holes', () => {
const a = mewt([null, undefined, , ])
expect(() => a.forEach(v => v)).not.toThrow()
})
it('should throw on mutation', () => {
const a = mewt([])
expect(() => a[0] = 'Lodger').toThrowError(/immutable/)
})
it('should throw on nested mutation', () => {
const a = mewt([{}])
expect(() => a[0].title = 'Lodger').toThrowError(/immutable/)
})
it('should create new mewt on child $set', () => {
const a = mewt([{}])
const b = a[0].$set('title', 'Lodger')
expect(a).not.toBe(b)
expect(a[0]).not.toBe(b[0])
expect(a[0]).toEqual({})
expect(b[0]).toEqual({title: 'Lodger'})
assertMewt(b)
})
it('should have $set & $unset', () => {
const a = mewt([])
assertMewt(a)
})
it('should get own property', () => {
const a = mewt([''])
expect(a[0]).toBe('')
})
it('should properly $set at index', () => {
const a = mewt([])
const n = a.$set(0, '')
expect(n).toEqual([''])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should properly $unset at index', () => {
const a = mewt([''])
const n = a.$unset(0)
expect(n).toEqual([])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should properly $unset negative indexes and string keys', () => {
const a = mewt([1, 2, 3])
const n = a.$set('foo', 'bar')
.$set(-1, 42)
.$unset(-1)
.$unset('foo')
expect(n).not.toBe(a)
expect(n).toEqual([1, 2, 3])
})
it('should copyWithin without mutation', () => {
const a = mewt([1, 2])
const n = a.copyWithin(0, 2)
expect(n).toEqual([1, 2])
expect(n).not.toBe(a)
assertMewt(n)
})
it('should fill without mutation', () => {
const a = mewt([,])
const n = a.fill('')
expect(n).toEqual([''])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should pop without mutation', () => {
const a = mewt([''])
const [str, n] = a.pop()
expect(a).toEqual([''])
expect(str).toBe('')
expect(n).toEqual([])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should push without mutation', () => {
const a = mewt([])
const [len, n] = a.push('')
expect(len).toBe(1)
expect(n).toEqual([''])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should reverse without mutation', () => {
const a = mewt([1, 2])
const n = a.reverse()
expect(n).toEqual([2, 1])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should shift without mutation', () => {
const a = mewt([1, 2])
const [num, n] = a.shift()
expect(num).toBe(1)
expect(n).toEqual([2])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should sort without mutation', () => {
const a = mewt(['c', 'b', 'a'])
const n = a.sort()
expect(n).toEqual(['a', 'b', 'c'])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should splice without mutation', () => {
const a = mewt(['a', 'b', 'c'])
const n = a.splice(0, 1)
expect(n).toEqual(['a'])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should unshift without mutation', () => {
const a = mewt([2])
const [len, n] = a.unshift(1)
expect(len).toBe(2)
expect(n).toEqual([1, 2])
expect(a).not.toBe(n)
assertMewt(n)
})
it('should filter and the new array should be a mewt', () => {
const a = mewt([1, 2, 3, 4, 5])
const filtered = a.filter(e => e % 2 === 0)
expect(filtered).toEqual([2, 4])
assertMewt(filtered)
})
it('should transform (map) and the new array should be a mewt', () => {
const a = mewt([1, 2, 3, 4, 5])
const transformed = a.map(e => e * 2)
expect(transformed).toEqual([2, 4, 6, 8, 10])
assertMewt(transformed)
})
it('should concat and the new array should be a mewt', () => {
const a = mewt([1, 2]).concat([3, 4, 5])
expect(a).toEqual([1, 2, 3, 4, 5])
assertMewt(a)
const b = mewt([5, 4]).concat(mewt([3, 2, 1]))
expect(b).toEqual([5, 4, 3, 2, 1])
assertMewt(b)
})
it('should slice and the new array should be a mewt', () => {
const a = mewt([1, 2, 3, 4, 5])
const sliced = a.slice(1, 4)
expect(sliced).toEqual([2, 3, 4])
assertMewt(sliced)
})
})
describe('object', () => {
it('should return a new object', () => {
const o = {}
const n = mewt(o)
expect(o).not.toBe(n)
})
it('should handle null and undefined property values', () => {
const a = mewt({ a: null, b: undefined })
expect(() => Object.keys(a).forEach(k => a[k])).not.toThrow()
})
it('should throw on mutation', () => {
const o = mewt({})
expect(() => o.track = 'The Promise').toThrowError(/immutable/)
})
it('should throw on nested mutation', () => {
const o = mewt({track: {}})
expect(() => o.track.title = 'The Promise').toThrowError(/immutable/)
})
it('should create new mewt on child $set', () => {
const a = mewt({cat: {name: 'Top'}})
const b = a.cat.$set('name', 'Pushkin')
expect(a).not.toBe(b)
expect(a.cat).not.toBe(b.cat)
expect(a.cat.name).toBe('Top')
expect(b.cat.name).toBe('Pushkin')
assertMewt(b)
})
it('should throw on Object.defineProperty', () => {
const o = mewt({})
expect(() => {
Object.defineProperty(o, 'foo', {
value: 'bar'
})
}).toThrowError(/immutable/)
})
it('should throw on delete', () => {
const o = mewt({})
expect(() => {
delete o.foo
}).toThrowError(/immutable/)
})
it('should throw on setPrototypeOf', () => {
const o = mewt({})
expect(() => {
Object.setPrototypeOf(o, { bar: 'baz' })
}).toThrowError(/immutable/)
})
it('should have $set & $unset', () => {
const o = mewt({})
assertMewt(o)
})
it('should get own property', () => {
const o = mewt({ album: 'Aladdin Sane' })
expect(o.album).toBe('Aladdin Sane')
})
it('should properly $set at key', () => {
const o = mewt({})
const n = o.$set('album', 'Hours')
expect(o.album).toBeUndefined()
expect(n.album).toBe('Hours')
expect(o).not.toBe(n)
})
it('should properly $unset at key', () => {
const o = mewt({ album: 'Heroes' })
const n = o.$unset('album')
expect(o.album).toBe('Heroes')
expect(n.album).toBeUndefined()
expect(o).not.toBe(n)
})
it('should properly $unset numeric indexes', () => {
const o = mewt({ foo: 'bar', 0: 'foo', 1: 'bar' })
expect(o.$unset(0)).toEqual({ foo: 'bar', 1: 'bar' })
expect(o.$unset(1)).toEqual({ foo: 'bar', 0: 'foo' })
})
it('should handle changes to the pre-mewted object', () => {
const a = ['foo']
const b = mewt(a)
a.push('bar')
expect(b).toEqual(['foo'])
})
})
})