Repository navigation
Expand file tree
/
Copy pathasync-storage.spec.ts
More file actions
111 lines (97 loc) · 3.34 KB
/
Copy pathasync-storage.spec.ts
File metadata and controls
111 lines (97 loc) · 3.34 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
111
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { vi, describe, it, expect } from 'vitest';
import { ensureMultiGet } from './async-storage';
describe('ensureMultiGet', () => {
it('should preserve multiGet when already present (v1/v2)', () => {
const multiGet = vi.fn();
const storage = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
getAllKeys: vi.fn(),
clear: vi.fn(),
multiGet,
};
const result = ensureMultiGet(storage);
expect(result.multiGet).toBe(multiGet);
});
it('should construct multiGet from getMany when multiGet is absent (v3)', async () => {
const items: Record<string, string> = { a: '1', b: '2' };
const storage = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
getAllKeys: vi.fn(),
clear: vi.fn(),
getMany: vi.fn((keys: readonly string[]) => {
const record: Record<string, string | null> = {};
for (const key of keys) { record[key] = items[key] ?? null; }
return Promise.resolve(record);
}),
};
const result = ensureMultiGet(storage);
expect(result.multiGet).toBeDefined();
const pairs = await result.multiGet(['b', 'missing', 'a']);
expect(pairs).toEqual([
['b', '2'],
['missing', null],
['a', '1'],
]);
});
it('should not modify the original storage when constructing multiGet (v3)', () => {
const storage = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
getAllKeys: vi.fn(),
clear: vi.fn(),
getMany: vi.fn(),
};
const result = ensureMultiGet(storage);
expect(result).not.toBe(storage);
expect('multiGet' in storage).toBe(false);
expect(Object.getPrototypeOf(result)).toBe(storage);
expect(result.getItem).toBe(storage.getItem);
expect(result.setItem).toBe(storage.setItem);
expect(result.removeItem).toBe(storage.removeItem);
expect(result.getAllKeys).toBe(storage.getAllKeys);
expect(result.clear).toBe(storage.clear);
});
it('should return pairs in the requested key order regardless of getMany record order', async () => {
const items: Record<string, string> = { x: 'vx', y: 'vy', z: 'vz' };
const storage = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
getAllKeys: vi.fn(),
clear: vi.fn(),
getMany: vi.fn((keys: readonly string[]) => {
const record: Record<string, string | null> = {};
const reversed = [...keys].reverse();
for (const key of reversed) { record[key] = items[key] ?? null; }
return Promise.resolve(record);
}),
};
const result = ensureMultiGet(storage);
const pairs = await result.multiGet(['z', 'x', 'y']);
expect(pairs).toEqual([
['z', 'vz'],
['x', 'vx'],
['y', 'vy'],
]);
});
});