|
1 |
| -import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
2 | 2 | import { getMountedPluginInstance, spyOnConsole } from './inc/helpers.js';
|
3 | 3 |
|
| 4 | +const defaultRule = { |
| 5 | + from: '/page-1/', |
| 6 | + to: '/page-2/', |
| 7 | + containers: ['#default'] |
| 8 | +}; |
4 | 9 | const fragmentPlugin = getMountedPluginInstance({
|
5 |
| - rules: [ |
6 |
| - { |
7 |
| - from: '/page-1/', |
8 |
| - to: '/page-2/', |
9 |
| - containers: ['#fragment-1'] |
10 |
| - } |
11 |
| - ] |
| 10 | + rules: [defaultRule] |
12 | 11 | });
|
| 12 | +const { swup } = fragmentPlugin; |
13 | 13 |
|
14 |
| -describe('get and set fragment rules', () => { |
| 14 | +describe('modify fragment rules', () => { |
| 15 | + beforeEach(() => { |
| 16 | + spyOnConsole(); |
| 17 | + /** Reset the rules */ |
| 18 | + fragmentPlugin.setRules([defaultRule]); |
| 19 | + }) |
15 | 20 | afterEach(() => {
|
16 | 21 | vi.restoreAllMocks();
|
17 | 22 | });
|
18 | 23 |
|
19 |
| - it('should return raw rules', () => { |
20 |
| - expect(fragmentPlugin.getRules()).toEqual([ |
21 |
| - { |
22 |
| - from: '/page-1/', |
23 |
| - to: '/page-2/', |
24 |
| - containers: ['#fragment-1'] |
25 |
| - } |
26 |
| - ]); |
| 24 | + it('should return unparsed raw rules', () => { |
| 25 | + expect(fragmentPlugin.getRules()).toEqual([defaultRule]); |
27 | 26 | });
|
28 | 27 |
|
29 |
| - it('should be possible to modify rules at runtime', () => { |
30 |
| - const console = spyOnConsole(); |
31 |
| - const { swup } = fragmentPlugin; |
| 28 | + it('should provide API methods on the swup instance', () => { |
| 29 | + expect(swup.getFragmentRules).toBeTypeOf('function'); |
| 30 | + expect(swup.setFragmentRules).toBeTypeOf('function'); |
| 31 | + expect(swup.prependFragmentRule).toBeTypeOf('function'); |
| 32 | + expect(swup.appendFragmentRule).toBeTypeOf('function'); |
| 33 | + }); |
32 | 34 |
|
33 |
| - /** Add a rule using the plugin's API, *after* the existing rules */ |
34 |
| - fragmentPlugin.setRules([ |
35 |
| - ...fragmentPlugin.getRules(), |
36 |
| - { |
37 |
| - from: '/foo/', |
38 |
| - to: '/bar/', |
39 |
| - containers: ['#fragment-1'], |
40 |
| - name: 'from-plugin' |
41 |
| - } |
42 |
| - ]); |
| 35 | + it('should provide access to prependRule() and appendRule()', () => { |
| 36 | + const prependRule = { |
| 37 | + from: '/corge/', |
| 38 | + to: '/grault/', |
| 39 | + containers: ['#corgegrault'] |
| 40 | + }; |
| 41 | + const appendRule = { |
| 42 | + from: '/garply/', |
| 43 | + to: '/waldo/', |
| 44 | + containers: ['#garplywaldo'] |
| 45 | + }; |
43 | 46 |
|
44 |
| - /** Add a rule using swup's API, *before* the existing rules */ |
45 |
| - swup.setFragmentRules?.([ |
46 |
| - { |
47 |
| - from: '/foo/', |
48 |
| - to: '/bar/', |
49 |
| - containers: ['#fragment-1'], |
50 |
| - name: 'from-swup' |
51 |
| - }, |
52 |
| - ...swup.getFragmentRules?.() || [], |
53 |
| - ]); |
| 47 | + swup.prependFragmentRule?.(prependRule); |
| 48 | + swup.appendFragmentRule?.(appendRule); |
54 | 49 |
|
55 |
| - const fromPlugin = fragmentPlugin.getRules(); |
56 |
| - const fromSwup = swup.getFragmentRules?.(); |
| 50 | + expect(swup.getFragmentRules?.()).toEqual([prependRule, defaultRule, appendRule]); |
| 51 | + }); |
57 | 52 |
|
58 |
| - /** make sure the method exists on swup as well */ |
59 |
| - expect(fromPlugin).toEqual(fromSwup); |
| 53 | + it('should provide access to getRules() and setRules()', () => { |
| 54 | + /** Add a rule using the plugin's API, *after* the existing rules */ |
| 55 | + const appendRule = { |
| 56 | + from: '/foo/', |
| 57 | + to: '/bar/', |
| 58 | + containers: ['#foobar'], |
| 59 | + name: 'from-plugin' |
| 60 | + }; |
| 61 | + fragmentPlugin.setRules([...fragmentPlugin.getRules(), appendRule]); |
| 62 | + |
| 63 | + /** Add a rule using swup's API, *before* the existing rules */ |
| 64 | + const prependRule = { |
| 65 | + from: '/baz/', |
| 66 | + to: '/bat/', |
| 67 | + containers: ['#bazbat'], |
| 68 | + name: 'from-swup' |
| 69 | + }; |
| 70 | + swup.setFragmentRules?.([prependRule, ...(swup.getFragmentRules?.() || [])]); |
60 | 71 |
|
61 |
| - expect(fromPlugin).toEqual([ |
62 |
| - { |
63 |
| - from: '/foo/', |
64 |
| - to: '/bar/', |
65 |
| - containers: ['#fragment-1'], |
66 |
| - name: 'from-swup' |
67 |
| - }, |
68 |
| - { |
69 |
| - from: '/page-1/', |
70 |
| - to: '/page-2/', |
71 |
| - containers: ['#fragment-1'] |
72 |
| - }, |
73 |
| - { |
74 |
| - from: '/foo/', |
75 |
| - to: '/bar/', |
76 |
| - containers: ['#fragment-1'], |
77 |
| - name: 'from-plugin' |
78 |
| - } |
79 |
| - ]); |
| 72 | + expect(swup.getFragmentRules?.()).toEqual([prependRule, defaultRule, appendRule]); |
80 | 73 | });
|
81 | 74 | });
|
0 commit comments