Skip to content

Commit 0eeed42

Browse files
added tests for TTLCache behaviour for date instance values-1
1 parent 09ffa49 commit 0eeed42

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

lib/cache.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,51 @@ describe('TTLCache', () => {
443443
cache.destroy();
444444
});
445445

446+
it('preserves Date instance with current timestamp (new Date())', () => {
447+
const cache = new TTLCache<Date>();
448+
449+
const now = new Date();
450+
451+
cache.set('current-date', now, 60_000);
452+
453+
const cached = cache.get('current-date');
454+
455+
expect(cached).toBeInstanceOf(Date);
456+
expect(cached?.getTime()).toBe(now.getTime());
457+
expect(cached?.toISOString()).toBe(now.toISOString());
458+
459+
cache.destroy();
460+
});
461+
462+
it('preserves Date instance nested in object with mixed types', () => {
463+
const cache = new TTLCache<{
464+
id: number;
465+
name: string;
466+
created: Date;
467+
isActive: boolean;
468+
}>();
469+
470+
const created = new Date('2024-03-15T10:30:45.123Z');
471+
const data = {
472+
id: 42,
473+
name: 'Test Event',
474+
created: created,
475+
isActive: true,
476+
};
477+
478+
cache.set('event', data, 60_000);
479+
480+
const cached = cache.get('event');
481+
482+
expect(cached?.id).toBe(42);
483+
expect(cached?.name).toBe('Test Event');
484+
expect(cached?.isActive).toBe(true);
485+
expect(cached?.created).toBeInstanceOf(Date);
486+
expect(cached?.created.toISOString()).toBe(created.toISOString());
487+
488+
cache.destroy();
489+
});
490+
446491
it('stores and retrieves nested object values', () => {
447492
const cache = new TTLCache<{
448493
user: { id: number; name: string };

0 commit comments

Comments
 (0)