File size: 4,281 Bytes
1e92f2d |
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 |
import { describe, expect, test } from 'vitest'
import { reactive, ref } from 'vue-demi'
import { cloneDeep, cloneDeepUnref, updateState } from '../utils'
describe('utils', () => {
describe('updateState', () => {
test('should update first object with values from the second one', () => {
const origin = { option1: 'a', option2: 'b', option3: 'c' }
const update = { option1: 'x', option2: 'y', option3: 'z' }
const expected = { option1: 'x', option2: 'y', option3: 'z' }
updateState(origin, update)
expect(origin).toEqual(expected)
})
test('should update only existing keys', () => {
const origin = { option1: 'a', option2: 'b' }
const update = { option1: 'x', option2: 'y', option3: 'z' }
const expected = { option1: 'x', option2: 'y' }
updateState(origin, update)
expect(origin).toEqual(expected)
})
test('should remove non existing keys', () => {
const origin = { option1: 'a', option2: 'b', option3: 'c' }
const update = { option1: 'x', option2: 'y' }
const expected = { option1: 'x', option2: 'y' }
updateState(origin, update)
expect(origin).toEqual(expected)
})
})
describe('cloneDeep', () => {
test('should copy primitives and functions AS-IS', () => {
expect(cloneDeep(3456)).toBe(3456)
expect(cloneDeep('theString')).toBe('theString')
expect(cloneDeep(null)).toBe(null)
})
test('should copy Maps and Sets AS-IS', () => {
const setVal = new Set([3, 4, 5])
const setValCopy = cloneDeep(setVal)
expect(setValCopy).toBe(setVal)
expect(setValCopy).toStrictEqual(new Set([3, 4, 5]))
const mapVal = new Map([
['a', 'aVal'],
['b', 'bVal'],
])
const mapValCopy = cloneDeep(mapVal)
expect(mapValCopy).toBe(mapVal)
expect(mapValCopy).toStrictEqual(
new Map([
['a', 'aVal'],
['b', 'bVal'],
]),
)
})
test('should deeply copy arrays', () => {
const val = [
25,
'str',
null,
new Set([3, 4]),
[5, 6, { a: 1 }],
undefined,
]
const cp = cloneDeep(val)
expect(cp).toStrictEqual([
25,
'str',
null,
new Set([3, 4]),
[5, 6, { a: 1 }],
undefined,
])
expect(cp).not.toBe(val)
expect(cp[3]).toBe(val[3]) // Set([3, 4])
expect(cp[4]).not.toBe(val[4]) // [5, 6, { a: 1 }]
expect((cp[4] as Array<number>)[2]).not.toBe((val[4] as Array<number>)[2]) // { a : 1 }
})
test('should deeply copy object', () => {
const val = reactive({
a: 25,
b: 'str',
c: null,
d: undefined,
e: new Set([5, 6]),
f: [3, 4],
g: { fa: 26 },
})
const cp = cloneDeep(val)
expect(cp).toStrictEqual({
a: 25,
b: 'str',
c: null,
d: undefined,
e: new Set([5, 6]),
f: [3, 4],
g: { fa: 26 },
})
expect(cp.e).toBe(val.e) // Set
expect(cp.f).not.toBe(val.f) // []
expect(cp.g).not.toBe(val.g) // {}
})
})
describe('cloneDeepUnref', () => {
test('should unref primitives', () => {
expect(cloneDeepUnref(ref(34))).toBe(34)
expect(cloneDeepUnref(ref('myStr'))).toBe('myStr')
})
test('should deeply unref arrays', () => {
const val = ref([2, 3, ref(4), ref('5'), { a: ref(6) }, [ref(7)]])
const cp = cloneDeepUnref(val)
expect(cp).toStrictEqual([2, 3, 4, '5', { a: 6 }, [7]])
})
test('should deeply unref objects', () => {
const val = ref({
a: 1,
b: ref(2),
c: [ref('c1'), ref(['c2'])],
d: {
e: ref('e'),
},
})
const cp = cloneDeepUnref(val)
expect(cp).toEqual({
a: 1,
b: 2,
c: ['c1', ['c2']],
d: { e: 'e' },
})
})
test('should clone getters returning values in queryKey', () => {
const val = ref({ queryKey: [1, 2, () => '3'] })
const cp = cloneDeepUnref(val)
expect(cp).toStrictEqual({ queryKey: [1, 2, '3'] })
})
test('should unref undefined', () => {
expect(cloneDeepUnref(ref(undefined))).toBe(undefined)
})
})
})
|