diff --git a/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts new file mode 100644 index 00000000000..127071f55e9 --- /dev/null +++ b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts @@ -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); + }); + }); +}); diff --git a/packages/apollo-cache-control/src/index.ts b/packages/apollo-cache-control/src/index.ts index db145f7a15f..80d8cd23913 100644 --- a/packages/apollo-cache-control/src/index.ts +++ b/packages/apollo-cache-control/src/index.ts @@ -156,10 +156,11 @@ export class CacheControlExtension let scope: CacheScope = CacheScope.Public; for (const hint of this.hints.values()) { - if (hint.maxAge) { - lowestMaxAge = lowestMaxAge - ? Math.min(lowestMaxAge, hint.maxAge) - : hint.maxAge; + if (hint.maxAge !== undefined) { + lowestMaxAge = + lowestMaxAge !== undefined + ? Math.min(lowestMaxAge, hint.maxAge) + : hint.maxAge; } if (hint.scope === CacheScope.Private) { scope = CacheScope.Private;