login-authorization-v2 2.0.0-beta.1 → 2.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -47
- package/dist/index.d.ts +14 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +413 -497
- package/dist/index.umd.js +432 -537
- package/dist/src/compatible.d.ts +1 -1
- package/dist/src/compatible.d.ts.map +1 -1
- package/dist/src/cookie.d.ts +2 -2
- package/dist/src/cookie.d.ts.map +1 -1
- package/dist/src/request.d.ts.map +1 -1
- package/package.json +29 -25
- package/babel.config.js +0 -6
- package/coverage/clover.xml +0 -140
- package/coverage/coverage-final.json +0 -8
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/common-authorization-v2/index.html +0 -116
- package/coverage/lcov-report/common-authorization-v2/index.ts.html +0 -547
- package/coverage/lcov-report/common-authorization-v2/src/compatible.ts.html +0 -223
- package/coverage/lcov-report/common-authorization-v2/src/constance.ts.html +0 -106
- package/coverage/lcov-report/common-authorization-v2/src/cookie.ts.html +0 -346
- package/coverage/lcov-report/common-authorization-v2/src/dom.ts.html +0 -262
- package/coverage/lcov-report/common-authorization-v2/src/index.html +0 -206
- package/coverage/lcov-report/common-authorization-v2/src/request.ts.html +0 -361
- package/coverage/lcov-report/common-authorization-v2/src/shares.ts.html +0 -163
- package/coverage/lcov-report/common-authorization-v2/src/types.ts.html +0 -412
- package/coverage/lcov-report/common-authorization-v2/src/utils.ts.html +0 -145
- package/coverage/lcov-report/compatible.ts.html +0 -223
- package/coverage/lcov-report/constance.ts.html +0 -106
- package/coverage/lcov-report/cookie.ts.html +0 -310
- package/coverage/lcov-report/dom.ts.html +0 -262
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -206
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/shares.ts.html +0 -163
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -210
- package/coverage/lcov-report/types.ts.html +0 -412
- package/coverage/lcov-report/utils.ts.html +0 -145
- package/coverage/lcov.info +0 -268
- package/dist/compatible.d.ts +0 -10
- package/dist/compatible.d.ts.map +0 -1
- package/dist/constance.d.ts +0 -6
- package/dist/constance.d.ts.map +0 -1
- package/dist/cookie.d.ts +0 -8
- package/dist/cookie.d.ts.map +0 -1
- package/dist/dom.d.ts +0 -2
- package/dist/dom.d.ts.map +0 -1
- package/dist/index.js.LICENSE.txt +0 -1
- package/dist/request.d.ts +0 -36
- package/dist/request.d.ts.map +0 -1
- package/dist/shares.d.ts +0 -10
- package/dist/shares.d.ts.map +0 -1
- package/dist/test/cookie.test.d.ts +0 -2
- package/dist/test/cookie.test.d.ts.map +0 -1
- package/dist/test/shares.test.d.ts +0 -2
- package/dist/test/shares.test.d.ts.map +0 -1
- package/dist/types.d.ts +0 -89
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -2
- package/dist/utils.d.ts.map +0 -1
- package/index.html +0 -28
- package/index.ts +0 -154
- package/jest.config.js +0 -6
- package/src/compatible.ts +0 -46
- package/src/constance.ts +0 -7
- package/src/cookie.ts +0 -75
- package/src/dom.ts +0 -59
- package/src/request.ts +0 -99
- package/src/shares.ts +0 -26
- package/src/types.ts +0 -109
- package/src/utils.ts +0 -20
- package/tests/compatible.test.ts +0 -420
- package/tests/cookie.test.ts +0 -78
- package/tests/dom.test.ts +0 -95
- package/tests/index.test.ts +0 -126
- package/tests/shares.test.ts +0 -52
- package/tests/utils.test.ts +0 -79
- package/tsconfig.json +0 -49
- package/webpack.config.mjs +0 -47
package/tests/compatible.test.ts
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import { describe, expect, test, jest, beforeEach, afterEach } from '@jest/globals'
|
|
5
|
-
import * as cookieUtils from '../src/cookie'
|
|
6
|
-
import Cookie from 'js-cookie'
|
|
7
|
-
import {
|
|
8
|
-
setIdToken,
|
|
9
|
-
getIdToken,
|
|
10
|
-
setCookie,
|
|
11
|
-
clearLoginCookie,
|
|
12
|
-
getCookie,
|
|
13
|
-
getUrlParam
|
|
14
|
-
} from '../src/compatible'
|
|
15
|
-
|
|
16
|
-
// 模拟 js-cookie
|
|
17
|
-
jest.mock('js-cookie', () => ({
|
|
18
|
-
set: jest.fn(),
|
|
19
|
-
get: jest.fn(),
|
|
20
|
-
remove: jest.fn()
|
|
21
|
-
}))
|
|
22
|
-
|
|
23
|
-
// 模拟 cookie 模块
|
|
24
|
-
jest.mock('../src/cookie', () => ({
|
|
25
|
-
getAccessToken: jest.fn(),
|
|
26
|
-
setAccessToken: jest.fn(),
|
|
27
|
-
setRefreshToken: jest.fn()
|
|
28
|
-
}))
|
|
29
|
-
|
|
30
|
-
describe('compatibility.ts 单元测试', () => {
|
|
31
|
-
let originalLocation: Location
|
|
32
|
-
let originalDocumentCookie: string
|
|
33
|
-
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
// 保存原始对象
|
|
36
|
-
originalLocation = window.location
|
|
37
|
-
originalDocumentCookie = document.cookie
|
|
38
|
-
|
|
39
|
-
// 重置所有模拟
|
|
40
|
-
jest.clearAllMocks()
|
|
41
|
-
|
|
42
|
-
// 模拟 document.cookie
|
|
43
|
-
Object.defineProperty(document, 'cookie', {
|
|
44
|
-
writable: true,
|
|
45
|
-
value: ''
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
afterEach(() => {
|
|
50
|
-
// 恢复原始对象
|
|
51
|
-
window.location = originalLocation
|
|
52
|
-
document.cookie = originalDocumentCookie
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
describe('setIdToken 和 getIdToken', () => {
|
|
56
|
-
test('setIdToken 应该是 setAccessToken 的别名', () => {
|
|
57
|
-
const mockToken = 'test-token-123'
|
|
58
|
-
|
|
59
|
-
// 调用 setIdToken
|
|
60
|
-
setIdToken(mockToken)
|
|
61
|
-
|
|
62
|
-
// 验证 setAccessToken 被调用
|
|
63
|
-
expect(cookieUtils.setAccessToken).toHaveBeenCalledWith(mockToken)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test('getIdToken 应该是 getAccessToken 的别名', () => {
|
|
67
|
-
(cookieUtils.getAccessToken as jest.Mock).mockReturnValue('test-token-456')
|
|
68
|
-
|
|
69
|
-
const result = getIdToken()
|
|
70
|
-
|
|
71
|
-
expect(cookieUtils.getAccessToken).toHaveBeenCalled()
|
|
72
|
-
expect(result).toBe('test-token-456')
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('setIdToken 传递 null 应该调用 setAccessToken(null)', () => {
|
|
76
|
-
setIdToken(null)
|
|
77
|
-
expect(cookieUtils.setAccessToken).toHaveBeenCalledWith(null)
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
describe('setCookie 函数', () => {
|
|
82
|
-
test('设置 cookie 应该调用 Cookie.set 正确参数', () => {
|
|
83
|
-
const name = 'testCookie'
|
|
84
|
-
const value = 'testValue'
|
|
85
|
-
const domain = 'example.com'
|
|
86
|
-
const path = '/test'
|
|
87
|
-
const time = 7 * 24 * 60 * 60 * 1000 // 7天
|
|
88
|
-
|
|
89
|
-
setCookie(name, value, domain, path, time)
|
|
90
|
-
|
|
91
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
92
|
-
name,
|
|
93
|
-
value,
|
|
94
|
-
{
|
|
95
|
-
domain,
|
|
96
|
-
path,
|
|
97
|
-
expires: 7 // time / (24 * 60 * 60 * 1000) = 7
|
|
98
|
-
}
|
|
99
|
-
)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test('setCookie 使用默认路径和时间', () => {
|
|
103
|
-
const name = 'testCookie'
|
|
104
|
-
const value = 'testValue'
|
|
105
|
-
const domain = 'example.com'
|
|
106
|
-
|
|
107
|
-
setCookie(name, value, domain)
|
|
108
|
-
|
|
109
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
110
|
-
name,
|
|
111
|
-
value,
|
|
112
|
-
{
|
|
113
|
-
domain,
|
|
114
|
-
path: '/',
|
|
115
|
-
expires: 30 // 默认时间 30天
|
|
116
|
-
}
|
|
117
|
-
)
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
test('setCookie 值为 null 应该删除 cookie', () => {
|
|
121
|
-
const name = 'testCookie'
|
|
122
|
-
const domain = 'example.com'
|
|
123
|
-
|
|
124
|
-
setCookie(name, null, domain)
|
|
125
|
-
|
|
126
|
-
expect(Cookie.remove).toHaveBeenCalledWith(name)
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
test('setCookie 值为 undefined 应该删除 cookie', () => {
|
|
130
|
-
const name = 'testCookie'
|
|
131
|
-
const domain = 'example.com'
|
|
132
|
-
|
|
133
|
-
setCookie(name, undefined, domain)
|
|
134
|
-
|
|
135
|
-
expect(Cookie.remove).toHaveBeenCalledWith(name)
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
test('setCookie 值为空字符串应该正常设置', () => {
|
|
139
|
-
const name = 'testCookie'
|
|
140
|
-
const value = ''
|
|
141
|
-
const domain = 'example.com'
|
|
142
|
-
|
|
143
|
-
setCookie(name, value, domain)
|
|
144
|
-
|
|
145
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
146
|
-
name,
|
|
147
|
-
value,
|
|
148
|
-
expect.objectContaining({
|
|
149
|
-
domain,
|
|
150
|
-
path: '/',
|
|
151
|
-
expires: 30
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
test('setCookie 时间计算正确', () => {
|
|
157
|
-
const name = 'testCookie'
|
|
158
|
-
const value = 'testValue'
|
|
159
|
-
const domain = 'example.com'
|
|
160
|
-
|
|
161
|
-
// 测试 1天
|
|
162
|
-
setCookie(name, value, domain, '/', 24 * 60 * 60 * 1000)
|
|
163
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
164
|
-
name,
|
|
165
|
-
value,
|
|
166
|
-
expect.objectContaining({
|
|
167
|
-
expires: 1
|
|
168
|
-
})
|
|
169
|
-
)
|
|
170
|
-
|
|
171
|
-
// 测试 0天
|
|
172
|
-
setCookie(name, value, domain, '/', 0)
|
|
173
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
174
|
-
name,
|
|
175
|
-
value,
|
|
176
|
-
expect.objectContaining({
|
|
177
|
-
expires: 0
|
|
178
|
-
})
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
// 测试半天
|
|
182
|
-
setCookie(name, value, domain, '/', 12 * 60 * 60 * 1000)
|
|
183
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
184
|
-
name,
|
|
185
|
-
value,
|
|
186
|
-
expect.objectContaining({
|
|
187
|
-
expires: 0.5
|
|
188
|
-
})
|
|
189
|
-
)
|
|
190
|
-
})
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
describe('clearLoginCookie 函数', () => {
|
|
194
|
-
test('应该调用 setAccessToken(null) 和 setRefreshToken(null)', () => {
|
|
195
|
-
clearLoginCookie()
|
|
196
|
-
|
|
197
|
-
expect(cookieUtils.setAccessToken).toHaveBeenCalledWith(null)
|
|
198
|
-
expect(cookieUtils.setRefreshToken).toHaveBeenCalledWith(null)
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
test('应该按照正确顺序调用', () => {
|
|
202
|
-
clearLoginCookie()
|
|
203
|
-
|
|
204
|
-
// 获取调用顺序
|
|
205
|
-
const calls = (cookieUtils.setAccessToken as jest.Mock).mock.invocationCallOrder[0]
|
|
206
|
-
const calls2 = (cookieUtils.setRefreshToken as jest.Mock).mock.invocationCallOrder[0]
|
|
207
|
-
|
|
208
|
-
// 两者都被调用,但顺序不重要
|
|
209
|
-
expect(cookieUtils.setAccessToken).toHaveBeenCalled()
|
|
210
|
-
expect(cookieUtils.setRefreshToken).toHaveBeenCalled()
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe('getCookie 函数', () => {
|
|
215
|
-
test('从 document.cookie 中获取 cookie 值', () => {
|
|
216
|
-
// 设置 document.cookie
|
|
217
|
-
document.cookie = 'testCookie=testValue; otherCookie=otherValue'
|
|
218
|
-
|
|
219
|
-
const result = getCookie('testCookie')
|
|
220
|
-
|
|
221
|
-
expect(result).toBe('testValue')
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
test('获取不存在的 cookie 返回 null', () => {
|
|
225
|
-
document.cookie = 'testCookie=testValue'
|
|
226
|
-
|
|
227
|
-
const result = getCookie('nonExistentCookie')
|
|
228
|
-
|
|
229
|
-
expect(result).toBeNull()
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
test('处理 document.cookie 为空的情况', () => {
|
|
233
|
-
document.cookie = ''
|
|
234
|
-
|
|
235
|
-
const result = getCookie('testCookie')
|
|
236
|
-
|
|
237
|
-
expect(result).toBeNull()
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
test('处理 cookie 值中包含特殊字符', () => {
|
|
241
|
-
const encodedValue = encodeURIComponent('test value with spaces & symbols=+')
|
|
242
|
-
document.cookie = `testCookie=${encodedValue}`
|
|
243
|
-
|
|
244
|
-
const result = getCookie('testCookie')
|
|
245
|
-
|
|
246
|
-
expect(result).toBe('test value with spaces & symbols=+')
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
test('处理多个 cookie 的情况', () => {
|
|
250
|
-
document.cookie = 'cookie1=value1; cookie2=value2; cookie3=value3'
|
|
251
|
-
|
|
252
|
-
const result1 = getCookie('cookie1')
|
|
253
|
-
const result2 = getCookie('cookie2')
|
|
254
|
-
const result3 = getCookie('cookie3')
|
|
255
|
-
|
|
256
|
-
expect(result1).toBe('value1')
|
|
257
|
-
expect(result2).toBe('value2')
|
|
258
|
-
expect(result3).toBe('value3')
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
test('处理 cookie 名称出现在值中的情况', () => {
|
|
262
|
-
document.cookie = 'testCookie=testValue; otherCookie=testCookie=notTheCookieName'
|
|
263
|
-
|
|
264
|
-
const result = getCookie('testCookie')
|
|
265
|
-
|
|
266
|
-
expect(result).toBe('testValue')
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
test('处理没有分号结尾的 cookie', () => {
|
|
270
|
-
document.cookie = 'testCookie=testValue'
|
|
271
|
-
|
|
272
|
-
const result = getCookie('testCookie')
|
|
273
|
-
|
|
274
|
-
expect(result).toBe('testValue')
|
|
275
|
-
})
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
describe('getUrlParam 函数', () => {
|
|
279
|
-
test('从 URL 查询参数中获取值', () => {
|
|
280
|
-
const name = getUrlParam('name', 'http://example.com/path?name=John&age=30')
|
|
281
|
-
const age = getUrlParam('age', 'http://example.com/path?name=John&age=30')
|
|
282
|
-
|
|
283
|
-
expect(name).toBe('John')
|
|
284
|
-
expect(age).toBe('30')
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
test('获取不存在的参数返回 null', () => {
|
|
288
|
-
const result = getUrlParam('nonExistent', 'http://example.com/path?name=John')
|
|
289
|
-
|
|
290
|
-
expect(result).toBeNull()
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
test('处理没有查询参数的 URL', () => {
|
|
294
|
-
const result = getUrlParam('name', 'http://example.com/path')
|
|
295
|
-
|
|
296
|
-
expect(result).toBeNull()
|
|
297
|
-
})
|
|
298
|
-
|
|
299
|
-
test('处理哈希后面的查询参数', () => {
|
|
300
|
-
const result = getUrlParam('notThis', 'http://example.com/#/pathsection?notThis=value&alsoNot=this')
|
|
301
|
-
|
|
302
|
-
expect(result).toBe('value') // 因为查询参数在哈希后面,不会被解析
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
test('处理空值的参数', () => {
|
|
306
|
-
const empty = getUrlParam('empty', 'http://example.com/path?empty=&null=null')
|
|
307
|
-
const nullParam = getUrlParam('null', 'http://example.com/path?empty=&null=null')
|
|
308
|
-
|
|
309
|
-
expect(empty).toBe('')
|
|
310
|
-
expect(nullParam).toBe('null')
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
test('处理编码的参数值', () => {
|
|
314
|
-
const name = getUrlParam('name', 'http://example.com/path?name=John%20Doe&city=New%20York')
|
|
315
|
-
const city = getUrlParam('city', 'http://example.com/path?name=John%20Doe&city=New%20York')
|
|
316
|
-
|
|
317
|
-
expect(name).toBe('John Doe')
|
|
318
|
-
expect(city).toBe('New York')
|
|
319
|
-
})
|
|
320
|
-
|
|
321
|
-
test('处理 URL 中有多个参数的情况', () => {
|
|
322
|
-
const a = getUrlParam('a', 'http://example.com/path?a=1&b=2&c=3&d=4&e=5')
|
|
323
|
-
const e = getUrlParam('e', 'http://example.com/path?a=1&b=2&c=3&d=4&e=5')
|
|
324
|
-
|
|
325
|
-
expect(a).toBe('1')
|
|
326
|
-
expect(e).toBe('5')
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
test('处理 URL 中带端口和路径的情况', () => {
|
|
330
|
-
const result = getUrlParam('param', 'http://example.com:8080/path/to/page?param=value')
|
|
331
|
-
|
|
332
|
-
expect(result).toBe('value')
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
test('处理 HTTPS URL', () => {
|
|
336
|
-
const result = getUrlParam('secure', 'https://example.com/path?secure=yes')
|
|
337
|
-
|
|
338
|
-
expect(result).toBe('yes')
|
|
339
|
-
})
|
|
340
|
-
})
|
|
341
|
-
|
|
342
|
-
describe('集成测试', () => {
|
|
343
|
-
test('setCookie 和 getCookie 配合工作', () => {
|
|
344
|
-
// 模拟 Cookie.set 来设置 document.cookie
|
|
345
|
-
(Cookie.set as jest.Mock).mockImplementation((name, value, options: any) => {
|
|
346
|
-
document.cookie = `${name}=${value}; domain=${options.domain}; path=${options.path}`
|
|
347
|
-
})
|
|
348
|
-
|
|
349
|
-
// 设置 cookie
|
|
350
|
-
setCookie('test', 'value', 'example.com')
|
|
351
|
-
|
|
352
|
-
// 使用 getCookie 获取
|
|
353
|
-
const result = getCookie('test')
|
|
354
|
-
|
|
355
|
-
expect(result).toBe('value')
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
test('clearLoginCookie 清除所有登录相关 cookie', () => {
|
|
359
|
-
clearLoginCookie()
|
|
360
|
-
|
|
361
|
-
expect(cookieUtils.setAccessToken).toHaveBeenCalledWith(null)
|
|
362
|
-
expect(cookieUtils.setRefreshToken).toHaveBeenCalledWith(null)
|
|
363
|
-
})
|
|
364
|
-
})
|
|
365
|
-
|
|
366
|
-
describe('错误处理和边缘情况', () => {
|
|
367
|
-
test('getCookie 处理空的 cookie 名称', () => {
|
|
368
|
-
document.cookie = '=value' // 无效的 cookie
|
|
369
|
-
|
|
370
|
-
const result = getCookie('')
|
|
371
|
-
|
|
372
|
-
// 根据实现,这应该返回 null,因为空字符串不是有效的 cookie 名称
|
|
373
|
-
expect(result).toBeNull()
|
|
374
|
-
})
|
|
375
|
-
|
|
376
|
-
test('getCookie 处理 cookie 值包含等号的情况', () => {
|
|
377
|
-
const complexValue = 'a=b&c=d'
|
|
378
|
-
document.cookie = `complex=${encodeURIComponent(complexValue)}`
|
|
379
|
-
|
|
380
|
-
const result = getCookie('complex')
|
|
381
|
-
|
|
382
|
-
expect(result).toBe(complexValue)
|
|
383
|
-
})
|
|
384
|
-
|
|
385
|
-
test('getUrlParam 处理 window.location.href 没有查询字符串的情况', () => {
|
|
386
|
-
const result = getUrlParam('any', 'http://example.com/path')
|
|
387
|
-
|
|
388
|
-
expect(result).toBeNull()
|
|
389
|
-
})
|
|
390
|
-
|
|
391
|
-
test('getUrlParam 处理 window.location.href 只有问号的情况', () => {
|
|
392
|
-
const result = getUrlParam('any', 'http://example.com/path?')
|
|
393
|
-
|
|
394
|
-
expect(result).toBeNull()
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
test('getUrlParam 处理 window.location.href 有空查询参数的情况', () => {
|
|
398
|
-
const result = getUrlParam('', 'http://example.com/path?&')
|
|
399
|
-
|
|
400
|
-
expect(result).toBeNull()
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
test('setCookie 处理异常时间值', () => {
|
|
404
|
-
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
405
|
-
|
|
406
|
-
// 测试 NaN
|
|
407
|
-
setCookie('test', 'value', 'example.com', '/', NaN as any)
|
|
408
|
-
|
|
409
|
-
expect(Cookie.set).toHaveBeenCalledWith(
|
|
410
|
-
'test',
|
|
411
|
-
'value',
|
|
412
|
-
expect.objectContaining({
|
|
413
|
-
expires: NaN
|
|
414
|
-
})
|
|
415
|
-
)
|
|
416
|
-
|
|
417
|
-
consoleSpy.mockRestore()
|
|
418
|
-
})
|
|
419
|
-
})
|
|
420
|
-
})
|
package/tests/cookie.test.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import Cookies from 'js-cookie'
|
|
5
|
-
import { describe, expect, test } from '@jest/globals'
|
|
6
|
-
import { setAccessToken, setRefreshToken } from '../src/cookie'
|
|
7
|
-
import { getUserInfo } from '../src/utils'
|
|
8
|
-
import { INVALID_REFRESH_TOKEN, INVALID_ACCESS_TOKEN } from '../src/constance'
|
|
9
|
-
|
|
10
|
-
const accessToken = 'eyJraWQiOiJUQTVBanVVYURheGlZdFVYR3l6ajlEZEdPZFJIYUVlUTRTRnF6VWdzc0ZNPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJkNzQ0M2FmOC1mMGExLTcwMzItZGIzMC0wM2Y3NDczZTY0ZDciLCJjb2duaXRvOmdyb3VwcyI6WyJBZ2VudFBvcnRhbCIsIkZpbmFuY2UiLCJUaWNrZXQiLCJQb3J0YWwiXSwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5hcC1ub3J0aGVhc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ERJAVYJ20Ji2F54KDYPr17Cv3ltz80qqqPN_1QI5oRszt6xU7IjTCp47YGVAHt7RcfR2FemiodpW3_-Xm2PYgo0rwgkEX7z7fZ6Ban1jSeZsYD0vPrlf6nlD8QvA7bP6acZufmfok5oMJ2SC5hv7mGjggd9HT8h3yj8yH4exfga2BSIHTwgNXU-cBw_mQyZCScELof20HJdpE8f3TzOHZfURn1uJdKSZ0tZAdIaTqdHIUlEgUZ8xok7JRpqayVsXve1GvCaZylG0Mw9FTx5DJRi6IblXVdL7yZwLFk8FUYcn4KqtPlamoxR9rpGGMtXIexY4yN99v-hc8_gn3ilYMeA3-a4cFx1mmld8VRQJeD7q_u0lRheDBqW9K6JRBjn8s59hLfyoiRPMi_NZFc2OJNwJofNJbmTcfQmR1dQtsTh2Rtv4krG2YnczzGmPZJppOpaebictrv_ogc93NnXZqJtZGAbSy4RdQYj3Fnx17EY-NSU-VQerlhgV_e35cz8ZNmsiISq7Lp6zUTQwr6ASztyLQ8i_71Xr0OXgf4l4qXWyVQJhbOTJdIUyX5s3ebJruU1qSwPG20Fd3Pm6boIbIlsGHWM6VvRxON8CbJ0PGU_rcIJPVkEGZ0MoiMGvh3BnFYaZYtSPD5p7F9w_LBMRdzb3JimdzNtn48jGYG.XHICmXlRw5-f1fQVFS1Zdw'
|
|
12
|
-
|
|
13
|
-
describe('cookie.ts access token(短token)测试', () => {
|
|
14
|
-
test('测试不合法的 access token 流程', () => {
|
|
15
|
-
setAccessToken(accessToken)
|
|
16
|
-
expect(() => setAccessToken('invalidToken')).toThrow(INVALID_ACCESS_TOKEN)
|
|
17
|
-
expect(Cookies.get('access_token')).toBe(accessToken)
|
|
18
|
-
|
|
19
|
-
setAccessToken(0)
|
|
20
|
-
expect(Cookies.get('access_token')).toBeUndefined()
|
|
21
|
-
expect(() => setAccessToken({})).toThrow()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test('测试设置合法的 access token 流程', async () => {
|
|
25
|
-
setAccessToken('')
|
|
26
|
-
expect(Cookies.get('access_token')).toBeUndefined()
|
|
27
|
-
|
|
28
|
-
setAccessToken(accessToken)
|
|
29
|
-
expect(Cookies.get('access_token')).toBe(accessToken)
|
|
30
|
-
|
|
31
|
-
const userInfo = await getUserInfo()
|
|
32
|
-
|
|
33
|
-
expect(userInfo).not.toBeNull()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('测试清除 access token 流程', () => {
|
|
37
|
-
setAccessToken(accessToken)
|
|
38
|
-
expect(Cookies.get('access_token')).toBe(accessToken)
|
|
39
|
-
setAccessToken('')
|
|
40
|
-
expect(Cookies.get('access_token')).toBeUndefined()
|
|
41
|
-
|
|
42
|
-
setAccessToken(accessToken)
|
|
43
|
-
expect(Cookies.get('access_token')).toBe(accessToken)
|
|
44
|
-
setAccessToken(null)
|
|
45
|
-
expect(Cookies.get('access_token')).toBeUndefined()
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
describe('cookie.ts refresh token tests', () => {
|
|
50
|
-
test('测试不合法的 refresh token 流程', () => {
|
|
51
|
-
setRefreshToken(refreshToken)
|
|
52
|
-
expect(() => setRefreshToken('invalidToken')).toThrow(INVALID_REFRESH_TOKEN)
|
|
53
|
-
|
|
54
|
-
setRefreshToken(0)
|
|
55
|
-
expect(Cookies.get('refresh_token')).toBeUndefined()
|
|
56
|
-
expect(() => setRefreshToken({})).toThrow()
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
test('测试设置合法的 refresh token 流程', async () => {
|
|
60
|
-
setRefreshToken('')
|
|
61
|
-
expect(Cookies.get('refresh_token')).toBeUndefined()
|
|
62
|
-
|
|
63
|
-
setRefreshToken(refreshToken)
|
|
64
|
-
expect(Cookies.get('refresh_token')).toBe(refreshToken)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('测试清除 refresh token 流程', () => {
|
|
68
|
-
setRefreshToken(refreshToken)
|
|
69
|
-
expect(Cookies.get('refresh_token')).toBe(refreshToken)
|
|
70
|
-
setRefreshToken('')
|
|
71
|
-
expect(Cookies.get('refresh_token')).toBeUndefined()
|
|
72
|
-
|
|
73
|
-
setRefreshToken(refreshToken)
|
|
74
|
-
expect(Cookies.get('refresh_token')).toBe(refreshToken)
|
|
75
|
-
setRefreshToken(null)
|
|
76
|
-
expect(Cookies.get('refresh_token')).toBeUndefined()
|
|
77
|
-
})
|
|
78
|
-
})
|
package/tests/dom.test.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import { describe, expect, test, jest, beforeEach, afterEach } from '@jest/globals'
|
|
5
|
-
import { closeDialog, openDialog } from '../src/dom'
|
|
6
|
-
|
|
7
|
-
// 模拟 utils 模块中的 isMobile 函数
|
|
8
|
-
jest.mock('../src/utils', () => ({
|
|
9
|
-
isMobile: jest.fn()
|
|
10
|
-
}))
|
|
11
|
-
|
|
12
|
-
// 模拟 shares 模块中的 loginPageUrl
|
|
13
|
-
jest.mock('../src/shares', () => ({
|
|
14
|
-
loginPageUrl: 'https://example.com/login'
|
|
15
|
-
}))
|
|
16
|
-
|
|
17
|
-
// 导入被模拟后的模块
|
|
18
|
-
import { isMobile } from '../src/utils'
|
|
19
|
-
|
|
20
|
-
describe('dom.ts 方法单元测试', () => {
|
|
21
|
-
let originalLocation: Location
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
originalLocation = window.location
|
|
25
|
-
|
|
26
|
-
document.body.innerHTML = ''
|
|
27
|
-
|
|
28
|
-
// 重置所有模拟
|
|
29
|
-
jest.clearAllMocks()
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
describe('openDialog', () => {
|
|
33
|
-
test('应该在 PC 端正确创建对话框', () => {
|
|
34
|
-
// 模拟 PC 端
|
|
35
|
-
(isMobile as jest.Mock).mockReturnValue(false)
|
|
36
|
-
|
|
37
|
-
// 执行
|
|
38
|
-
openDialog()
|
|
39
|
-
|
|
40
|
-
// 验证对话框容器是否存在
|
|
41
|
-
const container = document.querySelector('div > div#confirm-container')
|
|
42
|
-
expect(container).not.toBeNull()
|
|
43
|
-
|
|
44
|
-
// 验证 PC 端特定样式
|
|
45
|
-
expect(container?.outerHTML).toContain('width:615px')
|
|
46
|
-
expect(container?.outerHTML).toContain('left: 35%')
|
|
47
|
-
|
|
48
|
-
// 验证按钮存在
|
|
49
|
-
const confirmButton = document.querySelector('#confirm') as HTMLButtonElement
|
|
50
|
-
const cancelButton = document.querySelector('#cancel') as HTMLButtonElement
|
|
51
|
-
expect(confirmButton).not.toBeNull()
|
|
52
|
-
expect(cancelButton).not.toBeNull()
|
|
53
|
-
|
|
54
|
-
// 验证文本内容
|
|
55
|
-
expect(container?.textContent).toContain('You do not have access to the system.')
|
|
56
|
-
expect(container?.textContent).toContain('Sign in to another ZERO account')
|
|
57
|
-
expect(container?.textContent).toContain('Cancel')
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('应该在移动端正确创建对话框', () => {
|
|
61
|
-
(isMobile as jest.Mock).mockReturnValue(true)
|
|
62
|
-
|
|
63
|
-
openDialog()
|
|
64
|
-
|
|
65
|
-
const container = document.querySelector('div > div#confirm-container')
|
|
66
|
-
expect(container).not.toBeNull()
|
|
67
|
-
|
|
68
|
-
expect(container?.outerHTML).toContain('width:97.6vw')
|
|
69
|
-
expect(container?.outerHTML).toContain('height:145px')
|
|
70
|
-
|
|
71
|
-
const confirmButton = document.querySelector('#confirm')
|
|
72
|
-
const cancelButton = document.querySelector('#cancel')
|
|
73
|
-
|
|
74
|
-
expect(confirmButton).not.toBeNull()
|
|
75
|
-
expect(cancelButton).not.toBeNull()
|
|
76
|
-
|
|
77
|
-
expect(container?.textContent).toContain('You do not have access to the system.')
|
|
78
|
-
expect(container?.textContent).toContain('Sign in to another ZERO account')
|
|
79
|
-
expect(container?.textContent).toContain('Cancel')
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test('close dialog 应该正常关闭', () => {
|
|
83
|
-
(isMobile as jest.Mock).mockReturnValue(false)
|
|
84
|
-
|
|
85
|
-
openDialog()
|
|
86
|
-
let container = document.querySelector('div > div#confirm-container')
|
|
87
|
-
|
|
88
|
-
expect(container).not.toBeNull()
|
|
89
|
-
|
|
90
|
-
closeDialog()
|
|
91
|
-
container = document.querySelector('div > div#confirm-container')
|
|
92
|
-
expect(container).toBeNull()
|
|
93
|
-
})
|
|
94
|
-
})
|
|
95
|
-
})
|