-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathdevbrowser.test.ts
67 lines (57 loc) · 2.56 KB
/
devbrowser.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 { getDevBrowserJWTFromURL, setDevBrowserJWTInURL } from '../devBrowser';
describe('setDevBrowserJWTInURL(url, jwt)', () => {
const testCases: Array<[string, string, boolean, string]> = [
['', 'deadbeef', false, '#__clerk_db_jwt[deadbeef]'],
['foo', 'deadbeef', false, 'foo#__clerk_db_jwt[deadbeef]'],
['/foo', 'deadbeef', false, '/foo#__clerk_db_jwt[deadbeef]'],
['#foo', 'deadbeef', false, '#foo__clerk_db_jwt[deadbeef]'],
['/foo?bar=42#qux', 'deadbeef', false, '/foo?bar=42#qux__clerk_db_jwt[deadbeef]'],
['/foo#__clerk_db_jwt[deadbeef]', 'deadbeef', false, '/foo#__clerk_db_jwt[deadbeef]'],
['/foo?bar=42#qux__clerk_db_jwt[deadbeef]', 'deadbeef', false, '/foo?bar=42#qux__clerk_db_jwt[deadbeef]'],
['/foo', 'deadbeef', true, '/foo?__dev_session=deadbeef'],
['/foo?bar=42', 'deadbeef', true, '/foo?bar=42&__dev_session=deadbeef'],
];
test.each(testCases)(
'sets the dev browser JWT at the end of the provided url. Params: url=(%s), jwt=(%s), expected url=(%s)',
(hash, paramName, asQueryParam, expectedUrl) => {
expect(setDevBrowserJWTInURL(hash, paramName, asQueryParam)).toEqual(expectedUrl);
},
);
});
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('does not replaceState if the url does not contain a dev browser JWT', () => {
expect(getDevBrowserJWTFromURL('/foo')).toEqual('');
expect(replaceStateMock).not.toHaveBeenCalled();
});
const testCases: Array<[string, string, null | string]> = [
['', '', null],
['foo', '', null],
['#__clerk_db_jwt[deadbeef]', 'deadbeef', ''],
['foo#__clerk_db_jwt[deadbeef]', 'deadbeef', 'foo'],
['/foo#__clerk_db_jwt[deadbeef]', 'deadbeef', '/foo'],
['#foo__clerk_db_jwt[deadbeef]', 'deadbeef', '#foo'],
['/foo?bar=42#qux__clerk_db_jwt[deadbeef]', 'deadbeef', '/foo?bar=42#qux'],
];
test.each(testCases)('returns the dev browser JWT from a url. Params: url=(%s), jwt=(%s)', (url, jwt, calledWith) => {
expect(getDevBrowserJWTFromURL(url)).toEqual(jwt);
if (calledWith === null) {
expect(replaceStateMock).not.toHaveBeenCalled();
} else {
expect(replaceStateMock).toHaveBeenCalledWith(null, '', calledWith);
}
});
});