summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/test/css-head-mdx.test.js
blob: f4ece38d971041d19e6b72980c2fc58c3047b0ea (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import mdx from '@astrojs/mdx';

import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';
import * as cheerio from 'cheerio';

describe('Head injection w/ MDX', () => {
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: new URL('./fixtures/css-head-mdx/', import.meta.url),
			integrations: [mdx()],
		});
	});

	describe('build', () => {
		before(async () => {
			await fixture.build();
		});

		it('only injects contents into head', async () => {
			const html = await fixture.readFile('/indexThree/index.html');
			const { document } = parseHTML(html);

			const links = document.querySelectorAll('head link[rel=stylesheet]');
			expect(links).to.have.a.lengthOf(1);

			const scripts = document.querySelectorAll('head script[type=module]');
			expect(scripts).to.have.a.lengthOf(1);
		});

		it('injects into the head for content collections', async () => {
			const html = await fixture.readFile('/posts/test/index.html');
			const { document } = parseHTML(html);

			const links = document.querySelectorAll('head link[rel=stylesheet]');
			expect(links).to.have.a.lengthOf(1);
		});

		it('injects content from a component using Content#render()', async () => {
			const html = await fixture.readFile('/DirectContentUsage/index.html');
			const { document } = parseHTML(html);

			const links = document.querySelectorAll('head link[rel=stylesheet]');
			expect(links).to.have.a.lengthOf(1);

			const scripts = document.querySelectorAll('head script[type=module]');
			expect(scripts).to.have.a.lengthOf(2);
		});

		it('Using component using slots.render() API', async () => {
			const html = await fixture.readFile('/remote/index.html');
			const { document } = parseHTML(html);

			const links = document.querySelectorAll('head link[rel=stylesheet]');
			expect(links).to.have.a.lengthOf(1);
		});

		it('Using component but no layout', async () => {
			const html = await fixture.readFile('/noLayoutWithComponent/index.html');
			// Using cheerio here because linkedom doesn't support head tag injection
			const $ = cheerio.load(html);

			const headLinks = $('head link[rel=stylesheet]');
			expect(headLinks).to.have.a.lengthOf(1);

			const bodyLinks = $('body link[rel=stylesheet]');
			expect(bodyLinks).to.have.a.lengthOf(0);
		});

		it('JSX component rendering Astro children within head buffering phase', async () => {
			const html = await fixture.readFile('/posts/using-component/index.html');
			// Using cheerio here because linkedom doesn't support head tag injection
			const $ = cheerio.load(html);

			const headLinks = $('head link[rel=stylesheet]');
			expect(headLinks).to.have.a.lengthOf(1);

			const bodyLinks = $('body link[rel=stylesheet]');
			expect(bodyLinks).to.have.a.lengthOf(0);
		});

		it('Injection caused by delayed slots', async () => {
			const html = await fixture.readFile('/componentwithtext/index.html');

			// Using cheerio here because linkedom doesn't support head tag injection
			const $ = cheerio.load(html);

			const headLinks = $('head link[rel=stylesheet]');
			expect(headLinks).to.have.a.lengthOf(1);

			const bodyLinks = $('body link[rel=stylesheet]');
			expect(bodyLinks).to.have.a.lengthOf(0);
		});
	});
});