From 3387f75c992ca1444fc5e45cd0f9f4d2054fd5fd Mon Sep 17 00:00:00 2001 From: Atharva Date: Tue, 13 Feb 2024 20:12:55 +0530 Subject: chore: Migrate `content-collection-**.test.js` to `node:test` (#10098) * test: move to node:test * test: move content-collections-render to node:test * chore: bring back isWindows --------- Co-authored-by: Emanuele Stoppa --- .../test/content-collection-references.nodetest.js | 161 ++++++++++++ .../test/content-collection-references.test.js | 158 ------------ .../test/content-collections-render.nodetest.js | 287 +++++++++++++++++++++ .../astro/test/content-collections-render.test.js | 275 -------------------- 4 files changed, 448 insertions(+), 433 deletions(-) create mode 100644 packages/astro/test/content-collection-references.nodetest.js delete mode 100644 packages/astro/test/content-collection-references.test.js create mode 100644 packages/astro/test/content-collections-render.nodetest.js delete mode 100644 packages/astro/test/content-collections-render.test.js diff --git a/packages/astro/test/content-collection-references.nodetest.js b/packages/astro/test/content-collection-references.nodetest.js new file mode 100644 index 000000000..bf31f5dc4 --- /dev/null +++ b/packages/astro/test/content-collection-references.nodetest.js @@ -0,0 +1,161 @@ +import * as assert from 'node:assert/strict'; +import { after, describe, before, it } from 'node:test'; +import * as cheerio from 'cheerio'; +import { fixLineEndings, loadFixture } from './test-utils.js'; + +describe('Content Collections - references', () => { + let fixture; + let devServer; + before(async () => { + fixture = await loadFixture({ root: './fixtures/content-collection-references/' }); + }); + + const modes = ['dev', 'prod']; + + for (const mode of modes) { + describe(mode, () => { + before(async () => { + if (mode === 'prod') { + await fixture.build(); + } else if (mode === 'dev') { + devServer = await fixture.startDevServer(); + } + }); + + after(async () => { + if (mode === 'dev') devServer?.stop(); + }); + + describe(`JSON result`, () => { + let json; + before(async () => { + if (mode === 'prod') { + const rawJson = await fixture.readFile('/welcome-data.json'); + json = JSON.parse(rawJson); + } else if (mode === 'dev') { + const rawJsonResponse = await fixture.fetch('/welcome-data.json'); + const rawJson = await rawJsonResponse.text(); + json = JSON.parse(rawJson); + } + }); + + it('Returns expected keys', () => { + assert.ok(json.hasOwnProperty('welcomePost')); + assert.ok(json.hasOwnProperty('banner')); + assert.ok(json.hasOwnProperty('author')); + assert.ok(json.hasOwnProperty('relatedPosts')); + }); + + it('Returns `banner` data', () => { + const { banner } = json; + assert.ok(banner.hasOwnProperty('data')); + assert.equal(banner.id, 'welcome'); + assert.equal(banner.collection, 'banners'); + assert.equal( + banner.data.alt, + 'Futuristic landscape with chrome buildings and blue skies' + ); + + assert.equal(banner.data.src.width, 400); + assert.equal(banner.data.src.height, 225); + assert.equal(banner.data.src.format, 'jpg'); + assert.equal(banner.data.src.src.includes('the-future'), true); + }); + + it('Returns `author` data', () => { + const { author } = json; + assert.ok(author.hasOwnProperty('data')); + assert.deepEqual(author, { + id: 'nate-moore', + collection: 'authors', + data: { + name: 'Nate Something Moore', + twitter: 'https://twitter.com/n_moore', + }, + }); + }); + + it('Returns `relatedPosts` data', () => { + const { relatedPosts } = json; + assert.equal(Array.isArray(relatedPosts), true, 'Expected relatedPosts to be an array'); + const topLevelInfo = relatedPosts.map(({ data, body, ...meta }) => ({ + ...meta, + body: fixLineEndings(body).trim(), + })); + assert.deepEqual(topLevelInfo, [ + { + id: 'related-1.md', + slug: 'related-1', + body: '# Related post 1\n\nThis is related to the welcome post.', + collection: 'blog', + }, + { + id: 'related-2.md', + slug: 'related-2', + body: '# Related post 2\n\nThis is related to the welcome post.', + collection: 'blog', + }, + ]); + const postData = relatedPosts.map(({ data }) => data); + assert.deepEqual(postData, [ + { + title: 'Related post 1', + banner: { id: 'welcome', collection: 'banners' }, + author: { id: 'fred-schott', collection: 'authors' }, + }, + { + title: 'Related post 2', + banner: { id: 'welcome', collection: 'banners' }, + author: { id: 'ben-holmes', collection: 'authors' }, + }, + ]); + }); + }); + + describe(`Render result`, () => { + let $; + before(async () => { + if (mode === 'prod') { + const html = await fixture.readFile('/welcome/index.html'); + $ = cheerio.load(html); + } else if (mode === 'dev') { + const htmlResponse = await fixture.fetch('/welcome'); + const html = await htmlResponse.text(); + $ = cheerio.load(html); + } + }); + + it('Renders `banner` data', () => { + const banner = $('img[data-banner]'); + assert.equal(banner.length, 1); + assert.ok(banner.attr('src').includes('the-future')); + assert.equal( + banner.attr('alt'), + 'Futuristic landscape with chrome buildings and blue skies' + ); + assert.equal(banner.attr('width'), '400'); + assert.equal(banner.attr('height'), '225'); + }); + + it('Renders `author` data', () => { + const author = $('a[data-author-name]'); + assert.equal(author.length, 1); + assert.equal(author.attr('href'), 'https://twitter.com/n_moore'); + assert.equal(author.text(), 'Nate Something Moore'); + }); + + it('Renders `relatedPosts` data', () => { + const relatedPosts = $('ul[data-related-posts]'); + assert.equal(relatedPosts.length, 1); + const relatedPost1 = relatedPosts.find('li').eq(0); + + assert.equal(relatedPost1.find('a').attr('href'), '/blog/related-1'); + assert.equal(relatedPost1.find('a').text(), 'Related post 1'); + const relatedPost2 = relatedPosts.find('li').eq(1); + assert.equal(relatedPost2.find('a').attr('href'), '/blog/related-2'); + assert.equal(relatedPost2.find('a').text(), 'Related post 2'); + }); + }); + }); + } +}); diff --git a/packages/astro/test/content-collection-references.test.js b/packages/astro/test/content-collection-references.test.js deleted file mode 100644 index da1548621..000000000 --- a/packages/astro/test/content-collection-references.test.js +++ /dev/null @@ -1,158 +0,0 @@ -import { expect } from 'chai'; -import * as cheerio from 'cheerio'; -import { fixLineEndings, loadFixture } from './test-utils.js'; - -describe('Content Collections - references', () => { - let fixture; - let devServer; - before(async () => { - fixture = await loadFixture({ root: './fixtures/content-collection-references/' }); - }); - - const modes = ['dev', 'prod']; - - for (const mode of modes) { - describe(mode, () => { - before(async () => { - if (mode === 'prod') { - await fixture.build(); - } else if (mode === 'dev') { - devServer = await fixture.startDevServer(); - } - }); - - after(async () => { - if (mode === 'dev') devServer?.stop(); - }); - - describe(`JSON result`, () => { - let json; - before(async () => { - if (mode === 'prod') { - const rawJson = await fixture.readFile('/welcome-data.json'); - json = JSON.parse(rawJson); - } else if (mode === 'dev') { - const rawJsonResponse = await fixture.fetch('/welcome-data.json'); - const rawJson = await rawJsonResponse.text(); - json = JSON.parse(rawJson); - } - }); - - it('Returns expected keys', () => { - expect(json).to.haveOwnProperty('welcomePost'); - expect(json).to.haveOwnProperty('banner'); - expect(json).to.haveOwnProperty('author'); - expect(json).to.haveOwnProperty('relatedPosts'); - }); - - it('Returns `banner` data', () => { - const { banner } = json; - expect(banner).to.haveOwnProperty('data'); - expect(banner.id).to.equal('welcome'); - expect(banner.collection).to.equal('banners'); - expect(banner.data.alt).to.equal( - 'Futuristic landscape with chrome buildings and blue skies' - ); - - expect(banner.data.src.width).to.equal(400); - expect(banner.data.src.height).to.equal(225); - expect(banner.data.src.format).to.equal('jpg'); - expect(banner.data.src.src.includes('the-future')).to.be.true; - }); - - it('Returns `author` data', () => { - const { author } = json; - expect(author).to.haveOwnProperty('data'); - expect(author).to.deep.equal({ - id: 'nate-moore', - collection: 'authors', - data: { - name: 'Nate Something Moore', - twitter: 'https://twitter.com/n_moore', - }, - }); - }); - - it('Returns `relatedPosts` data', () => { - const { relatedPosts } = json; - expect(Array.isArray(relatedPosts)).to.be.true; - const topLevelInfo = relatedPosts.map(({ data, body, ...meta }) => ({ - ...meta, - body: fixLineEndings(body).trim(), - })); - expect(topLevelInfo).to.deep.equal([ - { - id: 'related-1.md', - slug: 'related-1', - body: '# Related post 1\n\nThis is related to the welcome post.', - collection: 'blog', - }, - { - id: 'related-2.md', - slug: 'related-2', - body: '# Related post 2\n\nThis is related to the welcome post.', - collection: 'blog', - }, - ]); - const postData = relatedPosts.map(({ data }) => data); - expect(postData).to.deep.equal([ - { - title: 'Related post 1', - banner: { id: 'welcome', collection: 'banners' }, - author: { id: 'fred-schott', collection: 'authors' }, - }, - { - title: 'Related post 2', - banner: { id: 'welcome', collection: 'banners' }, - author: { id: 'ben-holmes', collection: 'authors' }, - }, - ]); - }); - }); - - describe(`Render result`, () => { - let $; - before(async () => { - if (mode === 'prod') { - const html = await fixture.readFile('/welcome/index.html'); - $ = cheerio.load(html); - } else if (mode === 'dev') { - const htmlResponse = await fixture.fetch('/welcome'); - const html = await htmlResponse.text(); - $ = cheerio.load(html); - } - }); - - it('Renders `banner` data', () => { - const banner = $('img[data-banner]'); - expect(banner.length).to.equal(1); - expect(banner.attr('src')).to.include('the-future'); - expect(banner.attr('alt')).to.equal( - 'Futuristic landscape with chrome buildings and blue skies' - ); - expect(banner.attr('width')).to.equal('400'); - expect(banner.attr('height')).to.equal('225'); - }); - - it('Renders `author` data', () => { - const author = $('a[data-author-name]'); - expect(author.length).to.equal(1); - expect(author.attr('href')).to.equal('https://twitter.com/n_moore'); - expect(author.text()).to.equal('Nate Something Moore'); - }); - - it('Renders `relatedPosts` data', () => { - const relatedPosts = $('ul[data-related-posts]'); - expect(relatedPosts.length).to.equal(1); - const relatedPost1 = relatedPosts.find('li').eq(0); - - expect(relatedPost1.find('a').attr('href')).to.equal('/blog/related-1'); - expect(relatedPost1.find('a').text()).to.equal('Related post 1'); - const relatedPost2 = relatedPosts.find('li').eq(1); - expect(relatedPost2.find('a').attr('href')).to.equal('/blog/related-2'); - expect(relatedPost2.find('a').text()).to.equal('Related post 2'); - }); - }); - }); - } -}); diff --git a/packages/astro/test/content-collections-render.nodetest.js b/packages/astro/test/content-collections-render.nodetest.js new file mode 100644 index 000000000..9fbd78350 --- /dev/null +++ b/packages/astro/test/content-collections-render.nodetest.js @@ -0,0 +1,287 @@ +import * as assert from 'node:assert/strict'; +import { after, describe, before, it } from 'node:test'; +import * as cheerio from 'cheerio'; +import { loadFixture, isWindows } from './test-utils.js'; +import testAdapter from './test-adapter.js'; + +if(!isWindows) { + describe() +} + +describe('Content Collections - render()', () => { + describe('Build - SSG', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/content/', + // test suite was authored when inlineStylesheets defaulted to never + build: { inlineStylesheets: 'never' }, + }); + await fixture.build(); + }); + + it('Includes CSS for rendered entry', async () => { + const html = await fixture.readFile('/launch-week/index.html'); + const $ = cheerio.load(html); + + // Renders content + assert.equal($('ul li').length, 3); + + // Includes styles + assert.equal($('link[rel=stylesheet]').length, 1); + }); + + it('Excludes CSS for non-rendered entries', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + // Excludes styles + assert.equal($('link[rel=stylesheet]').length, 0); + }); + + it('De-duplicates CSS used both in layout and directly in target page', async () => { + const html = await fixture.readFile('/with-layout-prop/index.html'); + const $ = cheerio.load(html); + + const set = new Set(); + + $('link[rel=stylesheet]').each((_, linkEl) => { + const href = linkEl.attribs.href; + assert.equal(set.has(href), false); + set.add(href); + }); + + $('style').each((_, styleEl) => { + const textContent = styleEl.children[0].data; + assert.equal(set.has(textContent), false); + set.add(textContent); + }); + }); + + it('Includes component scripts for rendered entry', async () => { + const html = await fixture.readFile('/launch-week-component-scripts/index.html'); + const $ = cheerio.load(html); + + const allScripts = $('head > script[type="module"]'); + assert.ok(allScripts.length); + + // Includes hoisted script + const scriptWithSrc = [...allScripts].find((script) => + $(script).attr('src')?.includes('WithScripts') + ); + assert.notEqual( + scriptWithSrc, + undefined, + '`WithScripts.astro` hoisted script missing from head.' + ); + + // Includes inline script + assert.equal($('script[data-is-inline]').length, 1); + }); + + it('Excludes component scripts for non-rendered entries', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + const allScripts = $('head > script[type="module"]'); + + // Excludes hoisted script + const scriptWithText = [...allScripts].find((script) => + $(script).text().includes('document.querySelector("#update-me")') + ); + assert.equal( + scriptWithText, + undefined, + '`WithScripts.astro` hoisted script included unexpectedly.' + ); + }); + + it('Applies MDX components export', async () => { + const html = await fixture.readFile('/launch-week-components-export/index.html'); + const $ = cheerio.load(html); + + const h2 = $('h2'); + assert.equal(h2.length, 1); + assert.equal(h2.attr('data-components-export-applied'), 'true'); + }); + }); + + describe('Build - SSR', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + output: 'server', + root: './fixtures/content/', + adapter: testAdapter(), + // test suite was authored when inlineStylesheets defaulted to never + build: { inlineStylesheets: 'never' }, + }); + await fixture.build(); + }); + + it('Includes CSS for rendered entry', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/launch-week'); + const response = await app.render(request); + const html = await response.text(); + const $ = cheerio.load(html); + + // Renders content + assert.equal($('ul li').length, 3); + + // Includes styles + assert.equal($('link[rel=stylesheet]').length, 1); + }); + + it('Exclude CSS for non-rendered entries', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/'); + const response = await app.render(request); + const html = await response.text(); + const $ = cheerio.load(html); + + // Includes styles + assert.equal($('link[rel=stylesheet]').length, 0); + }); + + it('De-duplicates CSS used both in layout and directly in target page', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/with-layout-prop/'); + const response = await app.render(request); + const html = await response.text(); + const $ = cheerio.load(html); + + const set = new Set(); + + $('link[rel=stylesheet]').each((_, linkEl) => { + const href = linkEl.attribs.href; + assert.equal(set.has(href), false); + set.add(href); + }); + + $('style').each((_, styleEl) => { + const textContent = styleEl.children[0].data; + assert.equal(set.has(textContent), false); + set.add(textContent); + }); + }); + + it('Applies MDX components export', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/launch-week-components-export'); + const response = await app.render(request); + const html = await response.text(); + const $ = cheerio.load(html); + + const h2 = $('h2'); + assert.equal(h2.length, 1); + assert.equal(h2.attr('data-components-export-applied'), 'true'); + }); + + it('getCollection should return new instances of the array to be mutated safely', async () => { + const app = await fixture.loadTestAdapterApp(); + + let request = new Request('http://example.com/sort-blog-collection'); + let response = await app.render(request); + let html = await response.text(); + let $ = cheerio.load(html); + assert.equal($('li').first().text(), 'With Layout Prop'); + + request = new Request('http://example.com/'); + response = await app.render(request); + html = await response.text(); + $ = cheerio.load(html); + assert.equal($('li').first().text(), 'Hello world'); + }); + }); + + describe('Dev - SSG', () => { + let devServer; + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/content/', + }); + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('Includes CSS for rendered entry', async () => { + const response = await fixture.fetch('/launch-week', { method: 'GET' }); + assert.equal(response.status, 200); + + const html = await response.text(); + const $ = cheerio.load(html); + + // Renders content + assert.equal($('ul li').length, 3); + + // Includes styles + assert.equal($('head > style').length, 1); + assert.ok($('head > style').text().includes("font-family: 'Comic Sans MS'")); + }); + + it('Includes component scripts for rendered entry', async () => { + const response = await fixture.fetch('/launch-week-component-scripts', { method: 'GET' }); + assert.equal(response.status, 200); + + const html = await response.text(); + const $ = cheerio.load(html); + + const allScripts = $('head > script[src]'); + assert.ok(allScripts.length); + // Includes hoisted script + const scriptWithSrc = [...allScripts].find((script) => + script.attribs.src.includes('WithScripts.astro') + ); + assert.notEqual( + scriptWithSrc, + undefined, + '`WithScripts.astro` hoisted script missing from head.' + ); + + // Includes inline script + assert.equal($('script[data-is-inline]').length, 1); + }); + + it('Applies MDX components export', async () => { + const response = await fixture.fetch('/launch-week-components-export', { method: 'GET' }); + assert.equal(response.status, 200); + + const html = await response.text(); + const $ = cheerio.load(html); + + const h2 = $('h2'); + assert.equal(h2.length, 1); + assert.equal(h2.attr('data-components-export-applied'), 'true'); + }); + + it('Supports layout prop with recursive getCollection() call', async () => { + const response = await fixture.fetch('/with-layout-prop', { method: 'GET' }); + assert.equal(response.status, 200); + + const html = await response.text(); + const $ = cheerio.load(html); + + const body = $('body'); + assert.equal(body.attr('data-layout-prop'), 'true'); + + const h1 = $('h1'); + assert.equal(h1.length, 1); + assert.equal(h1.text(), 'With Layout Prop'); + + const h2 = $('h2'); + assert.equal(h2.length, 1); + assert.equal(h2.text(), 'Content with a layout prop'); + }); + }); +}); diff --git a/packages/astro/test/content-collections-render.test.js b/packages/astro/test/content-collections-render.test.js deleted file mode 100644 index 27eb33b5a..000000000 --- a/packages/astro/test/content-collections-render.test.js +++ /dev/null @@ -1,275 +0,0 @@ -import { expect } from 'chai'; -import * as cheerio from 'cheerio'; -import { loadFixture, isWindows } from './test-utils.js'; -import testAdapter from './test-adapter.js'; - -const describe = isWindows ? global.describe.skip : global.describe; - -describe('Content Collections - render()', () => { - describe('Build - SSG', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - - before(async () => { - fixture = await loadFixture({ - root: './fixtures/content/', - // test suite was authored when inlineStylesheets defaulted to never - build: { inlineStylesheets: 'never' }, - }); - await fixture.build(); - }); - - it('Includes CSS for rendered entry', async () => { - const html = await fixture.readFile('/launch-week/index.html'); - const $ = cheerio.load(html); - - // Renders content - expect($('ul li')).to.have.a.lengthOf(3); - - // Includes styles - expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1); - }); - - it('Excludes CSS for non-rendered entries', async () => { - const html = await fixture.readFile('/index.html'); - const $ = cheerio.load(html); - - // Excludes styles - expect($('link[rel=stylesheet]')).to.have.a.lengthOf(0); - }); - - it('De-duplicates CSS used both in layout and directly in target page', async () => { - const html = await fixture.readFile('/with-layout-prop/index.html'); - const $ = cheerio.load(html); - - const set = new Set(); - - $('link[rel=stylesheet]').each((_, linkEl) => { - const href = linkEl.attribs.href; - expect(set).to.not.contain(href); - set.add(href); - }); - - $('style').each((_, styleEl) => { - const textContent = styleEl.children[0].data; - expect(set).to.not.contain(textContent); - set.add(textContent); - }); - }); - - it('Includes component scripts for rendered entry', async () => { - const html = await fixture.readFile('/launch-week-component-scripts/index.html'); - const $ = cheerio.load(html); - - const allScripts = $('head > script[type="module"]'); - expect(allScripts).to.have.length; - - // Includes hoisted script - expect( - [...allScripts].find((script) => $(script).attr('src')?.includes('WithScripts')), - '`WithScripts.astro` hoisted script missing from head.' - ).to.not.be.undefined; - - // Includes inline script - expect($('script[data-is-inline]')).to.have.a.lengthOf(1); - }); - - it('Excludes component scripts for non-rendered entries', async () => { - const html = await fixture.readFile('/index.html'); - const $ = cheerio.load(html); - - const allScripts = $('head > script[type="module"]'); - - // Excludes hoisted script - expect( - [...allScripts].find((script) => - $(script).text().includes('document.querySelector("#update-me")') - ), - '`WithScripts.astro` hoisted script included unexpectedly.' - ).to.be.undefined; - }); - - it('Applies MDX components export', async () => { - const html = await fixture.readFile('/launch-week-components-export/index.html'); - const $ = cheerio.load(html); - - const h2 = $('h2'); - expect(h2).to.have.a.lengthOf(1); - expect(h2.attr('data-components-export-applied')).to.equal('true'); - }); - }); - - describe('Build - SSR', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - - before(async () => { - fixture = await loadFixture({ - output: 'server', - root: './fixtures/content/', - adapter: testAdapter(), - // test suite was authored when inlineStylesheets defaulted to never - build: { inlineStylesheets: 'never' }, - }); - await fixture.build(); - }); - - it('Includes CSS for rendered entry', async () => { - const app = await fixture.loadTestAdapterApp(); - const request = new Request('http://example.com/launch-week'); - const response = await app.render(request); - const html = await response.text(); - const $ = cheerio.load(html); - - // Renders content - expect($('ul li')).to.have.a.lengthOf(3); - - // Includes styles - expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1); - }); - - it('Exclude CSS for non-rendered entries', async () => { - const app = await fixture.loadTestAdapterApp(); - const request = new Request('http://example.com/'); - const response = await app.render(request); - const html = await response.text(); - const $ = cheerio.load(html); - - // Includes styles - expect($('link[rel=stylesheet]')).to.have.a.lengthOf(0); - }); - - it('De-duplicates CSS used both in layout and directly in target page', async () => { - const app = await fixture.loadTestAdapterApp(); - const request = new Request('http://example.com/with-layout-prop/'); - const response = await app.render(request); - const html = await response.text(); - const $ = cheerio.load(html); - - const set = new Set(); - - $('link[rel=stylesheet]').each((_, linkEl) => { - const href = linkEl.attribs.href; - expect(set).to.not.contain(href); - set.add(href); - }); - - $('style').each((_, styleEl) => { - const textContent = styleEl.children[0].data; - expect(set).to.not.contain(textContent); - set.add(textContent); - }); - }); - - it('Applies MDX components export', async () => { - const app = await fixture.loadTestAdapterApp(); - const request = new Request('http://example.com/launch-week-components-export'); - const response = await app.render(request); - const html = await response.text(); - const $ = cheerio.load(html); - - const h2 = $('h2'); - expect(h2).to.have.a.lengthOf(1); - expect(h2.attr('data-components-export-applied')).to.equal('true'); - }); - - it('getCollection should return new instances of the array to be mutated safely', async () => { - const app = await fixture.loadTestAdapterApp(); - - let request = new Request('http://example.com/sort-blog-collection'); - let response = await app.render(request); - let html = await response.text(); - let $ = cheerio.load(html); - expect($('li').first().text()).to.equal('With Layout Prop'); - - request = new Request('http://example.com/'); - response = await app.render(request); - html = await response.text(); - $ = cheerio.load(html); - expect($('li').first().text()).to.equal('Hello world'); - }); - }); - - describe('Dev - SSG', () => { - let devServer; - /** @type {import('./test-utils').Fixture} */ - let fixture; - - before(async () => { - fixture = await loadFixture({ - root: './fixtures/content/', - }); - devServer = await fixture.startDevServer(); - }); - - after(async () => { - await devServer.stop(); - }); - - it('Includes CSS for rendered entry', async () => { - const response = await fixture.fetch('/launch-week', { method: 'GET' }); - expect(response.status).to.equal(200); - - const html = await response.text(); - const $ = cheerio.load(html); - - // Renders content - expect($('ul li')).to.have.a.lengthOf(3); - - // Includes styles - expect($('head > style')).to.have.a.lengthOf(1); - expect($('head > style').text()).to.include("font-family: 'Comic Sans MS'"); - }); - - it('Includes component scripts for rendered entry', async () => { - const response = await fixture.fetch('/launch-week-component-scripts', { method: 'GET' }); - expect(response.status).to.equal(200); - - const html = await response.text(); - const $ = cheerio.load(html); - - const allScripts = $('head > script[src]'); - expect(allScripts).to.have.length; - - // Includes hoisted script - expect( - [...allScripts].find((script) => script.attribs.src.includes('WithScripts.astro')), - '`WithScripts.astro` hoisted script missing from head.' - ).to.not.be.undefined; - - // Includes inline script - expect($('script[data-is-inline]')).to.have.a.lengthOf(1); - }); - - it('Applies MDX components export', async () => { - const response = await fixture.fetch('/launch-week-components-export', { method: 'GET' }); - expect(response.status).to.equal(200); - - const html = await response.text(); - const $ = cheerio.load(html); - - const h2 = $('h2'); - expect(h2).to.have.a.lengthOf(1); - expect(h2.attr('data-components-export-applied')).to.equal('true'); - }); - - it('Supports layout prop with recursive getCollection() call', async () => { - const response = await fixture.fetch('/with-layout-prop', { method: 'GET' }); - expect(response.status).to.equal(200); - - const html = await response.text(); - const $ = cheerio.load(html); - - const body = $('body'); - expect(body.attr('data-layout-prop')).to.equal('true'); - - const h1 = $('h1'); - expect(h1).to.have.a.lengthOf(1); - expect(h1.text()).to.equal('With Layout Prop'); - - const h2 = $('h2'); - expect(h2).to.have.a.lengthOf(1); - expect(h2.text()).to.equal('Content with a layout prop'); - }); - }); -}); -- cgit v1.2.3