summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/test/propagated-assets.test.js
blob: a0768448f1d9190e8403c0edc8660e7d2cd0ca54 (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
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

describe('Markdoc - propagated assets', () => {
	let fixture;
	let devServer;
	before(async () => {
		fixture = await loadFixture({
			root: new URL('./fixtures/propagated-assets/', import.meta.url),
			// test suite was authored when inlineStylesheets defaulted to never
			build: { inlineStylesheets: 'never' },
		});
	});

	const modes = ['dev', 'prod'];

	for (const mode of modes) {
		describe(mode, () => {
			/** @type {Document} */
			let stylesDocument;
			/** @type {Document} */
			let scriptsDocument;

			before(async () => {
				if (mode === 'prod') {
					await fixture.build();
					stylesDocument = parseHTML(await fixture.readFile('/styles/index.html')).document;
					scriptsDocument = parseHTML(await fixture.readFile('/scripts/index.html')).document;
				} else if (mode === 'dev') {
					devServer = await fixture.startDevServer();
					const styleRes = await fixture.fetch('/styles');
					const scriptRes = await fixture.fetch('/scripts');
					stylesDocument = parseHTML(await styleRes.text()).document;
					scriptsDocument = parseHTML(await scriptRes.text()).document;
				}
			});

			after(async () => {
				if (mode === 'dev') devServer?.stop();
			});

			it('Bundles styles', async () => {
				let styleContents;
				if (mode === 'dev') {
					const styles = stylesDocument.querySelectorAll('style');
					assert.equal(styles.length, 1);
					styleContents = styles[0].textContent;
				} else {
					const links = stylesDocument.querySelectorAll('link[rel="stylesheet"]');
					assert.equal(links.length, 1);
					styleContents = await fixture.readFile(links[0].href);
				}
				assert.equal(styleContents.includes('--color-base-purple: 269, 79%;'), true);
			});

			it('[fails] Does not bleed styles to other page', async () => {
				if (mode === 'dev') {
					const styles = scriptsDocument.querySelectorAll('style');
					assert.equal(styles.length, 0);
				} else {
					const links = scriptsDocument.querySelectorAll('link[rel="stylesheet"]');
					assert.equal(links.length, 0);
				}
			});
		});
	}
});