|
| 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