Skip to content

Commit b132f52

Browse files
committed
test(core): add unit tests for plugin api
1 parent cdb20f4 commit b132f52

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createApp, normalizeReturnObjectHook } from '@vuepress/core'
2+
import type { ReturnObjectHook } 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+
10+
describe('core > pluginApi > normalizeReturnObjectHook', () => {
11+
it('should keep function as is', () => {
12+
const rawHook: ReturnObjectHook['exposed'] = jest.fn((app) => ({
13+
foo: 'bar',
14+
}))
15+
const normalizedHook = normalizeReturnObjectHook(rawHook)
16+
expect(normalizedHook(app)).toEqual({ foo: 'bar' })
17+
expect(rawHook).toHaveBeenCalledTimes(1)
18+
expect(rawHook).toHaveBeenCalledWith(app)
19+
})
20+
21+
it('should wrap object with a function', () => {
22+
const rawHook: ReturnObjectHook['exposed'] = {
23+
foo: 'bar',
24+
}
25+
const normalizedHook = normalizeReturnObjectHook(rawHook)
26+
expect(normalizedHook(app)).toEqual({ foo: 'bar' })
27+
})
28+
})

0 commit comments

Comments
 (0)