|
| 1 | +import { createApp, normalizeClientFilesHook } from '@vuepress/core' |
| 2 | +import type { ClientFilesHook } from '@vuepress/core' |
| 3 | +import { path } from '@vuepress/utils' |
| 4 | + |
| 5 | +const source = path.resolve(__dirname, 'fake-source') |
| 6 | +const app = createApp({ |
| 7 | + source, |
| 8 | +}) |
| 9 | +const clientFile = path.resolve( |
| 10 | + __dirname, |
| 11 | + '../__fixtures__/client-files/clientAppSetup.ts' |
| 12 | +) |
| 13 | +const clientFileNonExistent = path.resolve( |
| 14 | + __dirname, |
| 15 | + '../__fixtures__/client-files/non-existent.ts' |
| 16 | +) |
| 17 | +const clientFiles = [ |
| 18 | + path.resolve(__dirname, '../__fixtures__/client-files/clientAppSetup.ts'), |
| 19 | + path.resolve(__dirname, '../__fixtures__/client-files/clientAppEnhance.ts'), |
| 20 | +] |
| 21 | +const clientFilesNonExistent = [clientFile, clientFileNonExistent] |
| 22 | + |
| 23 | +describe('core > pluginApi > normalizeClientFilesHook', () => { |
| 24 | + describe('should keep function as is', () => { |
| 25 | + it('return value is string', async () => { |
| 26 | + const rawHook: ClientFilesHook['exposed'] = jest.fn((app) => clientFile) |
| 27 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 28 | + expect(await normalizedHook(app)).toEqual([clientFile]) |
| 29 | + expect(rawHook).toHaveBeenCalledTimes(1) |
| 30 | + expect(rawHook).toHaveBeenCalledWith(app) |
| 31 | + }) |
| 32 | + |
| 33 | + it('return value is string array', async () => { |
| 34 | + const rawHook: ClientFilesHook['exposed'] = jest.fn((app) => clientFiles) |
| 35 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 36 | + expect(await normalizedHook(app)).toEqual(clientFiles) |
| 37 | + expect(rawHook).toHaveBeenCalledTimes(1) |
| 38 | + expect(rawHook).toHaveBeenCalledWith(app) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should throw an error if file does not exist', async () => { |
| 42 | + const consoleError = console.error |
| 43 | + console.error = jest.fn() |
| 44 | + |
| 45 | + const rawHook: ClientFilesHook['exposed'] = clientFileNonExistent |
| 46 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 47 | + await expect(normalizedHook(app)).rejects.toThrow() |
| 48 | + expect(console.error).toHaveBeenCalled() |
| 49 | + |
| 50 | + console.error = consoleError |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('should wrap raw value with a function', () => { |
| 55 | + it('value is string', async () => { |
| 56 | + const rawHook: ClientFilesHook['exposed'] = clientFile |
| 57 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 58 | + expect(await normalizedHook(app)).toEqual([clientFile]) |
| 59 | + }) |
| 60 | + |
| 61 | + it('value is string array', async () => { |
| 62 | + const rawHook: ClientFilesHook['exposed'] = clientFiles |
| 63 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 64 | + expect(await normalizedHook(app)).toEqual(clientFiles) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should throw an error if file does not exist', async () => { |
| 68 | + const consoleError = console.error |
| 69 | + console.error = jest.fn() |
| 70 | + |
| 71 | + const rawHook: ClientFilesHook['exposed'] = clientFilesNonExistent |
| 72 | + const normalizedHook = normalizeClientFilesHook(rawHook) |
| 73 | + await expect(normalizedHook(app)).rejects.toThrow() |
| 74 | + expect(console.error).toHaveBeenCalled() |
| 75 | + |
| 76 | + console.error = consoleError |
| 77 | + }) |
| 78 | + }) |
| 79 | +}) |
0 commit comments