|
| 1 | +jest.mock('vuepress-plugin-a') |
| 2 | +jest.mock('@org/vuepress-plugin-a') |
| 3 | + |
| 4 | +const { |
| 5 | + resolvePlugin, |
| 6 | + hydratePlugin, |
| 7 | + resolveScopePackage |
| 8 | +} = require('../../lib/plugin-api/util') |
| 9 | + |
| 10 | +function resolveMockModule (name) { |
| 11 | + return require(`../../../../../__mocks__/${name}`) |
| 12 | +} |
| 13 | + |
| 14 | +// const Plugin = require('../../lib/plugin-api/index') |
| 15 | + |
| 16 | +describe('resolvePlugin', () => { |
| 17 | + test('should resolve scope packages correctly.', () => { |
| 18 | + const pkg1 = resolveScopePackage('@vuepress/plugin-a') |
| 19 | + expect(pkg1.org).toBe('vuepress') |
| 20 | + expect(pkg1.name).toBe('plugin-a') |
| 21 | + |
| 22 | + const pkg2 = resolveScopePackage('vuepress/plugin-a') |
| 23 | + expect(pkg2).toBe(null) |
| 24 | + |
| 25 | + const pkg3 = resolveScopePackage('vuepress-plugin-a') |
| 26 | + expect(pkg3).toBe(null) |
| 27 | + }) |
| 28 | + |
| 29 | + test('shoould resolve local plugin as expected.', () => { |
| 30 | + const plugin1 = () => {} |
| 31 | + const plugin2 = {} |
| 32 | + expect(resolvePlugin(plugin1)).toEqual({ name: 'plugin1', shortcut: 'plugin1', config: plugin1 }) |
| 33 | + expect(resolvePlugin(plugin2)).toEqual({ name: 'anonymous-1', shortcut: 'anonymous-1', config: plugin2 }) |
| 34 | + }) |
| 35 | + |
| 36 | + test('shoould resolve fullname usage correctly.', () => { |
| 37 | + let plugin = resolvePlugin('vuepress-plugin-a') |
| 38 | + expect(plugin.name).toBe('vuepress-plugin-a') |
| 39 | + expect(plugin.shortcut).toBe('a') |
| 40 | + expect(plugin.config).toBe(resolveMockModule('vuepress-plugin-a')) |
| 41 | + |
| 42 | + plugin = resolvePlugin('@org/vuepress-plugin-a') |
| 43 | + expect(plugin.name).toBe('@org/vuepress-plugin-a') |
| 44 | + expect(plugin.shortcut).toBe('@org/a') |
| 45 | + expect(plugin.config).toBe(resolveMockModule('@org/vuepress-plugin-a')) |
| 46 | + }) |
| 47 | + |
| 48 | + test('shoould resolve shortcut usage correctly.', () => { |
| 49 | + // normal package |
| 50 | + let plugin = resolvePlugin('a') |
| 51 | + expect(plugin.name).toBe('vuepress-plugin-a') |
| 52 | + expect(plugin.shortcut).toBe('a') |
| 53 | + expect(plugin.config).toBe(resolveMockModule('vuepress-plugin-a')) |
| 54 | + |
| 55 | + // scope packages |
| 56 | + plugin = resolvePlugin('@org/a') |
| 57 | + expect(plugin.name).toBe('@org/vuepress-plugin-a') |
| 58 | + expect(plugin.shortcut).toBe('@org/a') |
| 59 | + expect(plugin.config).toBe(resolveMockModule('@org/vuepress-plugin-a')) |
| 60 | + |
| 61 | + // special case for @vuepress package |
| 62 | + plugin = resolvePlugin('@vuepress/a') |
| 63 | + expect(plugin.name).toBe('@vuepress/plugin-a') |
| 64 | + expect(plugin.shortcut).toBe('@vuepress/a') |
| 65 | + expect(plugin.config).toBe(resolveMockModule('@vuepress/plugin-a')) |
| 66 | + }) |
| 67 | + |
| 68 | + test('shoould return null when plugin cannot be resolved.', () => { |
| 69 | + expect(resolvePlugin('c')).toEqual({ name: 'c', shortcut: 'c', config: null }) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('hydratePlugin', () => { |
| 74 | + test('shoould hydrate plugin correctly', () => { |
| 75 | + const plugin = { name: 'a', shortcut: 'a', config: { enhanceAppFiles: 'file' }} |
| 76 | + const hydratedPlugin = hydratePlugin(plugin, {}, {}) |
| 77 | + expect(hydratedPlugin.name).toBe('a') |
| 78 | + expect(hydratedPlugin.shortcut).toBe('a') |
| 79 | + expect(hydratedPlugin.enabled).toBe(true) |
| 80 | + expect(hydratedPlugin.enhanceAppFiles).toBe('file') |
| 81 | + }) |
| 82 | + |
| 83 | + test('shoould set \'enabled\' to false when \'pluginOptions\' is set to false.', () => { |
| 84 | + const plugin = { name: 'a', shortcut: 'a', config: {}} |
| 85 | + const hydratedPlugin = hydratePlugin(plugin, false, {}) |
| 86 | + expect(hydratedPlugin.name).toBe('a') |
| 87 | + expect(hydratedPlugin.shortcut).toBe('a') |
| 88 | + expect(hydratedPlugin.enabled).toBe(false) |
| 89 | + }) |
| 90 | + |
| 91 | + test('shoould hydrate functional plugin correctly.', () => { |
| 92 | + const config = jest.fn(() => ({ enhanceAppFiles: 'file' })) |
| 93 | + const plugin = { name: 'a', shortcut: 'a', config } |
| 94 | + const pluginOptions = {} |
| 95 | + const pluginContext = {} |
| 96 | + const hydratedPlugin = hydratePlugin(plugin, pluginOptions, pluginContext) |
| 97 | + expect(hydratedPlugin.name).toBe('a') |
| 98 | + expect(hydratedPlugin.shortcut).toBe('a') |
| 99 | + expect(hydratedPlugin.enabled).toBe(true) |
| 100 | + expect(hydratedPlugin.enhanceAppFiles).toBe('file') |
| 101 | + expect(config.mock.calls).toHaveLength(1) |
| 102 | + expect(config.mock.calls[0][0]).toBe(pluginOptions) |
| 103 | + expect(Object.getPrototypeOf(config.mock.calls[0][1])).toBe(pluginContext) |
| 104 | + }) |
| 105 | +}) |
0 commit comments