summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/test/image-assets.test.js
blob: 793bf1be6de24176969ea8e01a9f2c767dcf2360 (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
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';

const root = new URL('./fixtures/image-assets/', import.meta.url);

describe('Markdoc - Image assets', () => {
	let baseFixture;

	before(async () => {
		baseFixture = await loadFixture({
			root,
		});
	});

	describe('dev', () => {
		let devServer;

		before(async () => {
			devServer = await baseFixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('uses public/ image paths unchanged', async () => {
			const res = await baseFixture.fetch('/');
			const html = await res.text();
			const { document } = parseHTML(html);
			assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
		});

		it('transforms relative image paths to optimized path', async () => {
			const res = await baseFixture.fetch('/');
			const html = await res.text();
			const { document } = parseHTML(html);
			assert.match(
				document.querySelector('#relative > img')?.src,
				/\/_image\?href=.*%2Fsrc%2Fassets%2Frelative%2Foar.jpg%3ForigWidth%3D420%26origHeight%3D630%26origFormat%3Djpg&w=420&h=630&f=webp/,
			);
		});

		it('transforms aliased image paths to optimized path', async () => {
			const res = await baseFixture.fetch('/');
			const html = await res.text();
			const { document } = parseHTML(html);
			assert.match(
				document.querySelector('#alias > img')?.src,
				/\/_image\?href=.*%2Fsrc%2Fassets%2Falias%2Fcityscape.jpg%3ForigWidth%3D420%26origHeight%3D280%26origFormat%3Djpg&w=420&h=280&f=webp/,
			);
		});

		it('passes images inside image tags to configured image component', async () => {
			const res = await baseFixture.fetch('/');
			const html = await res.text();
			const { document } = parseHTML(html);
			assert.equal(document.querySelector('#component > img')?.className, 'custom-styles');
		});
	});

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

		it('uses public/ image paths unchanged', async () => {
			const html = await baseFixture.readFile('/index.html');
			const { document } = parseHTML(html);
			assert.equal(document.querySelector('#public > img')?.src, '/favicon.svg');
		});

		it('transforms relative image paths to optimized path', async () => {
			const html = await baseFixture.readFile('/index.html');
			const { document } = parseHTML(html);
			assert.match(document.querySelector('#relative > img')?.src, /^\/_astro\/oar.*\.webp$/);
		});

		it('transforms aliased image paths to optimized path', async () => {
			const html = await baseFixture.readFile('/index.html');
			const { document } = parseHTML(html);
			assert.match(document.querySelector('#alias > img')?.src, /^\/_astro\/cityscape.*\.webp$/);
		});

		it('passes images inside image tags to configured image component', async () => {
			const html = await baseFixture.readFile('/index.html');
			const { document } = parseHTML(html);
			assert.equal(document.querySelector('#component > img')?.className, 'custom-styles');
			assert.match(document.querySelector('#component > img')?.src, /^\/_astro\/oar.*\.webp$/);
		});
	});
});