forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
67 lines (43 loc) · 1.51 KB
/
index.test.ts
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
import { useBase64 } from '@vueuse/core'
import { describe, expect } from 'vitest'
function decode(encoded: string) {
const decodedStr = Buffer.from(encoded.split(',')[1], 'base64').toString('utf-8')
if (!decodedStr)
return ''
return JSON.parse(decodedStr)
}
describe('useBase64', () => {
it('should work with record', async () => {
const template = { test: 5 }
const { promise, base64 } = useBase64(template)
await promise.value
expect(decode(base64.value)).toEqual(template)
})
it('should work with map and default serialize function', async () => {
const map = new Map([['test', 1]])
const { promise, base64 } = useBase64(map)
await promise.value
expect(decode(base64.value)).toEqual(Object.fromEntries(map))
})
it('should work with set', async () => {
const set = new Set([1])
const { promise, base64 } = useBase64(set)
await promise.value
expect(decode(base64.value)).toEqual(Array.from(set))
})
it('should work with array', async () => {
const arr = [1, 2, 3]
const { promise, base64 } = useBase64(arr)
await promise.value
expect(decode(base64.value)).toEqual(arr)
})
it('should work with custom serialize function', async () => {
const arr = [1, 2, 3]
const serializer = (arr: number[]) => {
return JSON.stringify(arr.map(el => el * 2))
}
const { promise, base64 } = useBase64(arr, { serializer })
await promise.value
expect(decode(base64.value)).toEqual(JSON.parse(serializer(arr)))
})
})