forked from nuxt/vue-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.test.js
74 lines (56 loc) · 1.83 KB
/
utils.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
/**
* @jest-environment node
*/
import { find, findIndex, includes, toArray } from '../../src/utils/array'
import { ensureIsArray } from '../../src/utils/ensure'
import { hasGlobalWindowFn } from '../../src/utils/window'
describe('shared', () => {
afterEach(() => jest.restoreAllMocks())
test('ensureIsArray ensures var is array', () => {
let a = { p: 1 }
expect(ensureIsArray(a)).toEqual([])
a = 1
expect(ensureIsArray(a)).toEqual([])
a = [1]
expect(ensureIsArray(a)).toBe(a)
})
test('ensureIsArray ensures obj prop is array', () => {
const a = { p: 1 }
expect(ensureIsArray(a, 'p')).toEqual({ p: [] })
})
test('no error when window is not defined', () => {
expect(hasGlobalWindowFn()).toBe(false)
})
/* eslint-disable no-extend-native */
test('find polyfill', () => {
const _find = Array.prototype.find
Array.prototype.find = false
const arr = [1, 2, 3]
expect(find(arr, (v, i) => i === 0)).toBe(1)
expect(find(arr, (v, i) => i === 3)).toBe(undefined)
Array.prototype.find = _find
})
test('findIndex polyfill', () => {
const _findIndex = Array.prototype.findIndex
Array.prototype.findIndex = false
const arr = [1, 2, 3]
expect(findIndex(arr, v => v === 2)).toBe(1)
expect(findIndex(arr, v => v === 4)).toBe(-1)
Array.prototype.findIndex = _findIndex
})
test('includes polyfill', () => {
const _includes = Array.prototype.includes
Array.prototype.includes = false
const arr = [1, 2, 3]
expect(includes(arr, 2)).toBe(true)
expect(includes(arr, 4)).toBe(false)
Array.prototype.includes = _includes
})
test('from/toArray polyfill', () => {
const _from = Array.from
Array.from = false
expect(toArray('foo')).toEqual(['f', 'o', 'o'])
Array.from = _from
})
/* eslint-enable no-extend-native */
})