| 1 |
import { describe, expect, it, vi } from 'vitest'; |
| 2 |
|
| 3 |
import Swup from '../../src/Swup.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 |
}); |