swup 4.3.4 → 4.4.1
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 +10 -5
- package/dist/types/Swup.d.ts.map +1 -0
- package/dist/types/config/version.d.ts +1 -0
- package/dist/types/config/version.d.ts.map +1 -0
- package/dist/types/helpers/Location.d.ts +1 -0
- package/dist/types/helpers/Location.d.ts.map +1 -0
- package/dist/types/helpers/classify.d.ts +1 -0
- package/dist/types/helpers/classify.d.ts.map +1 -0
- package/dist/types/helpers/createHistoryRecord.d.ts +1 -0
- package/dist/types/helpers/createHistoryRecord.d.ts.map +1 -0
- package/dist/types/helpers/delegateEvent.d.ts +3 -2
- package/dist/types/helpers/delegateEvent.d.ts.map +1 -0
- package/dist/types/helpers/getCurrentUrl.d.ts +1 -0
- package/dist/types/helpers/getCurrentUrl.d.ts.map +1 -0
- package/dist/types/helpers/matchPath.d.ts +2 -1
- package/dist/types/helpers/matchPath.d.ts.map +1 -0
- package/dist/types/helpers/updateHistoryRecord.d.ts +1 -0
- package/dist/types/helpers/updateHistoryRecord.d.ts.map +1 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/modules/Cache.d.ts +3 -2
- package/dist/types/modules/Cache.d.ts.map +1 -0
- package/dist/types/modules/Classes.d.ts +2 -1
- package/dist/types/modules/Classes.d.ts.map +1 -0
- package/dist/types/modules/Hooks.d.ts +10 -4
- package/dist/types/modules/Hooks.d.ts.map +1 -0
- package/dist/types/modules/Visit.d.ts +4 -2
- package/dist/types/modules/Visit.d.ts.map +1 -0
- package/dist/types/modules/animatePageIn.d.ts +2 -1
- package/dist/types/modules/animatePageIn.d.ts.map +1 -0
- package/dist/types/modules/animatePageOut.d.ts +2 -1
- package/dist/types/modules/animatePageOut.d.ts.map +1 -0
- package/dist/types/modules/awaitAnimations.d.ts +3 -1
- package/dist/types/modules/awaitAnimations.d.ts.map +1 -0
- package/dist/types/modules/fetchPage.d.ts +10 -3
- package/dist/types/modules/fetchPage.d.ts.map +1 -0
- package/dist/types/modules/getAnchorElement.d.ts +1 -0
- package/dist/types/modules/getAnchorElement.d.ts.map +1 -0
- package/dist/types/modules/navigate.d.ts +4 -3
- package/dist/types/modules/navigate.d.ts.map +1 -0
- package/dist/types/modules/plugins.d.ts +2 -1
- package/dist/types/modules/plugins.d.ts.map +1 -0
- package/dist/types/modules/renderPage.d.ts +3 -2
- package/dist/types/modules/renderPage.d.ts.map +1 -0
- package/dist/types/modules/replaceContent.d.ts +4 -2
- package/dist/types/modules/replaceContent.d.ts.map +1 -0
- package/dist/types/modules/resolveUrl.d.ts +2 -1
- package/dist/types/modules/resolveUrl.d.ts.map +1 -0
- package/dist/types/modules/scrollToContent.d.ts +2 -1
- package/dist/types/modules/scrollToContent.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils.d.ts +1 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +13 -17
- package/src/Swup.ts +18 -7
- package/src/helpers/delegateEvent.ts +6 -2
- package/src/helpers/matchPath.ts +1 -1
- package/src/helpers/updateHistoryRecord.ts +1 -1
- package/src/modules/Cache.ts +2 -2
- package/src/modules/Classes.ts +1 -1
- package/src/modules/Hooks.ts +9 -4
- package/src/modules/Visit.ts +3 -2
- package/src/modules/animatePageIn.ts +1 -1
- package/src/modules/animatePageOut.ts +1 -1
- package/src/modules/awaitAnimations.ts +2 -1
- package/src/modules/fetchPage.ts +48 -9
- package/src/modules/navigate.ts +26 -17
- package/src/modules/plugins.ts +1 -1
- package/src/modules/renderPage.ts +2 -2
- package/src/modules/replaceContent.ts +3 -2
- package/src/modules/resolveUrl.ts +1 -1
- package/src/modules/scrollToContent.ts +1 -1
- 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/dist/types/modules/__test__/visit.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
- package/src/modules/__test__/visit.test.ts +0 -92
|
@@ -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
|
-
});
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import Swup from '../../Swup.js';
|
|
3
|
-
import { Visit, createVisit } from '../Visit.js';
|
|
4
|
-
|
|
5
|
-
class SwupWithPublicVisitMethods extends Swup {
|
|
6
|
-
public createVisit = createVisit;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const swup = new SwupWithPublicVisitMethods();
|
|
10
|
-
let visit: Visit;
|
|
11
|
-
|
|
12
|
-
describe('Visit', () => {
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
visit = swup.createVisit({ to: '' });
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('is an object', () => {
|
|
18
|
-
expect(visit).to.be.an('object');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('has an id', () => {
|
|
22
|
-
expect(visit.id).to.be.a('number');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('generates unique ids', () => {
|
|
26
|
-
let id = visit.id;
|
|
27
|
-
visit = swup.createVisit({ to: '' });
|
|
28
|
-
expect(visit.id).to.not.equal(id);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('has a from object with the current URL', () => {
|
|
32
|
-
expect(visit.from).to.be.an('object');
|
|
33
|
-
expect(visit.from.url).to.be.a('string');
|
|
34
|
-
visit = swup.createVisit({ to: '', from: '/from' });
|
|
35
|
-
expect(visit.from).toMatchObject({ url: '/from' });
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('has a to object with the next URL', () => {
|
|
39
|
-
expect(visit.to).to.be.an('object');
|
|
40
|
-
expect(visit.to.url).to.be.a('string');
|
|
41
|
-
visit = swup.createVisit({ to: '/to' });
|
|
42
|
-
expect(visit.to).toMatchObject({ url: '/to' });
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('has an animation object', () => {
|
|
46
|
-
expect(visit.animation).to.be.an('object');
|
|
47
|
-
expect(visit.animation).toMatchObject({
|
|
48
|
-
animate: true,
|
|
49
|
-
name: undefined,
|
|
50
|
-
scope: swup.options.animationScope,
|
|
51
|
-
selector: swup.options.animationSelector
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('has a container array', () => {
|
|
56
|
-
expect(visit.containers).to.be.an('array');
|
|
57
|
-
expect(visit.containers).toEqual(swup.options.containers);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('has a trigger object', () => {
|
|
61
|
-
expect(visit.trigger).to.be.an('object');
|
|
62
|
-
expect(visit.trigger).toMatchObject({
|
|
63
|
-
el: undefined,
|
|
64
|
-
event: undefined
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('has a cache object', () => {
|
|
69
|
-
expect(visit.cache).to.be.an('object');
|
|
70
|
-
expect(visit.cache).toEqual({
|
|
71
|
-
read: swup.options.cache,
|
|
72
|
-
write: swup.options.cache
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('has a history object', () => {
|
|
77
|
-
expect(visit.history).to.be.an('object');
|
|
78
|
-
expect(visit.history).toEqual({
|
|
79
|
-
action: 'push',
|
|
80
|
-
popstate: false,
|
|
81
|
-
direction: undefined
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('has a scroll object', () => {
|
|
86
|
-
expect(visit.scroll).to.be.an('object');
|
|
87
|
-
expect(visit.scroll).toEqual({
|
|
88
|
-
reset: true,
|
|
89
|
-
target: undefined
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
});
|