Skip to content

Commit a10310b

Browse files
committed
Add some test cases for custom ToC config
1 parent 15fce77 commit a10310b

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, test, vi } from 'vitest';
2+
import { routes } from '../../utils/routing';
3+
import { getRouteDataTestContext } from '../test-utils';
4+
import { generateRouteData } from '../../utils/routing/data';
5+
6+
vi.mock('astro:content', async () =>
7+
(await import('../test-utils')).mockedAstroContent({
8+
docs: [
9+
['index.mdx', { title: 'Home Page' }],
10+
['showcase.mdx', { title: 'ToC Disabled', tableOfContents: false }],
11+
[
12+
'environmental-impact.md',
13+
{ title: 'Explicit update date', tableOfContents: { minHeadingLevel: 2 } },
14+
],
15+
],
16+
})
17+
);
18+
19+
const headings = [
20+
{ depth: 1, slug: 'heading-1', text: 'Heading 1' },
21+
{ depth: 2, slug: 'heading-2', text: 'Heading 2' },
22+
{ depth: 3, slug: 'heading-3', text: 'Heading 3' },
23+
{ depth: 4, slug: 'heading-4', text: 'Heading 4' },
24+
];
25+
26+
describe('custom table of contents config', () => {
27+
test('table of contents heading levels match configuration', () => {
28+
const route = routes[0]!;
29+
const data = generateRouteData({
30+
props: { ...route, headings },
31+
context: getRouteDataTestContext(),
32+
});
33+
expect(data.toc?.minHeadingLevel).toBe(1);
34+
expect(data.toc?.maxHeadingLevel).toBe(4);
35+
});
36+
37+
test('table of contents can be disabled by frontmatter', () => {
38+
const route = routes[1]!;
39+
const data = generateRouteData({
40+
props: { ...route, headings },
41+
context: getRouteDataTestContext(),
42+
});
43+
expect(data.toc).toBeUndefined();
44+
});
45+
46+
test('table of contents heading levels can be customised by frontmatter', () => {
47+
const route = routes[2]!;
48+
const data = generateRouteData({
49+
props: { ...route, headings },
50+
context: getRouteDataTestContext(),
51+
});
52+
expect(data.toc?.minHeadingLevel).toBe(2);
53+
expect(data.toc?.maxHeadingLevel).toBe(3);
54+
});
55+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineVitestConfig } from '../test-config';
2+
3+
export default defineVitestConfig({
4+
title: 'custom ToC config',
5+
tableOfContents: {
6+
minHeadingLevel: 1,
7+
maxHeadingLevel: 4,
8+
},
9+
});

0 commit comments

Comments
 (0)