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