Skip to content

Commit 8b59140

Browse files
authored
test: improve file embed & code fragment tests (#2617)
1 parent 47b0956 commit 8b59140

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

test/integration/example.test.js

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,9 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
142142
# Embed Test
143143
144144
[filename](_media/example1.js ':include :type=code :fragment=demo')
145-
146-
[filename](_media/example2.js ':include :type=code :fragment=demo')
147145
`,
148146
},
149147
routes: {
150-
// Serve the example.js file so the embed fetch can retrieve it
151148
'_media/example1.js': `
152149
let myURL = 'https://api.example.com/data';
153150
/// [demo]
@@ -159,21 +156,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
159156
console.log(JSON.stringify(myJson));
160157
});
161158
/// [demo]
162-
163-
result.then(console.log).catch(console.error);
164-
`,
165-
'_media/example2.js': `
166-
let myURL = 'https://api.example.com/data';
167-
### [demo]
168-
const result = fetch(myURL)
169-
.then(response => {
170-
return response.json();
171-
})
172-
.then(myJson => {
173-
console.log(JSON.stringify(myJson));
174-
});
175-
### [demo]
176-
177159
result.then(console.log).catch(console.error);
178160
`,
179161
},
@@ -183,11 +165,65 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
183165
expect(
184166
await waitForText('#main', 'console.log(JSON.stringify(myJson));'),
185167
).toBeTruthy();
186-
// Ensure the URL outside the fragment is NOT included in the embedded code
168+
187169
const mainText = document.querySelector('#main').textContent;
188170
expect(mainText).not.toContain('https://api.example.com/data');
189171
expect(mainText).not.toContain(
190172
'result.then(console.log).catch(console.error);',
191173
);
192174
});
175+
176+
test('embed multiple file code fragments', async () => {
177+
await docsifyInit({
178+
markdown: {
179+
homepage: `
180+
# Embed Test
181+
182+
[filename](_media/example1.js ':include :type=code :fragment=demo')
183+
184+
[filename](_media/example2.js ":include :type=code :fragment=something")
185+
186+
# Text between
187+
188+
[filename](_media/example3.js ':include :fragment=something_else_not_code')
189+
190+
# Text after
191+
`,
192+
},
193+
routes: {
194+
'_media/example1.js': `
195+
let example1 = 1;
196+
/// [demo]
197+
example1 += 10;
198+
/// [demo]
199+
console.log(example1);`,
200+
'_media/example2.js': `
201+
let example1 = 1;
202+
### [something]
203+
example2 += 10;
204+
### [something]
205+
console.log(example2);`,
206+
'_media/example3.js': `
207+
let example3 = 1;
208+
### [something_else_not_code]
209+
example3 += 10;
210+
/// [something_else_not_code]
211+
console.log(example3);`,
212+
},
213+
});
214+
215+
expect(await waitForText('#main', 'example1 += 10;')).toBeTruthy();
216+
expect(await waitForText('#main', 'example2 += 10;')).toBeTruthy();
217+
expect(await waitForText('#main', 'example3 += 10;')).toBeTruthy();
218+
219+
const mainText = document.querySelector('#main').textContent;
220+
expect(mainText).toContain('Text between');
221+
expect(mainText).toContain('Text after');
222+
expect(mainText).not.toContain('let example1 = 1;');
223+
expect(mainText).not.toContain('let example2 = 1;');
224+
expect(mainText).not.toContain('let example3 = 1;');
225+
expect(mainText).not.toContain('console.log(example1);');
226+
expect(mainText).not.toContain('console.log(example2);');
227+
expect(mainText).not.toContain('console.log(example3);');
228+
});
193229
});

0 commit comments

Comments
 (0)