summaryrefslogtreecommitdiff
path: root/packages/markdown/component/test/astro-markdown-shiki.test.js
blob: fb4f8f9622a21cd0a3f364ddee840a252c9edc6f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Astro Markdown Shiki', () => {
	describe('Render shiki', () => {
		let fixture;

		before(async () => {
			fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/normal/' });
			await fixture.build();
		});

		it('Can render Astro <Markdown> with shiki', async () => {
			const html = await fixture.readFile('/astro/index.html');
			const $ = cheerio.load(html);

			// There should be no HTML from Prism
			expect($('.token')).to.have.lengthOf(0);

			expect($('pre')).to.have.lengthOf(2);

			expect($('span.line')).to.have.lengthOf(2);
			expect($('span.line').get(0).children).to.have.lengthOf(1);
			expect($('span.line').get(1).children).to.have.lengthOf(5);
		});
	});

	describe('Themes', () => {
		describe('Integrated theme', async () => {
			let fixture;

			before(async () => {
				fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/themes-integrated/' });
				await fixture.build();
			});

			it('<Markdown /> component', async () => {
				const html = await fixture.readFile('/astro/index.html');
				const $ = cheerio.load(html);

				expect($('pre')).to.have.lengthOf(1);
				expect($('pre').hasClass('astro-code')).to.equal(true);
				expect($('pre').attr().style).to.equal('background-color: #ffffff; overflow-x: auto;');
			});
		});

		describe('Custom theme', async () => {
			let fixture;

			before(async () => {
				fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/themes-custom/' });
				await fixture.build();
			});

			it('<Markdown /> component', async () => {
				const html = await fixture.readFile('/astro/index.html');
				const $ = cheerio.load(html);

				expect($('pre')).to.have.lengthOf(1);
				expect($('pre').hasClass('astro-code')).to.equal(true);
				expect($('pre').attr().style).to.equal('background-color: #FDFDFE; overflow-x: auto;');
			});
		});
	});

	describe('Custom langs', () => {
		let fixture;

		before(async () => {
			fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/langs/' });
			await fixture.build();
		});

		it('<Markdown /> component', async () => {
			const html = await fixture.readFile('/astro/index.html');
			const $ = cheerio.load(html);

			const segments = $('.line').get(6).children;
			expect(segments).to.have.lengthOf(3);
			expect(segments[0].attribs.style).to.be.equal('color: #C9D1D9');
			expect(segments[1].attribs.style).to.be.equal('color: #79C0FF');

			const unknownLang = $('.line').last().html();
			expect(unknownLang).to.be.equal(
				'<span style="color: #c9d1d9">This language does not exist</span>'
			);
		});
	});

	describe('Wrap', () => {
		describe('wrap = true', () => {
			const style =
				'background-color: #0d1117; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;';
			let fixture;

			before(async () => {
				fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-true/' });
				await fixture.build();
			});

			it('<Markdown /> component', async () => {
				const html = await fixture.readFile('/astro/index.html');
				const $ = cheerio.load(html);

				expect($('pre').get(0).attribs.style).to.equal(style);
				expect($('pre').get(1).attribs.style).to.equal(style);
			});
		});
	});

	describe('wrap = false', () => {
		const style = 'background-color: #0d1117; overflow-x: auto;';
		let fixture;

		before(async () => {
			fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-false/' });
			await fixture.build();
		});

		it('<Markdown /> component', async () => {
			const html = await fixture.readFile('/astro/index.html');
			const $ = cheerio.load(html);

			expect($('pre').get(0).attribs.style).to.equal(style);
			expect($('pre').get(1).attribs.style).to.equal(style);
		});
	});

	describe('wrap = null', () => {
		const style = 'background-color: #0d1117';
		let fixture;

		before(async () => {
			fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-null/' });
			await fixture.build();
		});

		it('<Markdown /> component', async () => {
			const html = await fixture.readFile('/astro/index.html');
			const $ = cheerio.load(html);

			expect($('pre').get(0).attribs.style).to.equal(style);
			expect($('pre').get(1).attribs.style).to.equal(style);
		});
	});
});