-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d92fe9
commit f4f2b1a
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
projects/ngneat/cashew/src/lib/specs/cache-context.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |