-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhawk-local-storage.test.ts
More file actions
75 lines (57 loc) · 2.37 KB
/
hawk-local-storage.test.ts
File metadata and controls
75 lines (57 loc) · 2.37 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
import type { Mock } from 'vitest';
import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';
import { HawkLocalStorage } from '../../src/storages/hawk-local-storage';
describe('HawkLocalStorage', () => {
let getItemSpy: Mock<(key: string) => string | null>;
let setItemSpy: Mock<(key: string, value: string) => void>;
let removeItemSpy: Mock<(key: string) => void>;
let storage: HawkLocalStorage;
beforeEach(() => {
localStorage.clear();
storage = new HawkLocalStorage();
getItemSpy = vi.spyOn(Storage.prototype, 'getItem');
setItemSpy = vi.spyOn(Storage.prototype, 'setItem');
removeItemSpy = vi.spyOn(Storage.prototype, 'removeItem');
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should return null when key does not exist', () => {
expect(storage.getItem('foo')).toBeNull();
expect(getItemSpy).toHaveBeenCalledOnce();
});
it('should return value when key exists in storage', () => {
localStorage.setItem('foo', 'bar');
expect(storage.getItem('foo')).toEqual('bar');
expect(getItemSpy).toHaveBeenCalledWith('foo');
});
it('should persist item via setItem()', () => {
storage.setItem('foo', 'bar');
expect(setItemSpy).toHaveBeenCalledWith('foo', 'bar');
expect(localStorage.getItem('foo')).toEqual('bar');
});
it('should remove item via removeItem()', () => {
localStorage.setItem('foo', 'bar');
storage.removeItem('foo');
expect(removeItemSpy).toHaveBeenCalledWith('foo');
expect(localStorage.getItem('foo')).toBeNull();
});
it('should not affect other keys when removing', () => {
localStorage.setItem('foo', 'bar');
storage.removeItem('baz');
expect(removeItemSpy).toHaveBeenCalledWith('baz');
expect(localStorage.getItem('foo')).toEqual('bar');
});
it('should return null when getItem throws', () => {
getItemSpy.mockImplementation(() => { throw new Error('SecurityError'); });
expect(storage.getItem('foo')).toBeNull();
});
it('should not throw when setItem throws', () => {
setItemSpy.mockImplementation(() => { throw new Error('QuotaExceededError'); });
expect(() => storage.setItem('foo', 'bar')).not.toThrow();
});
it('should not throw when removeItem throws', () => {
removeItemSpy.mockImplementation(() => { throw new Error('SecurityError'); });
expect(() => storage.removeItem('foo')).not.toThrow();
});
});