-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cache policy calculation when a hint has maxAge 0 (#2197)
- Loading branch information
Showing
2 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.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,68 @@ | ||
import { ResponsePath } from 'graphql'; | ||
import { CacheControlExtension, CacheScope } from '../'; | ||
|
||
describe('CacheControlExtension', () => { | ||
let cacheControlExtension: CacheControlExtension; | ||
|
||
beforeEach(() => { | ||
cacheControlExtension = new CacheControlExtension(); | ||
}); | ||
|
||
describe('computeOverallCachePolicy', () => { | ||
const responsePath: ResponsePath = { | ||
key: 'test', | ||
prev: undefined, | ||
}; | ||
const responseSubPath: ResponsePath = { | ||
key: 'subTest', | ||
prev: responsePath, | ||
}; | ||
const responseSubSubPath: ResponsePath = { | ||
key: 'subSubTest', | ||
prev: responseSubPath, | ||
}; | ||
|
||
it('returns undefined without cache hints', () => { | ||
const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); | ||
expect(cachePolicy).toBeUndefined(); | ||
}); | ||
|
||
it('returns lowest max age value', () => { | ||
cacheControlExtension.addHint(responsePath, { maxAge: 10 }); | ||
cacheControlExtension.addHint(responseSubPath, { maxAge: 20 }); | ||
|
||
const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); | ||
expect(cachePolicy).toHaveProperty('maxAge', 10); | ||
}); | ||
|
||
it('returns undefined if any cache hint has a maxAge of 0', () => { | ||
cacheControlExtension.addHint(responsePath, { maxAge: 120 }); | ||
cacheControlExtension.addHint(responseSubPath, { maxAge: 0 }); | ||
cacheControlExtension.addHint(responseSubSubPath, { maxAge: 20 }); | ||
|
||
const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); | ||
expect(cachePolicy).toBeUndefined(); | ||
}); | ||
|
||
it('returns PUBLIC scope by default', () => { | ||
cacheControlExtension.addHint(responsePath, { maxAge: 10 }); | ||
|
||
const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); | ||
expect(cachePolicy).toHaveProperty('scope', CacheScope.Public); | ||
}); | ||
|
||
it('returns PRIVATE scope if any cache hint has PRIVATE scope', () => { | ||
cacheControlExtension.addHint(responsePath, { | ||
maxAge: 10, | ||
scope: CacheScope.Public, | ||
}); | ||
cacheControlExtension.addHint(responseSubPath, { | ||
maxAge: 10, | ||
scope: CacheScope.Private, | ||
}); | ||
|
||
const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); | ||
expect(cachePolicy).toHaveProperty('scope', CacheScope.Private); | ||
}); | ||
}); | ||
}); |
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