mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-12 02:52:24 +03:00
fix: implement simply array polyfills (fixes #328)
This commit is contained in:
committed by
Alexander Lichter
parent
02c7beb6de
commit
d38f81e0a9
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @jest-environment node
|
||||
*/
|
||||
import { 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('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 */
|
||||
})
|
||||
Reference in New Issue
Block a user