-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.test.js
More file actions
64 lines (53 loc) · 1.77 KB
/
session.test.js
File metadata and controls
64 lines (53 loc) · 1.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
/* --------------------------------------------------------------------------------
* Created on Fri Jan 24 2025
*
* Copyright (c) 2025 Colorado State University. All rights reserved. (1)
*
* Contributors:
* Mackenzie Grimes (1)
*
* --------------------------------------------------------------------------------
*/
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { getSessionId, setSessionId, clearSessionId } from './session';
const EXAMPLE_SESSION = 'abcd';
const EXAMPLE_CLIENT = 'TEST_CLIENT';
// build a simulated localStorage system using a regular {}
const sessionStorageMock = () => {
let store = {};
return {
getItem: (key) => store[key] || null,
setItem: (key, value) => {
store[key] = value.toString();
},
removeItem: (key) => {
if (Object.keys(store).includes(key)) {
delete store[key];
}
},
};
};
beforeEach(() => {
vi.mock('sessionStorage', () => vi.fn().mockImplementation(() => sessionStorageMock()));
});
describe('setSessionId', () => {
it('sets value in localStorage', () => {
// mock window.location to have no search params
vi.spyOn(window, 'location', 'get').mockReturnValue({ search: '' });
let actualSession = getSessionId(EXAMPLE_CLIENT);
expect(actualSession).toBe(null);
setSessionId(EXAMPLE_SESSION);
actualSession = getSessionId(EXAMPLE_CLIENT);
expect(actualSession).toBe(EXAMPLE_SESSION);
});
});
describe('clearSessionId', () => {
it('clears an existing session', () => {
setSessionId(EXAMPLE_SESSION);
let actualSession = getSessionId(EXAMPLE_CLIENT);
expect(actualSession).toBe(EXAMPLE_SESSION);
clearSessionId();
actualSession = getSessionId(EXAMPLE_CLIENT);
expect(actualSession).toBe(null);
});
});