-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathutils.spec.js
309 lines (234 loc) · 7.87 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
import { debounce, deepEqual, computeTooltipPosition, cssTimeToMs, clearTimeoutRef } from 'utils'
import { injectStyle } from 'utils/handle-style.ts'
describe('compute positions', () => {
test('empty reference elements', async () => {
const value = await computeTooltipPosition({
elementReference: null,
tooltipReference: null,
tooltipArrowReference: null,
})
expect(value).toEqual({
tooltipStyles: {},
tooltipArrowStyles: {},
place: 'top',
})
})
test('empty tooltip reference element', async () => {
const element = document.createElement('div')
const value = await computeTooltipPosition({
elementReference: element,
tooltipReference: null,
tooltipArrowReference: null,
})
expect(value).toEqual({
tooltipStyles: {},
tooltipArrowStyles: {},
place: 'top',
})
})
test('empty tooltip arrow reference element', async () => {
const element = document.createElement('div')
const elementTooltip = document.createElement('div')
const value = await computeTooltipPosition({
elementReference: element,
tooltipReference: elementTooltip,
tooltipArrowReference: null,
})
expect(value).toEqual({
tooltipArrowStyles: {},
tooltipStyles: {
left: '5px',
top: '10px',
},
place: 'bottom',
})
})
test('all reference elements', async () => {
const element = document.createElement('div')
const elementTooltip = document.createElement('div')
const elementTooltipArrow = document.createElement('div')
const value = await computeTooltipPosition({
elementReference: element,
tooltipReference: elementTooltip,
tooltipArrowReference: elementTooltipArrow,
})
expect(value).toMatchSnapshot()
})
test('all reference elements with border', async () => {
const element = document.createElement('div')
const elementTooltip = document.createElement('div')
const elementTooltipArrow = document.createElement('div')
const value = await computeTooltipPosition({
elementReference: element,
tooltipReference: elementTooltip,
tooltipArrowReference: elementTooltipArrow,
border: '1px solid red',
})
expect(value).toMatchSnapshot()
})
test('all reference elements with non-px border', async () => {
const element = document.createElement('div')
const elementTooltip = document.createElement('div')
const elementTooltipArrow = document.createElement('div')
const value = await computeTooltipPosition({
elementReference: element,
tooltipReference: elementTooltip,
tooltipArrowReference: elementTooltipArrow,
border: 'medium solid red',
})
expect(value).toMatchSnapshot()
})
})
describe('css time to ms', () => {
test('converts time correctly', () => {
expect(cssTimeToMs('1s')).toBe(1000)
expect(cssTimeToMs('1ms')).toBe(1)
expect(cssTimeToMs('1.5s')).toBe(1500)
expect(cssTimeToMs('1.5ms')).toBe(1.5)
})
test('returns 0 if no time is provided', () => {
expect(cssTimeToMs('')).toBe(0)
})
test('returns 0 if unsupported unit', () => {
expect(cssTimeToMs('1h')).toBe(0)
})
test('returns 0 if no unit', () => {
expect(cssTimeToMs('1000')).toBe(0)
})
})
describe('debounce', () => {
jest.useFakeTimers()
const func = jest.fn()
test('execute just once', () => {
const debouncedFunc = debounce(func, 1000)
for (let i = 0; i < 100; i += 1) {
debouncedFunc()
}
expect(func).not.toHaveBeenCalled()
jest.runAllTimers()
expect(func).toBeCalledTimes(1)
})
test('execute immediately just once', () => {
const debouncedFunc = debounce(func, 1000, true)
debouncedFunc()
expect(func).toBeCalledTimes(1)
for (let i = 0; i < 100; i += 1) {
debouncedFunc()
}
jest.runAllTimers()
expect(func).toHaveBeenCalledTimes(1)
})
test('does not execute after cancel', () => {
const debouncedFunc = debounce(func, 1000)
debouncedFunc()
expect(func).not.toHaveBeenCalled()
debouncedFunc.cancel()
jest.runAllTimers()
expect(func).not.toHaveBeenCalled()
})
})
describe('deepEqual', () => {
test('returns true for equal primitives', () => {
expect(deepEqual(1, 1)).toBe(true)
expect(deepEqual('a', 'a')).toBe(true)
expect(deepEqual(true, true)).toBe(true)
})
test('returns false for different primitives', () => {
expect(deepEqual(1, 2)).toBe(false)
expect(deepEqual('a', 'b')).toBe(false)
expect(deepEqual(true, false)).toBe(false)
})
test('returns true for equal objects', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1, b: 2 }
expect(deepEqual(obj1, obj2)).toBe(true)
})
test('returns false for different objects', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1, b: 3 }
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns false for object with different amount of keys', () => {
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1 }
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns true for equal nested objects', () => {
const obj1 = { a: 1, b: { c: 2, d: 3 } }
const obj2 = { a: 1, b: { c: 2, d: 3 } }
expect(deepEqual(obj1, obj2)).toBe(true)
})
test('returns false for different nested objects', () => {
const obj1 = { a: 1, b: { c: 2, d: 3 } }
const obj2 = { a: 1, b: { c: 2, d: 4 } }
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns true for equal arrays', () => {
const obj1 = [1, 2, 3]
const obj2 = [1, 2, 3]
expect(deepEqual(obj1, obj2)).toBe(true)
})
test('returns false for different arrays', () => {
const obj1 = [1, 2, 3]
const obj2 = [1, 2, 4]
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns false for different length arrays', () => {
const obj1 = [1, 2, 3]
const obj2 = [1, 2]
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns false for array with non-array', () => {
const obj1 = [1, 2, 3]
const obj2 = 1
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns true for equal nested arrays', () => {
const obj1 = [1, [2, 3]]
const obj2 = [1, [2, 3]]
expect(deepEqual(obj1, obj2)).toBe(true)
})
test('returns false for different nested arrays', () => {
const obj1 = [1, [2, 3]]
const obj2 = [1, [2, 4]]
expect(deepEqual(obj1, obj2)).toBe(false)
})
test('returns true for equal mixed objects and arrays', () => {
const obj1 = { a: 1, b: [2, 3] }
const obj2 = { a: 1, b: [2, 3] }
expect(deepEqual(obj1, obj2)).toBe(true)
})
test('returns false for different mixed objects and arrays', () => {
const obj1 = { a: 1, b: [2, 3] }
const obj2 = { a: 1, b: [2, 4] }
expect(deepEqual(obj1, obj2)).toBe(false)
})
})
describe('clearTimeoutRef', () => {
jest.useFakeTimers()
const func = jest.fn()
test('clears timeout ref', () => {
const timeoutRef = { current: setTimeout(func, 1000) }
clearTimeoutRef(timeoutRef)
jest.runAllTimers()
expect(func).not.toHaveBeenCalled()
expect(timeoutRef.current).toBe(null)
})
})
describe('handleStyle', () => {
test('inject base styles with no CSS into the page', () => {
injectStyle({ css: null, type: 'base' })
const styleElement = document.getElementById('react-tooltip-base-styles')
expect(styleElement).toBe(null)
})
test('inject core styles into the page', () => {
injectStyle({ css: `body { background: 'red' }`, type: 'core' })
const styleElement = document.getElementById('react-tooltip-core-styles')
expect(styleElement.innerHTML).toBe(`body { background: 'red' }`)
})
test('inject base styles into the page', () => {
injectStyle({ css: `body { background: 'red' }`, type: 'base' })
const styleElement = document.getElementById('react-tooltip-base-styles')
expect(styleElement.innerHTML).toBe(`body { background: 'red' }`)
})
})