|
| 1 | +import { CachePolicy, CacheScope } from 'apollo-server-types'; |
| 2 | +import { newCachePolicy } from '../cachePolicy'; |
| 3 | + |
| 4 | +describe('newCachePolicy', () => { |
| 5 | + let cachePolicy: CachePolicy; |
| 6 | + beforeEach(() => { |
| 7 | + cachePolicy = newCachePolicy(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('starts uncacheable', () => { |
| 11 | + expect(cachePolicy.maxAge).toBeUndefined(); |
| 12 | + expect(cachePolicy.scope).toBeUndefined(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('restricting maxAge positive makes restricted', () => { |
| 16 | + cachePolicy.restrict({ maxAge: 10 }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('restricting maxAge 0 makes restricted', () => { |
| 20 | + cachePolicy.restrict({ maxAge: 0 }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('restricting scope to private makes restricted', () => { |
| 24 | + cachePolicy.restrict({ scope: CacheScope.Private }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns lowest max age value', () => { |
| 28 | + cachePolicy.restrict({ maxAge: 10 }); |
| 29 | + cachePolicy.restrict({ maxAge: 20 }); |
| 30 | + |
| 31 | + expect(cachePolicy.maxAge).toBe(10); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns lowest max age value in other order', () => { |
| 35 | + cachePolicy.restrict({ maxAge: 20 }); |
| 36 | + cachePolicy.restrict({ maxAge: 10 }); |
| 37 | + |
| 38 | + expect(cachePolicy.maxAge).toBe(10); |
| 39 | + }); |
| 40 | + |
| 41 | + it('maxAge 0 if any cache hint has a maxAge of 0', () => { |
| 42 | + cachePolicy.restrict({ maxAge: 120 }); |
| 43 | + cachePolicy.restrict({ maxAge: 0 }); |
| 44 | + cachePolicy.restrict({ maxAge: 20 }); |
| 45 | + |
| 46 | + expect(cachePolicy.maxAge).toBe(0); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns undefined if first cache hint has a maxAge of 0', () => { |
| 50 | + cachePolicy.restrict({ maxAge: 0 }); |
| 51 | + cachePolicy.restrict({ maxAge: 20 }); |
| 52 | + |
| 53 | + expect(cachePolicy.maxAge).toBe(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('only restricting maxAge keeps scope undefined', () => { |
| 57 | + cachePolicy.restrict({ maxAge: 10 }); |
| 58 | + |
| 59 | + expect(cachePolicy.scope).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns PRIVATE scope if any cache hint has PRIVATE scope', () => { |
| 63 | + cachePolicy.restrict({ |
| 64 | + maxAge: 10, |
| 65 | + scope: CacheScope.Public, |
| 66 | + }); |
| 67 | + cachePolicy.restrict({ |
| 68 | + maxAge: 10, |
| 69 | + scope: CacheScope.Private, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(cachePolicy).toHaveProperty('scope', CacheScope.Private); |
| 73 | + }); |
| 74 | + |
| 75 | + it('policyIfCacheable', () => { |
| 76 | + expect(cachePolicy.policyIfCacheable()).toBeNull(); |
| 77 | + |
| 78 | + cachePolicy.restrict({ scope: CacheScope.Private }); |
| 79 | + expect(cachePolicy.scope).toBe(CacheScope.Private); |
| 80 | + expect(cachePolicy.policyIfCacheable()).toBeNull(); |
| 81 | + |
| 82 | + cachePolicy.restrict({ maxAge: 10 }); |
| 83 | + expect(cachePolicy).toMatchObject({ |
| 84 | + maxAge: 10, |
| 85 | + scope: CacheScope.Private, |
| 86 | + }); |
| 87 | + expect(cachePolicy.policyIfCacheable()).toStrictEqual({ |
| 88 | + maxAge: 10, |
| 89 | + scope: CacheScope.Private, |
| 90 | + }); |
| 91 | + |
| 92 | + cachePolicy.restrict({ maxAge: 0 }); |
| 93 | + expect(cachePolicy).toMatchObject({ |
| 94 | + maxAge: 0, |
| 95 | + scope: CacheScope.Private, |
| 96 | + }); |
| 97 | + expect(cachePolicy.policyIfCacheable()).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('replace', () => { |
| 101 | + cachePolicy.restrict({ maxAge: 10, scope: CacheScope.Private }); |
| 102 | + cachePolicy.replace({ maxAge: 20, scope: CacheScope.Public }); |
| 103 | + |
| 104 | + expect(cachePolicy).toMatchObject({ |
| 105 | + maxAge: 20, |
| 106 | + scope: CacheScope.Public, |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments