From 487574e245374e18ae18dc8bf56fea751d0ed793 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 27 Oct 2025 22:21:51 +0100 Subject: [PATCH 1/2] fix: add test for file embed and code fragments --- test/integration/example.test.js | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/integration/example.test.js b/test/integration/example.test.js index 4f6b57378..9e9d34faa 100644 --- a/test/integration/example.test.js +++ b/test/integration/example.test.js @@ -134,4 +134,60 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { ).toBeTruthy(); expect(await waitForText('#main', 'This is a custom route')).toBeTruthy(); }); + + test('embed file code fragment renders', async () => { + await docsifyInit({ + markdown: { + homepage: ` + # Embed Test + + [filename](_media/example1.js ':include :type=code :fragment=demo') + + [filename](_media/example2.js ':include :type=code :fragment=demo') + `, + }, + routes: { + // Serve the example.js file so the embed fetch can retrieve it + '_media/example1.js': ` + let myURL = 'https://api.example.com/data'; + /// [demo] + const result = fetch(myURL) + .then(response => { + return response.json(); + }) + .then(myJson => { + console.log(JSON.stringify(myJson)); + }); + /// [demo] + + result.then(console.log).catch(console.error); + `, + '_media/example2.js': ` + let myURL = 'https://api.example.com/data'; + ### [demo] + const result = fetch(myURL) + .then(response => { + return response.json(); + }) + .then(myJson => { + console.log(JSON.stringify(myJson)); + }); + ### [demo] + + result.then(console.log).catch(console.error); + `, + }, + }); + + // Wait for the embedded fragment to be fetched and rendered into #main + expect( + await waitForText('#main', 'console.log(JSON.stringify(myJson));'), + ).toBeTruthy(); + // Ensure the URL outside the fragment is NOT included in the embedded code + const mainText = document.querySelector('#main').textContent; + expect(mainText).not.toContain('https://api.example.com/data'); + expect(mainText).not.toContain( + 'result.then(console.log).catch(console.error);', + ); + }); }); From c069f1932ad5d130bb287b9fcf39b72fbe22e96f Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 28 Oct 2025 21:33:19 +0100 Subject: [PATCH 2/2] test: improve file embed & code fragment tests --- test/integration/example.test.js | 74 ++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/test/integration/example.test.js b/test/integration/example.test.js index 9e9d34faa..c7e9ca4aa 100644 --- a/test/integration/example.test.js +++ b/test/integration/example.test.js @@ -142,12 +142,9 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { # Embed Test [filename](_media/example1.js ':include :type=code :fragment=demo') - - [filename](_media/example2.js ':include :type=code :fragment=demo') `, }, routes: { - // Serve the example.js file so the embed fetch can retrieve it '_media/example1.js': ` let myURL = 'https://api.example.com/data'; /// [demo] @@ -159,21 +156,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { console.log(JSON.stringify(myJson)); }); /// [demo] - - result.then(console.log).catch(console.error); - `, - '_media/example2.js': ` - let myURL = 'https://api.example.com/data'; - ### [demo] - const result = fetch(myURL) - .then(response => { - return response.json(); - }) - .then(myJson => { - console.log(JSON.stringify(myJson)); - }); - ### [demo] - result.then(console.log).catch(console.error); `, }, @@ -183,11 +165,65 @@ describe('Creating a Docsify site (integration tests in Jest)', function () { expect( await waitForText('#main', 'console.log(JSON.stringify(myJson));'), ).toBeTruthy(); - // Ensure the URL outside the fragment is NOT included in the embedded code + const mainText = document.querySelector('#main').textContent; expect(mainText).not.toContain('https://api.example.com/data'); expect(mainText).not.toContain( 'result.then(console.log).catch(console.error);', ); }); + + test('embed multiple file code fragments', async () => { + await docsifyInit({ + markdown: { + homepage: ` + # Embed Test + + [filename](_media/example1.js ':include :type=code :fragment=demo') + + [filename](_media/example2.js ":include :type=code :fragment=something") + + # Text between + + [filename](_media/example3.js ':include :fragment=something_else_not_code') + + # Text after + `, + }, + routes: { + '_media/example1.js': ` + let example1 = 1; + /// [demo] + example1 += 10; + /// [demo] + console.log(example1);`, + '_media/example2.js': ` + let example1 = 1; + ### [something] + example2 += 10; + ### [something] + console.log(example2);`, + '_media/example3.js': ` + let example3 = 1; + ### [something_else_not_code] + example3 += 10; + /// [something_else_not_code] + console.log(example3);`, + }, + }); + + expect(await waitForText('#main', 'example1 += 10;')).toBeTruthy(); + expect(await waitForText('#main', 'example2 += 10;')).toBeTruthy(); + expect(await waitForText('#main', 'example3 += 10;')).toBeTruthy(); + + const mainText = document.querySelector('#main').textContent; + expect(mainText).toContain('Text between'); + expect(mainText).toContain('Text after'); + expect(mainText).not.toContain('let example1 = 1;'); + expect(mainText).not.toContain('let example2 = 1;'); + expect(mainText).not.toContain('let example3 = 1;'); + expect(mainText).not.toContain('console.log(example1);'); + expect(mainText).not.toContain('console.log(example2);'); + expect(mainText).not.toContain('console.log(example3);'); + }); });