Debian Codegraph @

tests/unit/cache.test.ts

raw
1 import { beforeEach, describe, expect, it, vi } from 'vitest';
2 import Swup from '../../src/Swup.js';
3 import { Cache, CacheData } from '../../src/modules/Cache.js';
4 import { Visit } from '../../src/modules/Visit.js';
5
6 interface CacheTtlData {
7 ttl: number;
8 created: number;
9 }
10
11 interface CacheIndexData {
12 index: number;
13 }
14
15 interface AugmentedCacheData extends CacheData, CacheTtlData, CacheIndexData {}
16
17 const swup = new Swup();
18 const visit = swup.visit;
19 const cache = new Cache(swup);
20
21 const page0 = { url: 'https://localhost/page-1', html: '1' };
22 const page1 = { url: '/page-1', html: '1' };
23 const page2 = { url: '/page-2', html: '2' };
24 const page3 = { url: '/page-3', html: '3' };
25
26 describe('Cache', () => {
27 beforeEach(() => {
28 cache.clear();
29 });
30
31 it('should be empty', () => {
32 expect(cache.size).toBe(0);
33 });
34
35 it('should append pages', () => {
36 cache.set(page1.url, page1);
37 expect(cache.size).toBe(1);
38 });
39
40 it('should have pages', () => {
41 cache.set(page1.url, page1);
42 expect(cache.has(page1.url)).toBe(true);
43 });
44
45 it('should get pages', () => {
46 cache.set(page1.url, page1);
47 expect(cache.get(page1.url)).toEqual(page1);
48 });
49
50 it('should delete pages', () => {
51 cache.set(page1.url, page1);
52 expect(cache.has(page1.url)).toBe(true);
53 cache.delete(page1.url);
54 expect(cache.has(page1.url)).toBe(false);
55 });
56
57 it('should clear', () => {
58 cache.set(page1.url, page1);
59 expect(cache.size).toBe(1);
60 cache.clear();
61 expect(cache.size).toBe(0);
62 });
63
64 it('should overwrite identical pages', () => {
65 cache.set(page1.url, page1);
66 expect(cache.size).toBe(1);
67 cache.set(page1.url, page1);
68 expect(cache.size).toBe(1);
69 });
70
71 it('should not overwrite different pages', () => {
72 cache.set(page1.url, page1);
73 expect(cache.size).toBe(1);
74 cache.set(page2.url, page2);
75 expect(cache.size).toBe(2);
76 });
77
78 it('should remove host', () => {
79 cache.set(page1.url, page1);
80 expect(cache.size).toBe(1);
81 cache.set(page0.url, page0);
82 expect(cache.size).toBe(1);
83 });
84
85 it('should trigger a hook on set', () => {
86 const handler = vi.fn();
87
88 swup.hooks.on('cache:set', handler);
89
90 cache.set(page1.url, page1);
91
92 expect(handler).toBeCalledTimes(1);
93 expect(handler).toBeCalledWith(visit, { page: page1 }, undefined);
94 });
95
96 it('should allow augmenting cache entries on save', () => {
97 const now = Date.now();
98
99 swup.hooks.on('cache:set', (_, { page }) => {
100 const ttl: CacheTtlData = { ttl: 1000, created: now };
101 cache.update(page.url, ttl);
102 });
103
104 cache.set('/page', { url: '/page', html: '' });
105
106 const page = cache.get('/page');
107
108 expect(page).toEqual({ url: '/page', html: '', ttl: 1000, created: now });
109 });
110
111 it('should allow manual pruning', () => {
112 swup.hooks.on('cache:set', (_, { page }) => {
113 cache.update(page.url, { index: cache.size });
114 });
115
116 cache.set(page1.url, page1);
117 cache.set(page2.url, page2);
118 cache.set(page3.url, page3);
119
120 cache.prune((url, page) => (page as AugmentedCacheData).index > 2);
121
122 expect(cache.size).toBe(2);
123 expect(cache.has(page1.url)).toBe(true);
124 expect(cache.has(page2.url)).toBe(true);
125 expect(cache.has(page3.url)).toBe(false);
126 });
127
128 it('should return a copy from cache.get()', () => {
129 cache.set(page1.url, page1);
130 const page = cache.get(page1.url);
131 page!.html = 'new';
132 expect(cache.get(page1.url)?.html).toEqual(page1.html);
133 });
134
135 it('should return a new Map with shallow copies from cache.all', () => {
136 cache.set(page1.url, page1);
137 cache.set(page2.url, page2);
138
139 const all = cache.all;
140 all.get(page1.url)!.html = 'new';
141
142 expect(cache.get(page1.url)?.html).toEqual(page1.html);
143 });
144 });
145
146 describe('Types', () => {
147 it('error when necessary', async () => {
148 const swup = new Swup();
149 const cache = new Cache(swup);
150
151 // @ts-expect-no-error
152 swup.hooks.on('history:popstate', (visit: Visit, { event }: { event: PopStateEvent }) => {});
153 // @ts-expect-no-error
154 await swup.hooks.call('history:popstate', swup.visit, { event: new PopStateEvent('') });
155
156 try {
157 // @ts-expect-error
158 cache.set();
159 // @ts-expect-error
160 cache.set(url);
161 // @ts-expect-error
162 cache.set(url, {});
163 // @ts-expect-error
164 cache.set({ url: '/test' });
165 } catch (error) {}
166 });
167 });