-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.js
More file actions
183 lines (166 loc) Β· 5.93 KB
/
utils_test.js
File metadata and controls
183 lines (166 loc) Β· 5.93 KB
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
// MIT License
// Copyright (c) 2026 Open2b
// See the LICENSE file for full text.
import { assert, assertEquals } from '@std/assert'
import * as uuid from '@std/uuid/v4'
import { _uuid_imp, campaign, decodeBase64, encodeBase64, isURL, onVisibilityChange } from './utils.js'
import * as fake from './test_fake.js'
Deno.test('utils', async (t) => {
await t.step('campaign function', () => {
globalThis.location = { search: '' }
assertEquals(campaign().size, 0)
globalThis.location = { search: '?' }
assertEquals(campaign().size, 0)
globalThis.location = { search: '?a=b&c=d' }
assertEquals(campaign().size, 0)
globalThis.location = { search: '?utm_medium=social+network&utm_source=social&utm_campaign=paid' }
const values = campaign()
assertEquals(values.size, 3)
assertEquals(values.get('medium'), 'social network')
assertEquals(values.get('source'), 'social')
assertEquals(values.get('name'), 'paid')
})
await t.step('decodeBase64', () => {
// With TextDecoder.
assertEquals(decodeBase64(''), '')
assertEquals(decodeBase64('YQ'), 'a')
assertEquals(
decodeBase64('SGVsbG8hIPCfmIogVGhpcyBpcyBhIHRlc3QuIOS9oOWlvSDwn4yN'),
'Hello! π This is a test. δ½ ε₯½ π',
)
assertEquals(
decodeBase64(
'VGhlIHN1biBzZXRzIGJlaGluZCB0aGUgbW91bnRhaW5zLiDwn4yEIExldCdzIGdvIGZvciBhIPCfmpcgcmlkZSE',
),
"The sun sets behind the mountains. π Let's go for a π ride!",
)
// Without TextDecoder or TextEncoder.
const fns = ['TextDecoder', 'TextEncoder']
for (const i in fns) {
const fn = globalThis[fns[i]]
globalThis[fns[i]] = null
try {
assertEquals(decodeBase64(''), '')
assertEquals(decodeBase64('_YQA'), 'a')
assertEquals(
decodeBase64('_SABlAGwAbABvACEAIAA92AreIABUAGgAaQBzACAAaQBzACAAYQAgAHQAZQBzAHQALgAgAGBPfVkgADzYDd8'),
'Hello! π This is a test. δ½ ε₯½ π',
)
assertEquals(
decodeBase64(
'_VABoAGUAIABzAHUAbgAgAHMAZQB0AHMAIABiAGUAaABpAG4AZAAgAHQAaABlACAAbQBvAHUAbgB0AGEAaQBuAHMALgAgADzYBN8gAEwAZQB0ACcAcwAgAGcAbwAgAGYAbwByACAAYQAgAD3Yl94gAHIAaQBkAGUAIQA',
),
"The sun sets behind the mountains. π Let's go for a π ride!",
)
} finally {
globalThis[fns[i]] = fn
}
}
})
await t.step('encodeBase64', () => {
// With TextDecoder.
assertEquals(encodeBase64(''), '')
assertEquals(encodeBase64('a'), 'YQ')
assertEquals(
encodeBase64('Hello! π This is a test. δ½ ε₯½ π'),
'SGVsbG8hIPCfmIogVGhpcyBpcyBhIHRlc3QuIOS9oOWlvSDwn4yN',
)
assertEquals(
encodeBase64("The sun sets behind the mountains. π Let's go for a π ride!"),
'VGhlIHN1biBzZXRzIGJlaGluZCB0aGUgbW91bnRhaW5zLiDwn4yEIExldCdzIGdvIGZvciBhIPCfmpcgcmlkZSE',
)
// Without TextDecoder or TextEncoder.
const fns = ['TextDecoder', 'TextEncoder']
for (const i in fns) {
const fn = globalThis[fns[i]]
globalThis[fns[i]] = null
try {
assertEquals(encodeBase64(''), '')
assertEquals(encodeBase64('a'), '_YQA')
assertEquals(
encodeBase64('Hello! π This is a test. δ½ ε₯½ π'),
'_SABlAGwAbABvACEAIAA92AreIABUAGgAaQBzACAAaQBzACAAYQAgAHQAZQBzAHQALgAgAGBPfVkgADzYDd8',
)
assertEquals(
encodeBase64("The sun sets behind the mountains. π Let's go for a π ride!"),
'_VABoAGUAIABzAHUAbgAgAHMAZQB0AHMAIABiAGUAaABpAG4AZAAgAHQAaABlACAAbQBvAHUAbgB0AGEAaQBuAHMALgAgADzYBN8gAEwAZQB0ACcAcwAgAGcAbwAgAGYAbwByACAAYQAgAD3Yl94gAHIAaQBkAGUAIQA',
)
} finally {
globalThis[fns[i]] = fn
}
}
})
await t.step('isURL function', () => {
assert(isURL('http://example.com/'))
assert(isURL('https://example.com/'))
assert(!isURL('ftp://example.com/'))
assert(!isURL('/signup'))
assert(!isURL(''))
// Mock document.
globalThis.document = new fake.HTMLDocument()
const URL = globalThis.URL
globalThis.URL = undefined
try {
assert(isURL('http://example.com/'))
assert(isURL('https://example.com/'))
assert(!isURL('ftp://example.com/'))
assert(!isURL('/signup'))
assert(!isURL(''))
} finally {
delete (globalThis.document)
globalThis.URL = URL
}
})
await t.step('onVisibilityChange function', () => {
globalThis.document = {
visibilityState: 'visible',
addEventListener: addEventListener.bind(globalThis),
}
let isPageVisible = true
onVisibilityChange((visible) => {
assertEquals(visible, !isPageVisible)
isPageVisible = visible
})
document.visibilityState = 'hidden'
dispatchEvent(new Event('visibilitychange'))
assertEquals(isPageVisible, false)
document.visibilityState = 'visible'
dispatchEvent(new Event('visibilitychange'))
assertEquals(isPageVisible, true)
document.visibilityState = 'hidden'
dispatchEvent(new Event('pagehide'))
assertEquals(isPageVisible, false)
document.visibilityState = 'visible'
dispatchEvent(new Event('pageshow'))
assertEquals(isPageVisible, true)
})
await t.step('uuid function', () => {
const _randomUUID = globalThis.crypto.randomUUID.bind(globalThis.crypto)
const _getRandomValues = globalThis.crypto.getRandomValues.bind(globalThis.crypto)
const _URL = globalThis.URL
// Prepare the execution environment.
{
globalThis.crypto.randomUUID = undefined
assertEquals(globalThis.crypto.randomUUID, undefined)
globalThis.crypto.getRandomValues = undefined
assertEquals(globalThis.crypto.getRandomValues, undefined)
globalThis.URL = undefined
assertEquals(globalThis.URL, undefined)
}
// Test crypto.randomUUID implementation.
globalThis.crypto.randomUUID = _randomUUID
assert(uuid.validate(_uuid_imp()()))
globalThis.crypto.randomUUID = undefined
// Test crypto.getRandomValues implementation.
globalThis.crypto.getRandomValues = _getRandomValues
assert(uuid.validate(_uuid_imp()()))
globalThis.crypto.getRandomValues = undefined
// Test msCrypto.getRandomValues implementation.
globalThis.msCrypto = { getRandomValues: _getRandomValues }
assert(uuid.validate(_uuid_imp()()))
delete globalThis.msCrypto
// Test URL implementation.
globalThis.URL = _URL
assert(uuid.validate(_uuid_imp()()))
})
})