|
| 1 | +import assert from 'node:assert'; |
| 2 | +import SingleEntryCache from './single-entry-cache'; |
| 3 | + |
| 4 | +describe('SingleEntryCache', () => { |
| 5 | + let cache: SingleEntryCache; |
| 6 | + beforeEach(() => { |
| 7 | + cache = new SingleEntryCache(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should return undefined when getting from empty cache', () => { |
| 11 | + assert.strictEqual(cache.get({ key: 'value' }), undefined); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return the cached instance when getting with the same key object', () => { |
| 15 | + const keyObj = { key: 'value' }; |
| 16 | + const instance = { data: 'test data' }; |
| 17 | + |
| 18 | + cache.set(keyObj, instance); |
| 19 | + assert.strictEqual(cache.get(keyObj), instance); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return undefined when getting with a different key object', () => { |
| 23 | + const keyObj1 = { key: 'value1' }; |
| 24 | + const keyObj2 = { key: 'value2' }; |
| 25 | + const instance = { data: 'test data' }; |
| 26 | + |
| 27 | + cache.set(keyObj1, instance); |
| 28 | + assert.strictEqual(cache.get(keyObj2), undefined); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should update the cached instance when setting with the same key object', () => { |
| 32 | + const keyObj = { key: 'value' }; |
| 33 | + const instance1 = { data: 'test data 1' }; |
| 34 | + const instance2 = { data: 'test data 2' }; |
| 35 | + |
| 36 | + cache.set(keyObj, instance1); |
| 37 | + assert.strictEqual(cache.get(keyObj), instance1); |
| 38 | + |
| 39 | + cache.set(keyObj, instance2); |
| 40 | + assert.strictEqual(cache.get(keyObj), instance2); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle undefined key object', () => { |
| 44 | + const instance = { data: 'test data' }; |
| 45 | + |
| 46 | + cache.set(undefined, instance); |
| 47 | + assert.strictEqual(cache.get(undefined), instance); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle complex objects as keys', () => { |
| 51 | + const keyObj = { |
| 52 | + id: 123, |
| 53 | + nested: { |
| 54 | + prop: 'value', |
| 55 | + array: [1, 2, 3] |
| 56 | + } |
| 57 | + }; |
| 58 | + const instance = { data: 'complex test data' }; |
| 59 | + |
| 60 | + cache.set(keyObj, instance); |
| 61 | + assert.strictEqual(cache.get(keyObj), instance); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should consider objects with same properties but different order as different keys', () => { |
| 65 | + const keyObj1 = { a: 1, b: 2 }; |
| 66 | + const keyObj2 = { b: 2, a: 1 }; // Same properties but different order |
| 67 | + const instance = { data: 'test data' }; |
| 68 | + |
| 69 | + cache.set(keyObj1, instance); |
| 70 | + |
| 71 | + assert.strictEqual(cache.get(keyObj2), undefined); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle circular structures', () => { |
| 75 | + const keyObj: any = {}; |
| 76 | + keyObj.self = keyObj; |
| 77 | + |
| 78 | + const instance = { data: 'test data' }; |
| 79 | + |
| 80 | + cache.set(keyObj, instance); |
| 81 | + |
| 82 | + assert.strictEqual(cache.get(keyObj), instance); |
| 83 | + }); |
| 84 | + |
| 85 | +}); |
0 commit comments