1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-01 16:46:54 +03:00
Files
node-redis/packages/client/lib/single-entry-cache.spec.ts
Nikolay Karadzhov 6f961bd715 fix(client): cache subsequent clients (#2963)
* fix(client): cache subsequent clients

we dont need to recreate a client if
its config hasnt changed

fixes #2954

* handle circular structures

* make cache generic
2025-05-14 17:23:22 +03:00

86 lines
2.4 KiB
TypeScript

import assert from 'node:assert';
import SingleEntryCache from './single-entry-cache';
describe('SingleEntryCache', () => {
let cache: SingleEntryCache;
beforeEach(() => {
cache = new SingleEntryCache();
});
it('should return undefined when getting from empty cache', () => {
assert.strictEqual(cache.get({ key: 'value' }), undefined);
});
it('should return the cached instance when getting with the same key object', () => {
const keyObj = { key: 'value' };
const instance = { data: 'test data' };
cache.set(keyObj, instance);
assert.strictEqual(cache.get(keyObj), instance);
});
it('should return undefined when getting with a different key object', () => {
const keyObj1 = { key: 'value1' };
const keyObj2 = { key: 'value2' };
const instance = { data: 'test data' };
cache.set(keyObj1, instance);
assert.strictEqual(cache.get(keyObj2), undefined);
});
it('should update the cached instance when setting with the same key object', () => {
const keyObj = { key: 'value' };
const instance1 = { data: 'test data 1' };
const instance2 = { data: 'test data 2' };
cache.set(keyObj, instance1);
assert.strictEqual(cache.get(keyObj), instance1);
cache.set(keyObj, instance2);
assert.strictEqual(cache.get(keyObj), instance2);
});
it('should handle undefined key object', () => {
const instance = { data: 'test data' };
cache.set(undefined, instance);
assert.strictEqual(cache.get(undefined), instance);
});
it('should handle complex objects as keys', () => {
const keyObj = {
id: 123,
nested: {
prop: 'value',
array: [1, 2, 3]
}
};
const instance = { data: 'complex test data' };
cache.set(keyObj, instance);
assert.strictEqual(cache.get(keyObj), instance);
});
it('should consider objects with same properties but different order as different keys', () => {
const keyObj1 = { a: 1, b: 2 };
const keyObj2 = { b: 2, a: 1 }; // Same properties but different order
const instance = { data: 'test data' };
cache.set(keyObj1, instance);
assert.strictEqual(cache.get(keyObj2), undefined);
});
it('should handle circular structures', () => {
const keyObj: any = {};
keyObj.self = keyObj;
const instance = { data: 'test data' };
cache.set(keyObj, instance);
assert.strictEqual(cache.get(keyObj), instance);
});
});