-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathstorage.test.ts
More file actions
110 lines (88 loc) · 3.43 KB
/
storage.test.ts
File metadata and controls
110 lines (88 loc) · 3.43 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { afterEach, describe, expect, test, vi } from 'vitest';
import browser from 'webextension-polyfill';
import { BrowserStorageCache, MemoryStorageCache } from '../storage';
describe('StorageCache', () => {
const KEY = 'foo';
const VALUE = 'bar';
const _void = void 0;
describe('createKey', () => {
test('returns a string key', () => {
expect(BrowserStorageCache.createKey('a', 'b', 'c')).toBe('a|b|c');
});
test('omits falsy values', () => {
// @ts-expect-error - Testing; Intentionally passing undefined value
expect(BrowserStorageCache.createKey('a', undefined, false, null, 'c')).toBe('a|c');
});
});
describe('set', () => {
test('setting the storage cache', async () => {
const setMock = vi.mocked(browser.storage.local.set).mockResolvedValueOnce(_void);
expect(await BrowserStorageCache.set(KEY, VALUE)).toBe(_void);
expect(setMock).toHaveBeenCalledTimes(1);
expect(setMock).toHaveBeenCalledWith({ [KEY]: VALUE });
});
});
describe('remove', () => {
test('removing from the storage cache', async () => {
const removeMock = vi.mocked(browser.storage.local.remove).mockResolvedValueOnce(_void);
expect(await BrowserStorageCache.remove(KEY)).toBe(_void);
expect(removeMock).toHaveBeenCalledTimes(1);
expect(removeMock).toHaveBeenCalledWith(KEY);
});
});
describe('get', () => {
afterEach(() => {
vi.restoreAllMocks();
vi.mocked(browser.storage.local.get).mockClear();
});
test('value missing', async () => {
const getMock = vi.mocked(browser.storage.local.get).mockResolvedValue({});
expect(await BrowserStorageCache.get(KEY)).toBeUndefined();
expect(getMock).toHaveBeenCalledTimes(1);
expect(getMock).toHaveBeenCalledWith(KEY);
});
test('value exists', async () => {
const getMock = vi.mocked(browser.storage.local.get).mockResolvedValue({ [KEY]: VALUE });
expect(await BrowserStorageCache.get(KEY)).toBe(VALUE);
expect(getMock).toHaveBeenCalledTimes(1);
expect(getMock).toHaveBeenCalledWith(KEY);
});
});
});
describe('MemoryStorageCache', () => {
const KEY = 'foo';
const VALUE = 'bar';
const _void = void 0;
describe('createKey', () => {
test('returns a string key', () => {
expect(MemoryStorageCache.createKey('a', 'b', 'c')).toBe('a|b|c');
});
test('omits falsy values', () => {
// @ts-expect-error - Testing; Intentionally passing undefined value
expect(MemoryStorageCache.createKey('a', undefined, false, null, 'c')).toBe('a|c');
});
});
describe('set', () => {
test('setting the storage cache', async () => {
await MemoryStorageCache.set(KEY, VALUE);
expect(await MemoryStorageCache.get(KEY)).toBe(VALUE);
});
});
describe('remove', () => {
test('removing from the storage cache', async () => {
expect(await MemoryStorageCache.set(KEY, VALUE)).toBe(_void);
expect(await MemoryStorageCache.get(KEY)).toBe(VALUE);
expect(await MemoryStorageCache.remove(KEY)).toBe(_void);
expect(await MemoryStorageCache.get(KEY)).toBeUndefined();
});
});
describe('get', () => {
test('value missing', async () => {
expect(await MemoryStorageCache.get(KEY)).toBeUndefined();
});
test('value exists', async () => {
expect(await MemoryStorageCache.set(KEY, VALUE)).toBe(_void);
expect(await MemoryStorageCache.get(KEY)).toBe(VALUE);
});
});
});