forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.test.ts
More file actions
28 lines (23 loc) · 889 Bytes
/
Copy pathstorage.test.ts
File metadata and controls
28 lines (23 loc) · 889 Bytes
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
import { describe, it, expect, beforeEach } from 'vitest';
import { readJson, writeJson, removeKey } from './storage';
describe('storage helpers', () => {
beforeEach(() => {
window.localStorage.clear();
});
it('round-trips JSON values through write/read', () => {
writeJson('user', { name: 'Ada', age: 36 });
expect(readJson('user', null)).toEqual({ name: 'Ada', age: 36 });
});
it('returns the fallback when the key is missing', () => {
expect(readJson('missing', 'fallback-value')).toBe('fallback-value');
});
it('returns the fallback when the stored value is malformed JSON', () => {
window.localStorage.setItem('broken', '{not json');
expect(readJson('broken', 42)).toBe(42);
});
it('removes a key', () => {
writeJson('temp', { ok: true });
removeKey('temp');
expect(window.localStorage.getItem('temp')).toBeNull();
});
});