aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/test/mdx-frontmatter.test.js
blob: 3fcbf28e808f73aede843ba9cc2e99192ccf38d1 (plain) (blame)
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
import mdx from '@astrojs/mdx';

import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter/', import.meta.url);

describe('MDX frontmatter', () => {
	let fixture;
	before(async () => {
		fixture = await loadFixture({
			root: FIXTURE_ROOT,
			integrations: [mdx()],
		});
		await fixture.build();
	});
	it('builds when "frontmatter.property" is in JSX expression', async () => {
		assert.equal(true, true);
	});

	it('extracts frontmatter to "frontmatter" export', async () => {
		const { titles } = JSON.parse(await fixture.readFile('/glob.json'));
		assert.equal(titles.includes('Using YAML frontmatter'), true);
	});

	it('renders layout from "layout" frontmatter property', async () => {
		const html = await fixture.readFile('/index.html');
		const { document } = parseHTML(html);

		const layoutParagraph = document.querySelector('[data-layout-rendered]');

		assert.notEqual(layoutParagraph, null);
	});

	it('passes frontmatter to layout via "content" and "frontmatter" props', async () => {
		const html = await fixture.readFile('/index.html');
		const { document } = parseHTML(html);

		const contentTitle = document.querySelector('[data-content-title]');
		const frontmatterTitle = document.querySelector('[data-frontmatter-title]');

		assert.equal(contentTitle.textContent, 'Using YAML frontmatter');
		assert.equal(frontmatterTitle.textContent, 'Using YAML frontmatter');
	});

	it('passes headings to layout via "headings" prop', async () => {
		const html = await fixture.readFile('/with-headings/index.html');
		const { document } = parseHTML(html);

		const headingSlugs = [...document.querySelectorAll('[data-headings] > li')].map(
			(el) => el.textContent
		);

		assert.equal(headingSlugs.length > 0, true);
		assert.equal(headingSlugs.includes('section-1'), true);
		assert.equal(headingSlugs.includes('section-2'), true);
	});

	it('passes "file" and "url" to layout', async () => {
		const html = await fixture.readFile('/with-headings/index.html');
		const { document } = parseHTML(html);

		const frontmatterFile = document.querySelector('[data-frontmatter-file]')?.textContent;
		const frontmatterUrl = document.querySelector('[data-frontmatter-url]')?.textContent;
		const file = document.querySelector('[data-file]')?.textContent;
		const url = document.querySelector('[data-url]')?.textContent;

		assert.equal(
			frontmatterFile?.endsWith('with-headings.mdx'),
			true,
			'"file" prop does not end with correct path or is undefined'
		);
		assert.equal(frontmatterUrl, '/with-headings');
		assert.equal(file, frontmatterFile);
		assert.equal(url, frontmatterUrl);
	});
});