forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro-slots.test.js
184 lines (144 loc) · 5.82 KB
/
astro-slots.test.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Slots', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ root: './fixtures/astro-slots/' });
await fixture.build();
});
it('Basic named slots work', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').text().trim(), 'A');
assert.equal($('#b').text().trim(), 'B');
assert.equal($('#c').text().trim(), 'C');
assert.equal($('#default').text().trim(), 'Default');
});
it('Dynamic named slots work', async () => {
const html = await fixture.readFile('/dynamic/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').text().trim(), 'A');
assert.equal($('#b').text().trim(), 'B');
assert.equal($('#c').text().trim(), 'C');
assert.equal($('#default').text().trim(), 'Default');
});
it('Conditional named slots work', async () => {
const html = await fixture.readFile('/conditional/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').text().trim(), 'A');
assert.equal($('#b').text().trim(), 'B');
assert.equal($('#c').text().trim(), 'C');
assert.equal($('#default').text().trim(), 'Default');
});
it('Slots of a component render fallback content by default', async () => {
const html = await fixture.readFile('/fallback/index.html');
const $ = cheerio.load(html);
assert.equal($('#default').length, 1);
});
it('Slots of a page render fallback content', async () => {
const html = await fixture.readFile('/fallback-own/index.html');
const $ = cheerio.load(html);
assert.equal($('#default').length, 1);
});
it('Slots override fallback content', async () => {
const html = await fixture.readFile('/fallback-override/index.html');
const $ = cheerio.load(html);
assert.equal($('#override').length, 1);
assert.equal($('#fallback-2').text(), 'Slotty slot.');
});
it('Slots work with multiple elements', async () => {
const html = await fixture.readFile('/multiple/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').text().trim(), 'ABC');
});
it('Slots work on Components', async () => {
const html = await fixture.readFile('/component/index.html');
const $ = cheerio.load(html);
// test 1: #a renders
assert.equal($('#a').length, 1);
// test 2: Slotted component into #a
assert.equal($('#a').children('astro-component').length, 1);
// test 3: Slotted component into default slot
assert.equal($('#default').children('astro-component').length, 1);
});
describe('Slots API work on Components', () => {
it('IDs will exist whether the slots are filled or not', async () => {
const html = await fixture.readFile('/slottedapi-default/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').length, 1);
assert.equal($('#b').length, 1);
assert.equal($('#c').length, 1);
assert.equal($('#default').length, 1);
});
it('IDs will not exist because the slots are not filled', async () => {
const html = await fixture.readFile('/slottedapi-empty/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').length, 0);
assert.equal($('#b').length, 0);
assert.equal($('#c').length, 0);
assert.equal($('#default').length, 0);
});
it('IDs will exist because the slots are filled', async () => {
const html = await fixture.readFile('/slottedapi-filled/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').length, 1);
assert.equal($('#b').length, 1);
assert.equal($('#c').length, 1);
assert.equal($('#default').length, 0); // the default slot is not filled
});
it('Default ID will exist because the default slot is filled', async () => {
const html = await fixture.readFile('/slottedapi-default-filled/index.html');
const $ = cheerio.load(html);
assert.equal($('#a').length, 0);
assert.equal($('#b').length, 0);
assert.equal($('#c').length, 0);
assert.equal($('#default').length, 1); // the default slot is not filled
});
});
it('Slots.render() API', async () => {
// Simple imperative slot render
{
const html = await fixture.readFile('/slottedapi-render/index.html');
const $ = cheerio.load(html);
assert.equal($('#render').length, 1);
assert.equal($('#render').text(), 'render');
}
// Child function render without args
{
const html = await fixture.readFile('/slottedapi-render/index.html');
const $ = cheerio.load(html);
assert.equal($('#render-fn').length, 1);
assert.equal($('#render-fn').text(), 'render-fn');
}
// Child function render with args
{
const html = await fixture.readFile('/slottedapi-render/index.html');
const $ = cheerio.load(html);
assert.equal($('#render-args').length, 1);
assert.equal($('#render-args span').length, 1);
assert.equal($('#render-args').text(), 'render-args');
}
{
const html = await fixture.readFile('/rendered-multiple-times/index.html');
const $ = cheerio.load(html);
const elements = $('div');
assert.equal(elements.length, 10);
const [first, second, third] = elements;
assert.notEqual(first.children[0].data, second.children[0].data);
assert.notEqual(second.children[0].data, third.children[0].data);
assert.notEqual(third.children[0].data, first.children[0].data);
}
});
it('Arguments can be passed to named slots with Astro.slots.render()', async () => {
const html = await fixture.readFile('/slotted-named-functions/index.html');
const $ = cheerio.load(html);
const beforeDiv = $('div#before');
const [beforeChildren] = beforeDiv.children('div');
assert.deepEqual(beforeChildren.firstChild.data, 'Test Content BEFORE');
const afterDiv = $('div#after');
const [afterChildren] = afterDiv.children('div');
assert.deepEqual(afterChildren.firstChild.data, 'Test Content AFTER');
});
});