-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from swup/feat/modify-rules
- Loading branch information
Showing
7 changed files
with
187 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { cloneRules } from '../../src/inc/functions.js'; | ||
|
||
describe('cloneRules()', () => { | ||
it('should clone rules', () => { | ||
const original = [ | ||
{ from: 'foo', to: 'bar', containers: ['#foobar'] }, | ||
{ from: ['/', 'baz'], to: ['/', 'bat'], containers: ['#bazbat'] } | ||
]; | ||
/** test equality with a reference */ | ||
const reference = original; | ||
expect(original === reference).toEqual(true); | ||
|
||
/** test shallow copy */ | ||
const shallowCopy = { ...original }; | ||
expect(original === shallowCopy).toEqual(false); | ||
expect(original[0].containers === shallowCopy[0].containers).toEqual(true); | ||
expect(original[1].from === shallowCopy[1].from).toEqual(true); | ||
|
||
/** test deep clone */ | ||
const clone = cloneRules(original); | ||
expect(original === clone).toEqual(false); | ||
expect(original[0].containers === clone[0].containers).toEqual(false); | ||
expect(original[1].from === clone[1].from).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; | ||
import { getMountedPluginInstance, spyOnConsole } from './inc/helpers.js'; | ||
|
||
const defaultRule = { | ||
from: '/page-1/', | ||
to: '/page-2/', | ||
containers: ['#default'] | ||
}; | ||
const fragmentPlugin = getMountedPluginInstance({ | ||
rules: [defaultRule] | ||
}); | ||
|
||
describe('modify fragment rules', () => { | ||
beforeEach(() => { | ||
spyOnConsole(); | ||
/** Reset the rules */ | ||
fragmentPlugin.setRules([defaultRule]); | ||
}); | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('should return unparsed raw rules', () => { | ||
expect(fragmentPlugin.getRules()).toEqual([defaultRule]); | ||
}); | ||
|
||
it('should provide API methods on the swup instance', () => { | ||
expect(fragmentPlugin.getRules).toBeTypeOf('function'); | ||
expect(fragmentPlugin.setRules).toBeTypeOf('function'); | ||
expect(fragmentPlugin.prependRule).toBeTypeOf('function'); | ||
expect(fragmentPlugin.appendRule).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should provide access to prependRule() and appendRule()', () => { | ||
const prependRule = { | ||
from: '/corge/', | ||
to: '/grault/', | ||
containers: ['#corgegrault'] | ||
}; | ||
const appendRule = { | ||
from: '/garply/', | ||
to: '/waldo/', | ||
containers: ['#garplywaldo'] | ||
}; | ||
|
||
fragmentPlugin.prependRule(prependRule); | ||
fragmentPlugin.appendRule(appendRule); | ||
|
||
expect(fragmentPlugin.getRules()).toEqual([prependRule, defaultRule, appendRule]); | ||
}); | ||
|
||
it('should provide access to getRules() and setRules()', () => { | ||
/** Add a rule using the plugin's API, *after* the existing rules */ | ||
const appendRule = { | ||
from: '/foo/', | ||
to: '/bar/', | ||
containers: ['#foobar'], | ||
name: 'from-plugin' | ||
}; | ||
fragmentPlugin.setRules([...fragmentPlugin.getRules(), appendRule]); | ||
|
||
/** Add a rule using swup's API, *before* the existing rules */ | ||
const prependRule = { | ||
from: '/baz/', | ||
to: '/bat/', | ||
containers: ['#bazbat'], | ||
name: 'from-swup' | ||
}; | ||
fragmentPlugin.setRules([prependRule, ...fragmentPlugin.getRules()]); | ||
|
||
expect(fragmentPlugin.getRules()).toEqual([prependRule, defaultRule, appendRule]); | ||
}); | ||
|
||
it('should remove rules', () => { | ||
fragmentPlugin.appendRule({ | ||
from: '/foo/', | ||
to: '/bar/', | ||
containers: ['#foobar'], | ||
name: 'remove-me' | ||
}); | ||
const rules = fragmentPlugin.getRules(); | ||
fragmentPlugin.setRules(rules.filter((rule) => rule.name !== 'remove-me')); | ||
expect(fragmentPlugin.getRules()).toEqual([defaultRule]); | ||
}); | ||
}); |