Skip to content

Commit 922b0cf

Browse files
committed
Add API methods prependFragmentRule and appendFragmentRule
1 parent f5d7374 commit 922b0cf

File tree

3 files changed

+80
-73
lines changed

3 files changed

+80
-73
lines changed

src/SwupFragmentPlugin.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ export default class SwupFragmentPlugin extends PluginBase {
2525
_rawRules: Rule[] = [];
2626
_parsedRules: ParsedRule[] = [];
2727

28-
setRules(rules: Rule[]) {
29-
this._rawRules = structuredClone(rules);
30-
this._parsedRules = rules.map((rule) => this.parseRule(rule));
31-
if (__DEV__) this.logger?.log('Updated fragment rules', this.getRules());
32-
}
33-
34-
getRules() {
35-
return structuredClone(this._rawRules);
36-
}
37-
3828
options: Options;
3929

4030
defaults: Options = {
@@ -75,6 +65,8 @@ export default class SwupFragmentPlugin extends PluginBase {
7565
swup.getFragmentVisit = this.getFragmentVisit.bind(this);
7666
swup.getFragmentRules = this.getRules.bind(this);
7767
swup.setFragmentRules = this.setRules.bind(this);
68+
swup.prependFragmentRule = this.prependRule.bind(this);
69+
swup.appendFragmentRule = this.appendRule.bind(this);
7870

7971
if (__DEV__) {
8072
this.logger?.warnIf(
@@ -95,9 +87,29 @@ export default class SwupFragmentPlugin extends PluginBase {
9587
swup.getFragmentVisit = undefined;
9688
swup.getFragmentRules = undefined;
9789
swup.setFragmentRules = undefined;
90+
swup.prependFragmentRule = undefined;
91+
swup.appendFragmentRule = undefined;
9892
cleanupFragmentElements();
9993
}
10094

95+
setRules(rules: Rule[]) {
96+
this._rawRules = structuredClone(rules);
97+
this._parsedRules = rules.map((rule) => this.parseRule(rule));
98+
if (__DEV__) this.logger?.log('Updated fragment rules', this.getRules());
99+
}
100+
101+
getRules() {
102+
return structuredClone(this._rawRules);
103+
}
104+
105+
prependRule(rule: Rule) {
106+
this.setRules([rule, ...this.getRules()]);
107+
}
108+
109+
appendRule(rule: Rule) {
110+
this.setRules([...this.getRules(), rule]);
111+
}
112+
101113
/**
102114
* Add a fragment rule
103115
* @param {Rule} rule The rule options

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ declare module 'swup' {
88
getFragmentVisit?: FragmentPlugin['getFragmentVisit'];
99
getFragmentRules?: FragmentPlugin['getRules'];
1010
setFragmentRules?: FragmentPlugin['setRules'];
11+
prependFragmentRule?: FragmentPlugin['prependRule'];
12+
appendFragmentRule?: FragmentPlugin['appendRule'];
1113
}
1214
export interface Visit {
1315
fragmentVisit?: FragmentVisit;

tests/vitest/modifyRules.test.ts

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,74 @@
1-
import { describe, expect, it, vi, afterEach } from 'vitest';
1+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
22
import { getMountedPluginInstance, spyOnConsole } from './inc/helpers.js';
33

4+
const defaultRule = {
5+
from: '/page-1/',
6+
to: '/page-2/',
7+
containers: ['#default']
8+
};
49
const fragmentPlugin = getMountedPluginInstance({
5-
rules: [
6-
{
7-
from: '/page-1/',
8-
to: '/page-2/',
9-
containers: ['#fragment-1']
10-
}
11-
]
10+
rules: [defaultRule]
1211
});
12+
const { swup } = fragmentPlugin;
1313

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+
})
1520
afterEach(() => {
1621
vi.restoreAllMocks();
1722
});
1823

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]);
2726
});
2827

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+
});
3234

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+
};
4346

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);
5449

55-
const fromPlugin = fragmentPlugin.getRules();
56-
const fromSwup = swup.getFragmentRules?.();
50+
expect(swup.getFragmentRules?.()).toEqual([prependRule, defaultRule, appendRule]);
51+
});
5752

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?.() || [])]);
6071

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]);
8073
});
8174
});

0 commit comments

Comments
 (0)