Skip to content

Commit 63d1d46

Browse files
authored
Merge pull request #32 from windingtree/test/memory-storage
Test/memory storage
2 parents 9ad483c + fcaf781 commit 63d1d46

File tree

4 files changed

+119
-8
lines changed

4 files changed

+119
-8
lines changed

src/storage/abstract.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export interface GenericStorageOptions {
2222
/**
2323
* Storage initializer type
2424
*/
25-
export type StorageInitializer = () => Promise<Storage>;
25+
export type StorageInitializer<T extends Storage = Storage> = () => Promise<T>;
2626

2727
/**
2828
* Storage initializer callback function type
2929
*/
30-
export type StorageInitializerFunction = (
30+
export type StorageInitializerFunction<T extends Storage = Storage> = (
3131
options?: GenericStorageOptions,
32-
) => StorageInitializer;
32+
) => StorageInitializer<T>;

src/storage/local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class LocalStorage extends Storage {
222222
/**
223223
* Local storage configuration
224224
*/
225-
export const createInitializer: StorageInitializerFunction =
225+
export const createInitializer: StorageInitializerFunction<LocalStorage> =
226226
(options?: LocalStorageOptions) =>
227227
// eslint-disable-next-line @typescript-eslint/require-await
228228
async (): Promise<LocalStorage> => {

src/storage/memory.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ export class MemoryStorage extends Storage {
3838

3939
// Validate MemoryStorageOptions
4040

41-
this.db = new Map<string, unknown>(options?.entries);
42-
4341
if (options.scope) {
4442
this.scopeIdsKey = `memory_storage_scope_${options.scope}_ids`;
4543
}
4644

45+
this.db = new Map<string, unknown>(options?.entries);
46+
47+
if (options.entries) {
48+
options.entries.forEach((e) => {
49+
this.addScopeId(e[0]);
50+
});
51+
}
52+
4753
logger.trace('Memory storage initialized');
4854
}
4955

@@ -86,7 +92,7 @@ export class MemoryStorage extends Storage {
8692
* @returns
8793
* @memberof MemoryStorage
8894
*/
89-
private addScopeId(id: string) {
95+
protected addScopeId(id: string) {
9096
try {
9197
if (!this.scopeIdsKey) {
9298
return;
@@ -220,7 +226,7 @@ export class MemoryStorage extends Storage {
220226
}
221227

222228
// Storage configuration
223-
export const createInitializer: StorageInitializerFunction =
229+
export const createInitializer: StorageInitializerFunction<MemoryStorage> =
224230
(options?: MemoryStorageOptions) =>
225231
// eslint-disable-next-line @typescript-eslint/require-await
226232
async (): Promise<MemoryStorage> => {

test/storage.memory.spec.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, expect, it, beforeEach } from './setup.js';
2+
import {
3+
MemoryStorage,
4+
MemoryStorageOptions,
5+
createInitializer,
6+
} from '../src/storage/memory.js';
7+
8+
describe('MemoryStorage', () => {
9+
let memoryStorage: MemoryStorage;
10+
11+
beforeEach(() => {
12+
const options: MemoryStorageOptions = {
13+
scope: 'test',
14+
entries: [['test', 'value']],
15+
};
16+
memoryStorage = new MemoryStorage(options);
17+
});
18+
19+
describe('#constructor', () => {
20+
it('MemoryStorage is initialized correctly', () => {
21+
expect(memoryStorage).toBeDefined();
22+
expect(memoryStorage).toBeInstanceOf(MemoryStorage);
23+
});
24+
25+
it('Throws an error when a non-string key is used in the initial entries', () => {
26+
const badOptions: MemoryStorageOptions = {
27+
entries: {} as Array<[string, unknown]>,
28+
};
29+
expect(() => new MemoryStorage(badOptions)).toThrow(TypeError);
30+
});
31+
});
32+
33+
describe('#set', () => {
34+
it('Value is set correctly', async () => {
35+
await memoryStorage.set('testKey', 'testValue');
36+
const result = await memoryStorage.get('testKey');
37+
expect(result).toEqual('testValue');
38+
});
39+
});
40+
41+
describe('#get', () => {
42+
it('Value is retrieved correctly', async () => {
43+
const result = await memoryStorage.get('test');
44+
expect(result).toEqual('value');
45+
});
46+
47+
it('Returns undefined when a non-existing key is used', async () => {
48+
const result = await memoryStorage.get('nonExistingKey');
49+
expect(result).toBeUndefined();
50+
});
51+
52+
it('Throws an error when a non-string key is used', async () => {
53+
const result = await memoryStorage.get(undefined as unknown as string);
54+
expect(result).toBeUndefined();
55+
});
56+
});
57+
58+
describe('#delete', () => {
59+
it('Value is deleted correctly', async () => {
60+
await memoryStorage.delete('test');
61+
const result = await memoryStorage.get('test');
62+
expect(result).toBeUndefined();
63+
});
64+
65+
it('Returns false when a non-existing key is used', async () => {
66+
const result = await memoryStorage.delete('nonExistingKey');
67+
expect(result).toBeFalsy();
68+
});
69+
});
70+
71+
describe('#entries', () => {
72+
it('Entries are retrieved correctly', () => {
73+
const entries = Array.from(memoryStorage.entries<string>());
74+
expect(entries).toContainEqual(['test', 'value']);
75+
});
76+
77+
it('Returns an empty iterator when storage is empty', async () => {
78+
await memoryStorage.reset();
79+
const entries = Array.from(memoryStorage.entries<string>());
80+
expect(entries.length).toEqual(0);
81+
});
82+
});
83+
84+
describe('#reset', () => {
85+
it('Storage is reset correctly', async () => {
86+
await memoryStorage.reset<string>();
87+
const result = await memoryStorage.get('test');
88+
expect(result).toBeUndefined();
89+
const entries = Array.from(memoryStorage.entries<string>());
90+
expect(entries.length).toEqual(0);
91+
});
92+
});
93+
94+
describe('#createInitializer', () => {
95+
it('Initializer creates instance correctly', async () => {
96+
const options: MemoryStorageOptions = { scope: 'testInitializer' };
97+
const initializer = createInitializer(options);
98+
const initializedStorage = await initializer();
99+
expect(initializedStorage).toBeInstanceOf(MemoryStorage);
100+
expect(initializedStorage.scopeIdsKey).toEqual(
101+
'memory_storage_scope_testInitializer_ids',
102+
);
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)