-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathcomparisonSelection.test.ts
More file actions
65 lines (54 loc) · 1.87 KB
/
Copy pathcomparisonSelection.test.ts
File metadata and controls
65 lines (54 loc) · 1.87 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
import { describe, expect, it, beforeEach, vi } from 'vitest';
import {
COMPARISON_SELECTION_STORAGE_KEY,
loadComparisonSelection,
normalizeComparisonSelection,
saveComparisonSelection,
} from './comparisonSelection';
describe('comparison selection persistence', () => {
beforeEach(() => {
sessionStorage.clear();
localStorage.clear();
});
it('keeps only valid, unique IDs and trims the selection to 2', () => {
const normalized = normalizeComparisonSelection([
' line-1 ',
'line-1',
'',
'line-2',
'bad id',
'line-3',
'line-4',
'line-4',
]);
expect(normalized).toEqual(['line-1', 'line-2']);
});
it('reads persisted selections from sessionStorage and ignores bad payloads', () => {
sessionStorage.setItem(
COMPARISON_SELECTION_STORAGE_KEY,
JSON.stringify({ ids: ['line-1', 'bad id', 'line-2', 'line-2'] }),
);
expect(loadComparisonSelection()).toEqual(['line-1', 'line-2']);
});
it('writes to sessionStorage and never stores raw user account data in localStorage', () => {
const persisted = saveComparisonSelection(['line-1', 'line-2']);
expect(persisted).toBe(true);
expect(sessionStorage.getItem(COMPARISON_SELECTION_STORAGE_KEY)).toContain('line-1');
expect(localStorage.getItem(COMPARISON_SELECTION_STORAGE_KEY)).toBeNull();
});
it('rejects oversized or malicious IDs without throwing', () => {
const normalized = normalizeComparisonSelection([
'a'.repeat(256),
'line-1',
'line-2',
'line;drop table',
]);
expect(normalized).toEqual(['line-1', 'line-2']);
});
it('returns false when sessionStorage is unavailable', () => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new DOMException('Quota exceeded');
});
expect(saveComparisonSelection(['line-1', 'line-2'])).toBe(false);
});
});