forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevbrowser.test.ts
More file actions
78 lines (66 loc) · 2.77 KB
/
Copy pathdevbrowser.test.ts
File metadata and controls
78 lines (66 loc) · 2.77 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
import { extractDevBrowserJWTFromURL, setDevBrowserJWTInURL } from '../devBrowser';
const DUMMY_URL_BASE = 'http://clerk-dummy';
describe('setDevBrowserJWTInURL(url, jwt)', () => {
const testCases: Array<[string, string, string]> = [
['', 'deadbeef', '?__clerk_db_jwt=deadbeef'],
['foo', 'deadbeef', 'foo?__clerk_db_jwt=deadbeef'],
['/foo', 'deadbeef', '/foo?__clerk_db_jwt=deadbeef'],
['#foo', 'deadbeef', '?__clerk_db_jwt=deadbeef#foo'],
['/foo?bar=42#qux', 'deadbeef', '/foo?bar=42&__clerk_db_jwt=deadbeef#qux'],
['/foo#__clerk_db_jwt[deadbeef2]', 'deadbeef', '/foo?__clerk_db_jwt=deadbeef#__clerk_db_jwt[deadbeef2]'],
[
'/foo?bar=42#qux__clerk_db_jwt[deadbeef2]',
'deadbeef',
'/foo?bar=42&__clerk_db_jwt=deadbeef#qux__clerk_db_jwt[deadbeef2]',
],
['/foo', 'deadbeef', '/foo?__clerk_db_jwt=deadbeef'],
['/foo?bar=42', 'deadbeef', '/foo?bar=42&__clerk_db_jwt=deadbeef'],
];
test.each(testCases)(
'sets the dev browser JWT at the end of the provided url. Params: url=(%s), jwt=(%s), expected url=(%s)',
(input, paramName, expected) => {
expect(setDevBrowserJWTInURL(new URL(input, DUMMY_URL_BASE), paramName).href).toEqual(
new URL(expected, DUMMY_URL_BASE).href,
);
},
);
});
const oldHistory = globalThis.history;
describe('getDevBrowserJWTFromURL(url)', () => {
const replaceStateMock = jest.fn();
beforeEach(() => {
const mockHistory = {
replaceState: replaceStateMock,
} as any;
Object.defineProperty(globalThis, 'history', { value: mockHistory });
});
afterEach(() => {
Object.defineProperty(globalThis, 'history', {
value: oldHistory,
});
replaceStateMock.mockReset();
});
it('it calls replaceState and clears the url if it contains any devBrowser related token', () => {
expect(extractDevBrowserJWTFromURL(new URL('/foo?__clerk_db_jwt=token', DUMMY_URL_BASE))).toEqual('token');
expect(replaceStateMock).toHaveBeenCalled();
});
it('it does not call replaceState if the clean url is the same as the current url', () => {
expect(extractDevBrowserJWTFromURL(new URL('/foo?__otherParam=hello', DUMMY_URL_BASE))).toEqual('');
expect(replaceStateMock).not.toHaveBeenCalled();
});
const testCases: Array<[string, string]> = [
['', ''],
['foo', ''],
['?__clerk_db_jwt=token', 'token'],
['foo?__clerk_db_jwt=token', 'token'],
['/foo?__clerk_db_jwt=token', 'token'],
['?__clerk_db_jwt=token#foo', 'token'],
['/foo?bar=42&__clerk_db_jwt=token#qux__clerk_db_jwt[token2]', 'token'],
];
test.each(testCases)(
'returns the dev browser JWT from a url and cleans all dev . Params: url=(%s), jwt=(%s)',
(input, jwt) => {
expect(extractDevBrowserJWTFromURL(new URL(input, DUMMY_URL_BASE))).toEqual(jwt);
},
);
});