Skip to content

Commit

Permalink
feat: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienDehopre committed Jul 7, 2021
1 parent 0d92fe9 commit f4f2b1a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions projects/ngneat/cashew/src/lib/specs/cache-context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HttpContext, HttpContextToken } from '@angular/common/http';
import { CACHE_CONTEXT, withCache } from '../cache-context';

describe('withCache', () => {
const token = new HttpContextToken<number>(() => 0);

it('should reuse existing HttpContext when provided', () => {
const existingContext = new HttpContext().set(token, 42);

const result = withCache({ cache: true, ttl: 60000 }, existingContext);
expect(result === existingContext).toBeTruthy();
const allTokens = Array.from(result.keys());
expect(allTokens.length).toEqual(2);
expect(result.get(CACHE_CONTEXT)).toEqual({ cache: true, ttl: 60000 });
expect(result.get(token)).toEqual(42);
});

it('should create a new HttpContext when no existing context exists', () => {
const result = withCache({ cache: true, ttl: 60000 });
expect(result).toBeDefined();
const allTokens = Array.from(result.keys());
expect(allTokens.length).toEqual(1);
expect(result.get(CACHE_CONTEXT)).toEqual({ cache: true, ttl: 60000 });
expect(result.get(token)).toEqual(0);
});
});

0 comments on commit f4f2b1a

Please sign in to comment.