-
-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathhooks.test.ts
More file actions
97 lines (89 loc) · 2.87 KB
/
hooks.test.ts
File metadata and controls
97 lines (89 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { defineCollection } from '../../src/utils'
import { resolveCollection } from '../../src/utils/collection'
import { parseContent } from '../utils/content'
import type { FileAfterParseHook, FileBeforeParseHook } from '../../src/types'
import { initiateValidatorsContext } from '../../src/utils/dependencies'
import type { Manifest } from '../../src/types/manifest'
describe('Hooks', async () => {
await initiateValidatorsContext()
const collection = resolveCollection('hookTest', defineCollection({
type: 'data',
source: 'content/**',
schema: z.object({
body: z.any(),
foo: z.any(),
}),
}))!
it('content:file:beforeParse', async () => {
let hookCtx: FileBeforeParseHook
const nuxtMock = {
callHook(hook: string, ctx: FileBeforeParseHook) {
if (hook === 'content:file:beforeParse') {
ctx.file.body = ctx.file.body.replace('replace-me', 'bar')
hookCtx = ctx
}
},
}
const content = await parseContent('content/index.md', `---
foo: 'replace-me'
---
# Hello World
`, collection, nuxtMock)
expect(hookCtx.file.id).toEqual('content/index.md')
expect(content.foo).toEqual('bar')
})
it('content:file:afterParse', async () => {
let hookCtx: FileAfterParseHook
const nuxtMock = {
callHook(hook: string, ctx: FileAfterParseHook) {
if (hook === 'content:file:afterParse') {
// augment
ctx.content.bar = 'foo'
hookCtx = ctx
}
},
}
const parsed = await parseContent('content/index.md', `---
foo: 'bar'
---
# Hello World
`, collection, nuxtMock)
expect(hookCtx.collection.name).toEqual('hookTest')
expect(parsed.id).toEqual('content/index.md')
expect(parsed.foo).toEqual('bar')
expect(parsed.bar).toEqual('foo')
})
it('content:manifest mutations are reflected in manifest', async () => {
const extraCollection = resolveCollection('injected', defineCollection({
type: 'data',
source: 'extra/**',
schema: z.object({
body: z.any(),
}),
}))!
const manifest: Manifest = {
checksumStructure: {},
checksum: {},
dump: {},
components: [],
collections: [collection],
}
// Simulate the module calling the hook
const nuxtMock = {
callHook(hook: string, ctx: Manifest) {
if (hook === 'content:manifest') {
ctx.collections.push(extraCollection)
}
},
}
nuxtMock.callHook('content:manifest', manifest)
// must be visible on original manifest object
expect(manifest.collections).toHaveLength(2)
// new collection is visible
expect(manifest.collections.find(c => c.name === 'injected')).toBeDefined()
// original collection still exists
expect(manifest.collections.find(c => c.name === 'hookTest')).toBeDefined()
})
})