forked from riophae/vue-treeselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.spec.js
318 lines (259 loc) · 7.62 KB
/
utils.spec.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
/* eslint import/namespace: 0 */
import sleep from 'yaku/lib/sleep'
import * as utils from '@src/utils'
describe('Utils', () => {
describe('Debugging Helpers', () => {
describe('warning', () => {
const { warning } = utils
const WARNING_MSG = '$MESSAGE$'
beforeEach(() => {
spyOn(console, 'error')
})
it('when true', () => {
warning(() => true, () => WARNING_MSG)
expect(console.error).not.toHaveBeenCalled()
})
it('when false', () => {
warning(() => false, () => WARNING_MSG)
expect(console.error).toHaveBeenCalledWith('[Vue-Treeselect Warning]', WARNING_MSG)
})
})
})
describe('DOM Utilites', () => {
describe('onLeftClick', () => {
const { onLeftClick } = utils
let spy
beforeEach(() => {
spy = jasmine.createSpy('onmousedown')
})
it('should invoke the function when left button has been clicked', () => {
const eventObj = {
type: 'mousedown',
button: 0,
}
onLeftClick(spy)(eventObj)
expect(spy).toHaveBeenCalledWith(eventObj)
})
it('should not invoke the function if wrong event type', () => {
const eventObj = {
type: 'mouseup',
button: 0,
}
onLeftClick(spy)(eventObj)
expect(spy).not.toHaveBeenCalled()
})
it('should not invoke the function if clicked with buttons other than left button', () => {
const eventObj = {
type: 'mousedown',
button: 1,
}
onLeftClick(spy)(eventObj)
expect(spy).not.toHaveBeenCalled()
})
it('should pass extra args', () => {
const eventObj = {
type: 'mousedown',
button: 0,
}
const extraArg = {}
onLeftClick(spy)(eventObj, extraArg)
expect(spy).toHaveBeenCalledWith(eventObj, extraArg)
})
})
it('scrollIntoView', () => {
// TODO
})
it('debounce', () => {
// vendor codes
})
describe('watchSize', () => {
const { watchSize } = utils
let $el
let height
let log
const wait = 100
const listener = (...args) => {
log.push(args)
}
const enlarge = () => {
$el.style.height = (height += 10) + 'px'
}
const reset = () => {
$el = document.createElement('div')
$el.style.height = (height = 100) + 'px'
$el.style.position = 'relative'
document.body.append($el)
log = []
}
const cleanup = () => {
$el.remove()
}
const test = async () => {
reset()
const unwatch = watchSize($el, listener)
expect(log).toBeArrayOfSize(0)
enlarge()
await sleep(wait)
expect(log).toBeArrayOfSize(1)
expect(log[0][0].height).toBe(110)
enlarge()
await sleep(wait)
expect(log).toBeArrayOfSize(2)
expect(log[1][0].height).toBe(120)
unwatch()
cleanup()
}
it('for browsers other than IE9', async () => {
await test()
})
it('for IE9', async () => {
document.documentMode = 9
await test()
delete document.documentMode
})
})
it('setupResizeAndScrollEventListeners', async () => {
const { setupResizeAndScrollEventListeners } = utils
let child, parent, grandparent, called
const init = () => {
grandparent = document.createElement('div')
// eslint-disable-next-line unicorn/prefer-node-append
parent = grandparent.appendChild(document.createElement('div'))
parent.style.overflow = 'auto'
parent.style.height = '100px'
// eslint-disable-next-line unicorn/prefer-node-append
child = parent.appendChild(document.createElement('div'))
child.style.height = '99999px'
document.body.append(grandparent)
called = 0
}
const cleanup = () => {
parent.remove()
}
const trigger = ($el, type) => {
const event = document.createEvent('Event')
event.initEvent(type, true, true)
$el.dispatchEvent(event)
}
const test = async () => {
init()
const listener = () => called++
const unwatch = setupResizeAndScrollEventListeners(child, listener)
parent.scrollTop += 100
await sleep(16)
expect(called).toBe(1)
parent.scrollTop -= 100
await sleep(16)
expect(called).toBe(2)
trigger(window, 'scroll')
await sleep(16)
expect(called).toBe(3)
trigger(window, 'resize')
await sleep(16)
expect(called).toBe(4)
trigger(window, 'scroll')
await sleep(16)
expect(called).toBe(5)
trigger(window, 'resize')
await sleep(16)
expect(called).toBe(6)
unwatch()
parent.scrollTop += 100
await sleep(16)
expect(called).toBe(6)
trigger(window, 'scroll')
await sleep(16)
expect(called).toBe(6)
trigger(window, 'resize')
await sleep(16)
expect(called).toBe(6)
cleanup()
}
await test()
})
})
describe('Language Helpers', () => {
describe('isNaN', () => {
const { isNaN } = utils
it('check if value is NaN', () => {
expect(isNaN(NaN)).toBe(true)
expect(isNaN(0)).toBe(false)
expect(isNaN(-1)).toBe(false)
expect(isNaN(1)).toBe(false)
expect(isNaN('NaN')).toBe(false)
})
})
it('isPromise', () => {
// vender codes
})
it('once', () => {
// vender codes
})
it('noop', () => {
// vender codes
})
it('identity', () => {
// vender codes
})
it('constant', () => {
// vender codes
})
describe('createMap', () => {
const { createMap } = utils
it('prototype should be null', () => {
expect(Object.getPrototypeOf(createMap())).toBe(null)
})
})
describe('deepExtend', () => {
const { deepExtend } = utils
it('should deep extend the target object', () => {
expect(deepExtend({ b: 2 }, { a: 1, c: 3 })).toEqual({ a: 1, b: 2, c: 3 })
})
it('should work with undefined/null', () => {
expect(deepExtend({}, undefined)).toEqual({})
expect(deepExtend({}, null)).toEqual({})
})
})
it('last', () => {
// vendor codes
})
describe('includes', () => {
const { includes } = utils
it('string', () => {
expect(includes('abc', 'ab')).toBe(true)
expect(includes('xyz', 'bc')).toBe(false)
})
it('array', () => {
expect(includes([ 'a', 'b', 'c' ], 'b')).toBe(true)
expect(includes([ 'x', 'y', 'z' ], 'b')).toBe(false)
})
})
describe('find', () => {
const { find } = utils
it('should return the element if matched', () => {
expect(find([ 1, 2, 3 ], n => n % 2 === 0)).toBe(2)
})
it('should return undefined if not matched', () => {
expect(find([ 1 ], n => n < 0)).toBe(undefined)
})
})
it('removeFromArray', () => {
const { removeFromArray } = utils
const arr = [ 1, 2, 3 ]
removeFromArray(arr, 2)
expect(arr).toEqual([ 1, 3 ])
removeFromArray(arr, 9)
expect(arr).toEqual([ 1, 3 ])
})
})
describe('Other Utilities', () => {
it('quickDiff', () => {
const { quickDiff } = utils
const obj = {}
expect(quickDiff([], [])).toBe(false)
expect(quickDiff([ 1 ], [])).toBe(true)
expect(quickDiff([ {} ], [ {} ])).toBe(true)
expect(quickDiff([ obj ], [ obj ])).toBe(false)
})
})
})