swup 4.3.3 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Swup.cjs +1 -1
- package/dist/Swup.cjs.map +1 -1
- package/dist/Swup.modern.js +1 -1
- package/dist/Swup.modern.js.map +1 -1
- package/dist/Swup.module.js +1 -1
- package/dist/Swup.module.js.map +1 -1
- package/dist/Swup.umd.js +1 -1
- package/dist/Swup.umd.js.map +1 -1
- package/dist/types/Swup.d.ts +3 -1
- package/dist/types/modules/Hooks.d.ts +5 -0
- package/dist/types/modules/Visit.d.ts +2 -0
- package/dist/types/modules/fetchPage.d.ts +8 -2
- package/dist/types/modules/renderPage.d.ts +1 -2
- package/package.json +12 -17
- package/src/Swup.ts +4 -1
- package/src/modules/Hooks.ts +5 -0
- package/src/modules/Visit.ts +3 -0
- package/src/modules/fetchPage.ts +47 -8
- package/src/modules/navigate.ts +45 -32
- package/src/modules/renderPage.ts +1 -7
- package/dist/types/__test__/index.test.d.ts +0 -1
- package/dist/types/helpers/__test__/matchPath.test.d.ts +0 -1
- package/dist/types/modules/__test__/cache.test.d.ts +0 -1
- package/dist/types/modules/__test__/delegateEvent.test.d.ts +0 -1
- package/dist/types/modules/__test__/hooks.test.d.ts +0 -1
- package/dist/types/modules/__test__/plugins.test.d.ts +0 -1
- package/dist/types/modules/__test__/replaceContent.test.d.ts +0 -1
- package/src/__test__/index.test.ts +0 -83
- package/src/helpers/__test__/matchPath.test.ts +0 -54
- package/src/modules/__test__/cache.test.ts +0 -159
- package/src/modules/__test__/delegateEvent.test.ts +0 -36
- package/src/modules/__test__/hooks.test.ts +0 -319
- package/src/modules/__test__/plugins.test.ts +0 -89
- package/src/modules/__test__/replaceContent.test.ts +0 -91
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import Swup from '../../Swup.js';
|
|
3
|
-
import { Cache, CacheData } from '../Cache.js';
|
|
4
|
-
import { Visit } from '../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 page1 = { url: '/page-1', html: '1' };
|
|
22
|
-
const page2 = { url: '/page-2', html: '2' };
|
|
23
|
-
const page3 = { url: '/page-3', html: '3' };
|
|
24
|
-
|
|
25
|
-
describe('Cache', () => {
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
cache.clear();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should be empty', () => {
|
|
31
|
-
expect(cache.size).toBe(0);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should append pages', () => {
|
|
35
|
-
cache.set(page1.url, page1);
|
|
36
|
-
expect(cache.size).toBe(1);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should have pages', () => {
|
|
40
|
-
cache.set(page1.url, page1);
|
|
41
|
-
expect(cache.has(page1.url)).toBe(true);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('should get pages', () => {
|
|
45
|
-
cache.set(page1.url, page1);
|
|
46
|
-
expect(cache.get(page1.url)).toEqual(page1);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should delete pages', () => {
|
|
50
|
-
cache.set(page1.url, page1);
|
|
51
|
-
expect(cache.has(page1.url)).toBe(true);
|
|
52
|
-
cache.delete(page1.url);
|
|
53
|
-
expect(cache.has(page1.url)).toBe(false);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('should clear', () => {
|
|
57
|
-
cache.set(page1.url, page1);
|
|
58
|
-
expect(cache.size).toBe(1);
|
|
59
|
-
cache.clear();
|
|
60
|
-
expect(cache.size).toBe(0);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should overwrite identical pages', () => {
|
|
64
|
-
cache.set(page1.url, page1);
|
|
65
|
-
expect(cache.size).toBe(1);
|
|
66
|
-
cache.set(page1.url, page1);
|
|
67
|
-
expect(cache.size).toBe(1);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should not overwrite different pages', () => {
|
|
71
|
-
cache.set(page1.url, page1);
|
|
72
|
-
expect(cache.size).toBe(1);
|
|
73
|
-
cache.set(page2.url, page2);
|
|
74
|
-
expect(cache.size).toBe(2);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should trigger a hook on set', () => {
|
|
78
|
-
const handler = vi.fn();
|
|
79
|
-
|
|
80
|
-
swup.hooks.on('cache:set', handler);
|
|
81
|
-
|
|
82
|
-
cache.set(page1.url, page1);
|
|
83
|
-
|
|
84
|
-
expect(handler).toBeCalledTimes(1);
|
|
85
|
-
expect(handler).toBeCalledWith(visit, { page: page1 }, undefined);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should allow augmenting cache entries on save', () => {
|
|
89
|
-
const now = Date.now();
|
|
90
|
-
|
|
91
|
-
swup.hooks.on('cache:set', (_, { page }) => {
|
|
92
|
-
const ttl: CacheTtlData = { ttl: 1000, created: now };
|
|
93
|
-
cache.update(page.url, ttl);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
cache.set('/page', { url: '/page', html: '' });
|
|
97
|
-
|
|
98
|
-
const page = cache.get('/page');
|
|
99
|
-
|
|
100
|
-
expect(page).toEqual({ url: '/page', html: '', ttl: 1000, created: now });
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should allow manual pruning', () => {
|
|
104
|
-
swup.hooks.on('cache:set', (_, { page }) => {
|
|
105
|
-
cache.update(page.url, { index: cache.size });
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
cache.set(page1.url, page1);
|
|
109
|
-
cache.set(page2.url, page2);
|
|
110
|
-
cache.set(page3.url, page3);
|
|
111
|
-
|
|
112
|
-
cache.prune((url, page) => (page as AugmentedCacheData).index > 2);
|
|
113
|
-
|
|
114
|
-
expect(cache.size).toBe(2);
|
|
115
|
-
expect(cache.has(page1.url)).toBe(true);
|
|
116
|
-
expect(cache.has(page2.url)).toBe(true);
|
|
117
|
-
expect(cache.has(page3.url)).toBe(false);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should return a copy from cache.get()', () => {
|
|
121
|
-
cache.set(page1.url, page1);
|
|
122
|
-
const page = cache.get(page1.url);
|
|
123
|
-
page!.html = 'new';
|
|
124
|
-
expect(cache.get(page1.url)?.html).toEqual(page1.html);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('should return a new Map with shallow copies from cache.all', () => {
|
|
128
|
-
cache.set(page1.url, page1);
|
|
129
|
-
cache.set(page2.url, page2);
|
|
130
|
-
|
|
131
|
-
const all = cache.all;
|
|
132
|
-
all.get(page1.url)!.html = 'new';
|
|
133
|
-
|
|
134
|
-
expect(cache.get(page1.url)?.html).toEqual(page1.html);
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('Types', () => {
|
|
139
|
-
it('error when necessary', async () => {
|
|
140
|
-
const swup = new Swup();
|
|
141
|
-
const cache = new Cache(swup);
|
|
142
|
-
|
|
143
|
-
// @ts-expect-no-error
|
|
144
|
-
swup.hooks.on('history:popstate', (visit: Visit, { event: PopStateEvent }) => {});
|
|
145
|
-
// @ts-expect-no-error
|
|
146
|
-
await swup.hooks.call('history:popstate', { event: new PopStateEvent('') });
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
// @ts-expect-error
|
|
150
|
-
cache.set();
|
|
151
|
-
// @ts-expect-error
|
|
152
|
-
cache.set(url);
|
|
153
|
-
// @ts-expect-error
|
|
154
|
-
cache.set(url, {});
|
|
155
|
-
// @ts-expect-error
|
|
156
|
-
cache.set({ url: '/test' });
|
|
157
|
-
} catch (error) {}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { DelegateEvent } from 'delegate-it';
|
|
2
|
-
import { describe, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { delegateEvent } from '../../helpers/delegateEvent.js';
|
|
5
|
-
|
|
6
|
-
describe('delegateEvent', () => {
|
|
7
|
-
it('should return correct types', () => {
|
|
8
|
-
delegateEvent('form', 'submit', (event) => {});
|
|
9
|
-
|
|
10
|
-
// @ts-expect-no-error
|
|
11
|
-
delegateEvent('form', 'submit', (event: SubmitEvent) => {});
|
|
12
|
-
// @ts-expect-error
|
|
13
|
-
delegateEvent('form', 'submit', (event: MouseEvent) => {});
|
|
14
|
-
|
|
15
|
-
// @ts-expect-no-error
|
|
16
|
-
delegateEvent('form', 'submit', (event: DelegateEvent<SubmitEvent>) => {});
|
|
17
|
-
// @ts-expect-error
|
|
18
|
-
delegateEvent('form', 'submit', (event: DelegateEvent<MouseEvent>) => {});
|
|
19
|
-
|
|
20
|
-
// @ts-expect-no-error
|
|
21
|
-
delegateEvent('form', 'submit', (event: DelegateEvent<SubmitEvent, HTMLFormElement>) => {});
|
|
22
|
-
delegateEvent(
|
|
23
|
-
'form',
|
|
24
|
-
'submit',
|
|
25
|
-
// @ts-expect-error
|
|
26
|
-
(event: DelegateEvent<MouseEvent, HTMLAnchorElement>) => {}
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
delegateEvent('form', 'submit', (event) => {
|
|
30
|
-
// @ts-expect-no-error
|
|
31
|
-
const el: HTMLFormElement = event.delegateTarget;
|
|
32
|
-
});
|
|
33
|
-
// @ts-expect-error
|
|
34
|
-
delegateEvent('form', 'submit', (event: MouseEvent) => {});
|
|
35
|
-
});
|
|
36
|
-
});
|
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import Swup from '../../Swup.js';
|
|
3
|
-
import { DefaultHandler, Handler, Hooks } from '../Hooks.js';
|
|
4
|
-
import { Visit } from '../Visit.js';
|
|
5
|
-
|
|
6
|
-
describe('Hook registry', () => {
|
|
7
|
-
it('should add handlers', () => {
|
|
8
|
-
const swup = new Swup();
|
|
9
|
-
const handler = vi.fn();
|
|
10
|
-
|
|
11
|
-
// Make private fields public for this test
|
|
12
|
-
const HooksWithAccess = class extends Hooks {
|
|
13
|
-
getRegistry() {
|
|
14
|
-
return this.registry;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
const hooks = new HooksWithAccess(swup);
|
|
18
|
-
|
|
19
|
-
hooks.on('enable', handler);
|
|
20
|
-
const ledger = hooks.getRegistry().get('enable');
|
|
21
|
-
|
|
22
|
-
expect(ledger).toBeDefined();
|
|
23
|
-
expect(ledger).toBeInstanceOf(Map);
|
|
24
|
-
expect(ledger!.size).toBe(1);
|
|
25
|
-
|
|
26
|
-
const registrations = Array.from(ledger!.values());
|
|
27
|
-
const registration = registrations.find((reg) => reg.handler === handler);
|
|
28
|
-
|
|
29
|
-
expect(registration?.handler).toEqual(handler);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should remove handlers', async () => {
|
|
33
|
-
const swup = new Swup();
|
|
34
|
-
const handler1 = vi.fn();
|
|
35
|
-
const handler2 = vi.fn();
|
|
36
|
-
|
|
37
|
-
swup.hooks.on('enable', handler1);
|
|
38
|
-
swup.hooks.on('enable', handler2);
|
|
39
|
-
|
|
40
|
-
await swup.hooks.call('enable', undefined);
|
|
41
|
-
|
|
42
|
-
expect(handler1).toBeCalledTimes(1);
|
|
43
|
-
expect(handler2).toBeCalledTimes(1);
|
|
44
|
-
|
|
45
|
-
swup.hooks.off('enable', handler2);
|
|
46
|
-
|
|
47
|
-
await swup.hooks.call('enable', undefined);
|
|
48
|
-
|
|
49
|
-
expect(handler1).toBeCalledTimes(2);
|
|
50
|
-
expect(handler2).toBeCalledTimes(1);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should return a function to unregister the handler', async () => {
|
|
54
|
-
const swup = new Swup();
|
|
55
|
-
const handler1 = vi.fn();
|
|
56
|
-
const handler2 = vi.fn();
|
|
57
|
-
|
|
58
|
-
const unregister1 = swup.hooks.on('enable', handler1);
|
|
59
|
-
const unregister2 = swup.hooks.on('enable', handler2);
|
|
60
|
-
|
|
61
|
-
expect(unregister1).toBeTypeOf('function');
|
|
62
|
-
|
|
63
|
-
await swup.hooks.call('enable', undefined);
|
|
64
|
-
|
|
65
|
-
expect(handler1).toBeCalledTimes(1);
|
|
66
|
-
expect(handler2).toBeCalledTimes(1);
|
|
67
|
-
|
|
68
|
-
unregister2();
|
|
69
|
-
|
|
70
|
-
await swup.hooks.call('enable', undefined);
|
|
71
|
-
|
|
72
|
-
expect(handler1).toBeCalledTimes(2);
|
|
73
|
-
expect(handler2).toBeCalledTimes(1);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should trigger custom handlers', async () => {
|
|
77
|
-
const swup = new Swup();
|
|
78
|
-
const handler = vi.fn();
|
|
79
|
-
|
|
80
|
-
swup.hooks.on('enable', handler);
|
|
81
|
-
|
|
82
|
-
await swup.hooks.call('enable', undefined);
|
|
83
|
-
|
|
84
|
-
expect(handler).toBeCalledTimes(1);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should only trigger custom handlers once if requested', async () => {
|
|
88
|
-
const swup = new Swup();
|
|
89
|
-
const handler = vi.fn();
|
|
90
|
-
|
|
91
|
-
swup.hooks.on('enable', handler, { once: true });
|
|
92
|
-
|
|
93
|
-
await swup.hooks.call('enable', undefined, () => {});
|
|
94
|
-
await swup.hooks.call('enable', undefined, () => {});
|
|
95
|
-
|
|
96
|
-
expect(handler).toBeCalledTimes(1);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('should only trigger custom handlers once if using alias', async () => {
|
|
100
|
-
const swup = new Swup();
|
|
101
|
-
const handler = vi.fn();
|
|
102
|
-
|
|
103
|
-
swup.hooks.once('enable', handler);
|
|
104
|
-
|
|
105
|
-
await swup.hooks.call('enable', undefined, () => {});
|
|
106
|
-
await swup.hooks.call('enable', undefined, () => {});
|
|
107
|
-
|
|
108
|
-
expect(handler).toBeCalledTimes(1);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('should trigger original handlers', async () => {
|
|
112
|
-
const swup = new Swup();
|
|
113
|
-
const handler = vi.fn();
|
|
114
|
-
|
|
115
|
-
await swup.hooks.call('enable', undefined, handler);
|
|
116
|
-
|
|
117
|
-
expect(handler).toBeCalledTimes(1);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should allow triggering custom handlers before original handler', async () => {
|
|
121
|
-
const swup = new Swup();
|
|
122
|
-
|
|
123
|
-
let called: Array<string> = [];
|
|
124
|
-
const handlers = {
|
|
125
|
-
before: () => {
|
|
126
|
-
called.push('before');
|
|
127
|
-
},
|
|
128
|
-
original: () => {
|
|
129
|
-
called.push('original');
|
|
130
|
-
},
|
|
131
|
-
normal: () => {
|
|
132
|
-
called.push('normal');
|
|
133
|
-
},
|
|
134
|
-
after: () => {
|
|
135
|
-
called.push('after');
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
swup.hooks.on('disable', handlers.before, { before: true });
|
|
140
|
-
swup.hooks.on('disable', handlers.normal, {});
|
|
141
|
-
swup.hooks.on('disable', handlers.after, {});
|
|
142
|
-
|
|
143
|
-
await swup.hooks.call('disable', undefined, handlers.original);
|
|
144
|
-
|
|
145
|
-
expect(called).toEqual(['before', 'original', 'normal', 'after']);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('should sort custom handlers by priority', async () => {
|
|
149
|
-
const swup = new Swup();
|
|
150
|
-
|
|
151
|
-
let called: Array<number> = [];
|
|
152
|
-
const handlers = {
|
|
153
|
-
1: () => {
|
|
154
|
-
called.push(1);
|
|
155
|
-
},
|
|
156
|
-
2: () => {
|
|
157
|
-
called.push(2);
|
|
158
|
-
},
|
|
159
|
-
3: () => {
|
|
160
|
-
called.push(3);
|
|
161
|
-
},
|
|
162
|
-
4: () => {
|
|
163
|
-
called.push(4);
|
|
164
|
-
},
|
|
165
|
-
5: () => {
|
|
166
|
-
called.push(5);
|
|
167
|
-
},
|
|
168
|
-
6: () => {
|
|
169
|
-
called.push(6);
|
|
170
|
-
},
|
|
171
|
-
7: () => {
|
|
172
|
-
called.push(7);
|
|
173
|
-
},
|
|
174
|
-
8: () => {
|
|
175
|
-
called.push(8);
|
|
176
|
-
},
|
|
177
|
-
9: () => {
|
|
178
|
-
called.push(9);
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
swup.hooks.on('disable', handlers['1'], { priority: 2, before: true });
|
|
183
|
-
swup.hooks.on('disable', handlers['2'], { priority: -1, before: true });
|
|
184
|
-
swup.hooks.on('disable', handlers['3'], { priority: 1 });
|
|
185
|
-
swup.hooks.on('disable', handlers['9']);
|
|
186
|
-
swup.hooks.on('disable', handlers['4']);
|
|
187
|
-
swup.hooks.on('disable', handlers['8'], { priority: 4 });
|
|
188
|
-
swup.hooks.on('disable', handlers['7'], { priority: 4 });
|
|
189
|
-
|
|
190
|
-
await swup.hooks.call('disable', undefined, handlers['5']);
|
|
191
|
-
|
|
192
|
-
expect(called).toEqual([2, 1, 5, 9, 4, 3, 8, 7]);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('should allow replacing original handlers', async () => {
|
|
196
|
-
const swup = new Swup();
|
|
197
|
-
const customHandler = vi.fn();
|
|
198
|
-
const defaultHandler = vi.fn();
|
|
199
|
-
|
|
200
|
-
swup.hooks.on('enable', customHandler, { replace: true });
|
|
201
|
-
|
|
202
|
-
await swup.hooks.call('enable', undefined, defaultHandler);
|
|
203
|
-
|
|
204
|
-
expect(defaultHandler).toBeCalledTimes(0);
|
|
205
|
-
expect(customHandler).toBeCalledTimes(1);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('should only execute the last replacing handler', async () => {
|
|
209
|
-
const swup = new Swup();
|
|
210
|
-
const firstHandler = vi.fn();
|
|
211
|
-
const secondHandler = vi.fn();
|
|
212
|
-
const defaultHandler = vi.fn();
|
|
213
|
-
|
|
214
|
-
swup.hooks.on('enable', firstHandler, { replace: true });
|
|
215
|
-
swup.hooks.on('enable', secondHandler, { replace: true });
|
|
216
|
-
|
|
217
|
-
await swup.hooks.call('enable', undefined, defaultHandler);
|
|
218
|
-
|
|
219
|
-
expect(defaultHandler).toBeCalledTimes(0);
|
|
220
|
-
expect(firstHandler).toBeCalledTimes(0);
|
|
221
|
-
expect(secondHandler).toBeCalledTimes(1);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should pass original handler into replacing handlers', async () => {
|
|
225
|
-
const swup = new Swup();
|
|
226
|
-
const args = { foo: 'bar' };
|
|
227
|
-
const customHandler = vi.fn((c, a, handler) => handler(c, args));
|
|
228
|
-
const defaultHandler = vi.fn();
|
|
229
|
-
const visit = swup.visit;
|
|
230
|
-
|
|
231
|
-
swup.hooks.on('enable', customHandler, { replace: true });
|
|
232
|
-
|
|
233
|
-
await swup.hooks.call('enable', undefined, defaultHandler);
|
|
234
|
-
|
|
235
|
-
expect(customHandler).toBeCalledWith(visit, undefined, defaultHandler);
|
|
236
|
-
expect(defaultHandler).toBeCalledWith(visit, args);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it('should return result of replacing handler', async () => {
|
|
240
|
-
const swup = new Swup();
|
|
241
|
-
const customHandler = vi.fn(async () => 'foo');
|
|
242
|
-
const defaultHandler = vi.fn(async () => 'bar');
|
|
243
|
-
|
|
244
|
-
swup.hooks.on('enable', customHandler, { replace: true });
|
|
245
|
-
|
|
246
|
-
const result = await swup.hooks.call('enable', undefined, defaultHandler);
|
|
247
|
-
|
|
248
|
-
expect(result).toEqual('foo');
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it('should pass previous handler into nested replacing handlers', async () => {
|
|
252
|
-
const swup = new Swup();
|
|
253
|
-
const args1 = { foo: 'bar' };
|
|
254
|
-
const args2 = { foo: 'baz' };
|
|
255
|
-
const args3 = { foo: 'bag' };
|
|
256
|
-
const defaultHandler = vi.fn();
|
|
257
|
-
const firstHandler = vi.fn((c, a, handler) => handler(c, args1));
|
|
258
|
-
const secondHandler = vi.fn((c, a, handler) => handler(c, args2));
|
|
259
|
-
const thirdHandler = vi.fn((c, a, handler) => handler(c, args3, handler));
|
|
260
|
-
const visit = swup.visit;
|
|
261
|
-
|
|
262
|
-
swup.hooks.on('enable', firstHandler, { replace: true });
|
|
263
|
-
swup.hooks.on('enable', secondHandler, { replace: true });
|
|
264
|
-
swup.hooks.on('enable', thirdHandler, { replace: true });
|
|
265
|
-
|
|
266
|
-
await swup.hooks.call('enable', undefined, defaultHandler);
|
|
267
|
-
|
|
268
|
-
expect(defaultHandler).toBeCalledWith(visit, args1);
|
|
269
|
-
expect(thirdHandler).toBeCalledWith(visit, undefined, expect.any(Function));
|
|
270
|
-
expect(secondHandler).toBeCalledWith(visit, args3, expect.any(Function));
|
|
271
|
-
expect(firstHandler).toBeCalledWith(visit, args2, expect.any(Function));
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('should not pass original handler into normal handlers', async () => {
|
|
275
|
-
const swup = new Swup();
|
|
276
|
-
const listener = vi.fn();
|
|
277
|
-
const handler = vi.fn();
|
|
278
|
-
const visit = swup.visit;
|
|
279
|
-
|
|
280
|
-
swup.hooks.on('enable', listener);
|
|
281
|
-
|
|
282
|
-
await swup.hooks.call('enable', undefined, handler);
|
|
283
|
-
|
|
284
|
-
expect(listener).toBeCalledWith(visit, undefined, undefined);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it('should trigger event handler with visit and args', async () => {
|
|
288
|
-
const swup = new Swup();
|
|
289
|
-
const handler: Handler<'history:popstate'> = vi.fn();
|
|
290
|
-
const visit = swup.visit;
|
|
291
|
-
const args = { event: new PopStateEvent('') };
|
|
292
|
-
|
|
293
|
-
swup.hooks.on('history:popstate', handler);
|
|
294
|
-
await swup.hooks.call('history:popstate', args);
|
|
295
|
-
|
|
296
|
-
expect(handler).toBeCalledTimes(1);
|
|
297
|
-
expect(handler).toBeCalledWith(visit, args, undefined);
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
describe('Types', () => {
|
|
302
|
-
it('error when necessary', async () => {
|
|
303
|
-
const swup = new Swup();
|
|
304
|
-
|
|
305
|
-
// @ts-expect-no-error
|
|
306
|
-
swup.hooks.on('history:popstate', (visit: Visit, { event }: { event: PopStateEvent }) => {});
|
|
307
|
-
// @ts-expect-no-error
|
|
308
|
-
await swup.hooks.call('history:popstate', { event: new PopStateEvent('') });
|
|
309
|
-
|
|
310
|
-
// @ts-expect-error: first arg must be Visit object
|
|
311
|
-
swup.hooks.on('history:popstate', ({ event: MouseEvent }) => {});
|
|
312
|
-
// @ts-expect-error: event arg must be PopStateEvent
|
|
313
|
-
swup.hooks.on('history:popstate', (visit: Visit, { event }: { event: MouseEvent }) => {});
|
|
314
|
-
// @ts-expect-error: event arg must be PopStateEvent
|
|
315
|
-
await swup.hooks.call('history:popstate', { event: new MouseEvent('') });
|
|
316
|
-
// @ts-expect-error: handler arg must be optional: handler?
|
|
317
|
-
swup.hooks.replace('enable', (visit: Visit, args: undefined, handler: DefaultHandler<'enable'>) => {});
|
|
318
|
-
});
|
|
319
|
-
});
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import Swup from '../../index.js';
|
|
4
|
-
|
|
5
|
-
function createPlugin(plugin = {}) {
|
|
6
|
-
return {
|
|
7
|
-
name: 'SwupTestPlugin',
|
|
8
|
-
isSwupPlugin: true as const,
|
|
9
|
-
mount: vi.fn(() => {}),
|
|
10
|
-
unmount: vi.fn(() => {}),
|
|
11
|
-
_checkRequirements: vi.fn(() => true),
|
|
12
|
-
...plugin
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
describe('Plugins', () => {
|
|
17
|
-
it('should mount and unmount plugins', function () {
|
|
18
|
-
const plugin = createPlugin();
|
|
19
|
-
const swup = new Swup();
|
|
20
|
-
swup.use(plugin);
|
|
21
|
-
swup.unuse(plugin);
|
|
22
|
-
|
|
23
|
-
expect(plugin.mount.mock.calls).toHaveLength(1);
|
|
24
|
-
expect(plugin.unmount.mock.calls).toHaveLength(1);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should mount plugins from options', function () {
|
|
28
|
-
const plugin = createPlugin();
|
|
29
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
30
|
-
expect(plugin.mount.mock.calls).toHaveLength(1);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should find a plugin instance by reference', function () {
|
|
34
|
-
const plugin = createPlugin({ name: 'SwupExamplePlugin' });
|
|
35
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
36
|
-
const instance = swup.findPlugin(plugin);
|
|
37
|
-
|
|
38
|
-
expect(instance).toEqual(expect.objectContaining({ name: 'SwupExamplePlugin' }));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should not find a plugin instance by wrong reference', function () {
|
|
42
|
-
const plugin = createPlugin({ name: 'SwupExamplePlugin' });
|
|
43
|
-
const otherPlugin = createPlugin({ name: 'SwupOtherPlugin' });
|
|
44
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
45
|
-
const instance = swup.findPlugin(otherPlugin);
|
|
46
|
-
|
|
47
|
-
expect(instance).toEqual(undefined);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should find a plugin instance by name', function () {
|
|
51
|
-
const plugin = createPlugin({ name: 'SwupExamplePlugin' });
|
|
52
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
53
|
-
const instance = swup.findPlugin('SwupExamplePlugin');
|
|
54
|
-
|
|
55
|
-
expect(instance).toEqual(expect.objectContaining({ name: 'SwupExamplePlugin' }));
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should find a plugin instance by unprefixed name', function () {
|
|
59
|
-
const plugin = createPlugin({ name: 'SwupExamplePlugin' });
|
|
60
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
61
|
-
const instance = swup.findPlugin('ExamplePlugin');
|
|
62
|
-
|
|
63
|
-
expect(instance).toEqual(expect.objectContaining({ name: 'SwupExamplePlugin' }));
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should check plugin requirements', function () {
|
|
67
|
-
const plugin = createPlugin();
|
|
68
|
-
const swup = new Swup({ plugins: [plugin] });
|
|
69
|
-
expect(plugin._checkRequirements.mock.calls).toHaveLength(1);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should reject plugins with unmet requirements', function () {
|
|
73
|
-
const allowedPlugin = createPlugin({
|
|
74
|
-
name: 'AllowedPlugin',
|
|
75
|
-
_checkRequirements: () => true
|
|
76
|
-
});
|
|
77
|
-
const unallowedPlugin = createPlugin({
|
|
78
|
-
name: 'UnallowedPlugin',
|
|
79
|
-
_checkRequirements: () => false
|
|
80
|
-
});
|
|
81
|
-
const swup = new Swup({ plugins: [allowedPlugin, unallowedPlugin] });
|
|
82
|
-
|
|
83
|
-
const allowedInstance = swup.findPlugin(allowedPlugin);
|
|
84
|
-
expect(allowedInstance).toEqual(expect.objectContaining({ name: 'AllowedPlugin' }));
|
|
85
|
-
|
|
86
|
-
const unallowedInstance = swup.findPlugin(unallowedPlugin);
|
|
87
|
-
expect(unallowedInstance).toBeUndefined();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi, afterEach } from 'vitest';
|
|
2
|
-
import Swup from '../../Swup.js';
|
|
3
|
-
import type { PageData } from '../fetchPage.js';
|
|
4
|
-
import { JSDOM } from 'jsdom';
|
|
5
|
-
|
|
6
|
-
const getHtml = (body: string): string => {
|
|
7
|
-
return /*html*/ `
|
|
8
|
-
<!DOCTYPE html>
|
|
9
|
-
<body>
|
|
10
|
-
${body}
|
|
11
|
-
</body>
|
|
12
|
-
`;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const mockPage = (body: string): PageData => {
|
|
16
|
-
return {
|
|
17
|
-
url: '',
|
|
18
|
-
html: getHtml(body)
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const stubGlobalDocument = (body: string): void => {
|
|
23
|
-
const dom = new JSDOM(getHtml(body));
|
|
24
|
-
vi.stubGlobal('document', dom.window.document);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
describe('replaceContent', () => {
|
|
28
|
-
afterEach(() => {
|
|
29
|
-
vi.unstubAllGlobals();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should replace containers', () => {
|
|
33
|
-
stubGlobalDocument(/*html*/ `
|
|
34
|
-
<div id="container-1" data-from="current"></div>
|
|
35
|
-
<div id="container-2" data-from="current"></div>
|
|
36
|
-
<div id="container-3" data-from="current"></div>
|
|
37
|
-
`);
|
|
38
|
-
|
|
39
|
-
const page = mockPage(/*html*/ `
|
|
40
|
-
<div id="container-1" data-from="incoming"></div>
|
|
41
|
-
<div id="container-2" data-from="incoming"></div>`);
|
|
42
|
-
const swup = new Swup();
|
|
43
|
-
|
|
44
|
-
const result = swup.replaceContent(page, { containers: ['#container-1', '#container-2'] });
|
|
45
|
-
|
|
46
|
-
expect(result).toBe(true);
|
|
47
|
-
expect(document.querySelector('#container-1')?.getAttribute('data-from')).toBe('incoming');
|
|
48
|
-
expect(document.querySelector('#container-2')?.getAttribute('data-from')).toBe('incoming');
|
|
49
|
-
expect(document.querySelector('#container-3')?.getAttribute('data-from')).toBe('current');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should handle missing containers in current DOM', () => {
|
|
53
|
-
stubGlobalDocument(/*html*/ `
|
|
54
|
-
<div id="container-1" data-from="current"></div>
|
|
55
|
-
`);
|
|
56
|
-
const warn = vi.spyOn(console, 'warn');
|
|
57
|
-
const page = mockPage(/*html*/ `
|
|
58
|
-
<div id="container-1" data-from="incoming"></div>
|
|
59
|
-
<div id="container-2" data-from="incoming"></div>
|
|
60
|
-
`);
|
|
61
|
-
|
|
62
|
-
const swup = new Swup();
|
|
63
|
-
const result = swup.replaceContent(page, { containers: ['#container-1', '#missing'] });
|
|
64
|
-
|
|
65
|
-
expect(result).toBe(false);
|
|
66
|
-
expect(warn).not.toBeCalledWith(
|
|
67
|
-
'[swup] Container missing in current document: #container-1'
|
|
68
|
-
);
|
|
69
|
-
expect(warn).toBeCalledWith('[swup] Container missing in current document: #missing');
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should handle missing containers in incoming DOM', () => {
|
|
73
|
-
stubGlobalDocument(/*html*/ `
|
|
74
|
-
<div id="container-1" data-from="current"></div>
|
|
75
|
-
<div id="container-2" data-from="current"></div>
|
|
76
|
-
<div id="container-3" data-from="current"></div>
|
|
77
|
-
`);
|
|
78
|
-
const warn = vi.spyOn(console, 'warn');
|
|
79
|
-
const page = mockPage(/*html*/ `
|
|
80
|
-
<div id="container-1" data-from="incoming"></div>`);
|
|
81
|
-
|
|
82
|
-
const swup = new Swup();
|
|
83
|
-
const result = swup.replaceContent(page, { containers: ['#container-1', '#missing'] });
|
|
84
|
-
|
|
85
|
-
expect(result).toBe(false);
|
|
86
|
-
expect(warn).not.toBeCalledWith(
|
|
87
|
-
'[swup] Container missing in incoming document: #container-1'
|
|
88
|
-
);
|
|
89
|
-
expect(warn).toBeCalledWith('[swup] Container missing in incoming document: #missing');
|
|
90
|
-
});
|
|
91
|
-
});
|