request-context-cache.service.spec.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { beforeEach, describe, expect, it } from 'vitest';
  2. import { RequestContext } from '../api';
  3. import { RequestContextCacheService } from './request-context-cache.service';
  4. describe('Request context cache', () => {
  5. let cache: RequestContextCacheService;
  6. beforeEach(() => {
  7. cache = new RequestContextCacheService();
  8. });
  9. it('stores and retrieves a multiple values', () => {
  10. const ctx = RequestContext.empty();
  11. cache.set(ctx, 'test', 1);
  12. cache.set(ctx, 'test2', 2);
  13. expect(cache.get(ctx, 'test')).toBe(1);
  14. expect(cache.get(ctx, 'test2')).toBe(2);
  15. });
  16. it('uses getDefault function', async () => {
  17. const ctx = RequestContext.empty();
  18. const result = cache.get(ctx, 'test', async () => 'foo');
  19. expect(result instanceof Promise).toBe(true);
  20. expect(await result).toBe('foo');
  21. });
  22. it('can use objects as keys', () => {
  23. const ctx = RequestContext.empty();
  24. const x = {};
  25. cache.set(ctx, x, 1);
  26. expect(cache.get(ctx, x)).toBe(1);
  27. });
  28. it('uses separate stores per context', () => {
  29. const ctx = RequestContext.empty();
  30. const ctx2 = RequestContext.empty();
  31. cache.set(ctx, 'test', 1);
  32. cache.set(ctx2, 'test', 2);
  33. expect(cache.get(ctx, 'test')).toBe(1);
  34. expect(cache.get(ctx2, 'test')).toBe(2);
  35. });
  36. });