-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
test-utils.ts
75 lines (70 loc) 路 2.06 KB
/
test-utils.ts
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
import { z } from 'astro/zod';
import { docsSchema, i18nSchema } from '../schema';
import type { StarlightDocsEntry } from '../utils/routing';
import { vi } from 'vitest';
const frontmatterSchema = docsSchema()({
image: () =>
z.object({
src: z.string(),
width: z.number(),
height: z.number(),
format: z.union([
z.literal('png'),
z.literal('jpg'),
z.literal('jpeg'),
z.literal('tiff'),
z.literal('webp'),
z.literal('gif'),
z.literal('svg'),
z.literal('avif'),
]),
}),
});
function mockDoc(
id: StarlightDocsEntry['id'],
data: z.input<typeof frontmatterSchema>,
body = ''
): StarlightDocsEntry {
return {
id,
slug: id.replace(/\.[^\.]+$/, '').replace(/\/index$/, ''),
body,
collection: 'docs',
data: frontmatterSchema.parse(data),
render: (() => {}) as StarlightDocsEntry['render'],
};
}
function mockDict(id: string, data: z.input<ReturnType<typeof i18nSchema>>) {
return { id, data: i18nSchema().parse(data) };
}
export async function mockedAstroContent({
docs = [],
i18n = [],
}: {
docs?: Parameters<typeof mockDoc>[];
i18n?: Parameters<typeof mockDict>[];
}) {
const mod = await vi.importActual<typeof import('astro:content')>('astro:content');
const mockDocs = docs.map((doc) => mockDoc(...doc));
const mockDicts = i18n.map((dict) => mockDict(...dict));
return {
...mod,
getCollection: (
collection: 'docs' | 'i18n',
filter?: (entry: ReturnType<typeof mockDoc> | ReturnType<typeof mockDict>) => unknown
) => {
const entries = collection === 'i18n' ? mockDicts : mockDocs;
return filter ? entries.filter(filter) : entries;
},
};
}
export async function mockedCollectionConfig(docsUserSchema?: Parameters<typeof docsSchema>[0]) {
const content = await vi.importActual<typeof import('astro:content')>('astro:content');
const schemas = await vi.importActual<typeof import('../schema')>('../schema');
return {
collections: {
docs: content.defineCollection({ schema: schemas.docsSchema(docsUserSchema) }),
i18n: content.defineCollection({ type: 'data', schema: schemas.i18nSchema() }),
},
};
}