From fa9bd3bcecc9143d5a645e2e21a7ac36ec814a59 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 9 Feb 2023 13:25:18 -0500 Subject: [PATCH] test: entry and collections parse --- packages/integrations/markdoc/src/index.ts | 2 +- .../markdoc/test/content-collections.test.js | 72 +++++++++++++++++++ .../content-collections/astro.config.mjs | 7 ++ .../src/components/CustomMarquee.astro | 1 + .../src/content/blog/simple.mdoc | 7 ++ .../src/content/blog/with-components.mdoc | 17 +++++ .../src/content/blog/with-config.mdoc | 14 ++++ .../content-collections/src/content/config.ts | 12 ++++ .../src/pages/collection.json.js | 10 +++ .../src/pages/content-simple.astro | 18 +++++ .../src/pages/content-with-components.astro | 50 +++++++++++++ .../src/pages/content-with-config.astro | 38 ++++++++++ .../src/pages/entry.json.js | 10 +++ .../fixtures/content-collections/utils.js | 8 +++ 14 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 packages/integrations/markdoc/test/content-collections.test.js create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/astro.config.mjs create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/components/CustomMarquee.astro create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/simple.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-components.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-config.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/content/config.ts create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/pages/collection.json.js create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-simple.astro create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-components.astro create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-config.astro create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/src/pages/entry.json.js create mode 100644 packages/integrations/markdoc/test/fixtures/content-collections/utils.js diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index 516980735..9d386a555 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -45,7 +45,7 @@ if (!config) { config = {}; } } -return Markdoc.transform(getParsed(), config) }\nexport async function Content ({ transformConfig, components }) { return h(Renderer, { content: await getTransformed(transformConfig), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`; +return Markdoc.transform(getParsed(), config) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: await getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`; }, }, ], diff --git a/packages/integrations/markdoc/test/content-collections.test.js b/packages/integrations/markdoc/test/content-collections.test.js new file mode 100644 index 000000000..d0725b5bd --- /dev/null +++ b/packages/integrations/markdoc/test/content-collections.test.js @@ -0,0 +1,72 @@ +import { parseHTML } from 'linkedom'; +import { parse as parseDevalue } from 'devalue'; +import { expect } from 'chai'; +import { loadFixture } from '../../../astro/test/test-utils.js'; + +describe('Markdoc - Content Collections', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/content-collections/', import.meta.url), + }); + }); + + describe('dev', () => { + let devServer; + + before(async () => { + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('loads entry', async () => { + const res = await fixture.fetch('/entry.json'); + const post = parseDevalue(await res.text()); + expect(post).to.deep.equal(simplePostEntry); + }); + + it('loads collection', async () => { + const res = await fixture.fetch('/collection.json'); + const posts = parseDevalue(await res.text()); + expect(posts).to.not.be.null; + expect(posts.sort()).to.deep.equal([ + simplePostEntry, + { + id: 'with-components.mdoc', + slug: 'with-components', + collection: 'blog', + data: { + schemaWorks: true, + title: 'Post with components', + }, + body: '\n## Post with components\n\nThis uses a custom marquee component with a shortcode:\n\n{% mq direction="right" %}\nI\'m a marquee too!\n{% /mq %}\n\nAnd a code component for code blocks:\n\n```js\nconst isRenderedWithShiki = true;\n```\n', + }, + { + id: 'with-config.mdoc', + slug: 'with-config', + collection: 'blog', + data: { + schemaWorks: true, + title: 'Post with config', + }, + body: '\n## Post with config\n\nThis uses a shortcode to render a marquee element,\nwith a variable to show and hide:\n\n{% if $showMarquee %}\n{% mq direction="down" %}\nIm a marquee!\n{% /mq %}\n{% /if %}\n', + }, + ]); + }); + }); +}); + +const simplePostEntry = { + id: 'simple.mdoc', + slug: 'simple', + collection: 'blog', + data: { + schemaWorks: true, + title: 'Simple post', + }, + body: '\n## Simple post\n\nThis is a simple Markdoc post.\n', +}; diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/astro.config.mjs b/packages/integrations/markdoc/test/fixtures/content-collections/astro.config.mjs new file mode 100644 index 000000000..29d846359 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import markdoc from '@astrojs/markdoc'; + +// https://astro.build/config +export default defineConfig({ + integrations: [markdoc()], +}); diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/components/CustomMarquee.astro b/packages/integrations/markdoc/test/fixtures/content-collections/src/components/CustomMarquee.astro new file mode 100644 index 000000000..3108b9973 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/components/CustomMarquee.astro @@ -0,0 +1 @@ + diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/simple.mdoc b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/simple.mdoc new file mode 100644 index 000000000..557f7b8e5 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/simple.mdoc @@ -0,0 +1,7 @@ +--- +title: Simple post +--- + +## Simple post + +This is a simple Markdoc post. diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-components.mdoc b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-components.mdoc new file mode 100644 index 000000000..2c9bae972 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-components.mdoc @@ -0,0 +1,17 @@ +--- +title: Post with components +--- + +## Post with components + +This uses a custom marquee component with a shortcode: + +{% mq direction="right" %} +I'm a marquee too! +{% /mq %} + +And a code component for code blocks: + +```js +const isRenderedWithShiki = true; +``` diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-config.mdoc b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-config.mdoc new file mode 100644 index 000000000..8563378fc --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/blog/with-config.mdoc @@ -0,0 +1,14 @@ +--- +title: Post with config +--- + +## Post with config + +This uses a shortcode to render a marquee element, +with a variable to show and hide: + +{% if $showMarquee %} +{% mq direction="down" %} +Im a marquee! +{% /mq %} +{% /if %} diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/content/config.ts b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/config.ts new file mode 100644 index 000000000..3b411201a --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/content/config.ts @@ -0,0 +1,12 @@ +import { defineCollection, z } from 'astro:content'; + +const blog = defineCollection({ + schema: z.object({ + title: z.string(), + }).transform(data => ({ + ...data, + schemaWorks: true, + })) +}); + +export const collections = { blog }; diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/collection.json.js b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/collection.json.js new file mode 100644 index 000000000..98ae36a58 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/collection.json.js @@ -0,0 +1,10 @@ +import { getCollection } from 'astro:content'; +import { stringify } from 'devalue'; +import { stripAllRenderFn } from '../../utils.js'; + +export async function get() { + const posts = await getCollection('blog'); + return { + body: stringify(stripAllRenderFn(posts)) + }; +} diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-simple.astro b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-simple.astro new file mode 100644 index 000000000..effbbee1c --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-simple.astro @@ -0,0 +1,18 @@ +--- +import { getEntryBySlug } from "astro:content"; +const post = await getEntryBySlug('blog', 'simple'); +const { Content } = await post.render(); +--- + + + + + + + + Content - Simple + + + + + diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-components.astro b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-components.astro new file mode 100644 index 000000000..ad65bec64 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-components.astro @@ -0,0 +1,50 @@ +--- +import { getEntryBySlug } from "astro:content"; +import { Code } from 'astro/components'; +import CustomMarquee from '../components/CustomMarquee.astro'; +import Markdoc from '@markdoc/markdoc'; + +const post = await getEntryBySlug('blog', 'with-components'); +const { Content } = await post.render(); +--- + + + + + + + + Content - with components + + + + + diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-config.astro b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-config.astro new file mode 100644 index 000000000..d63e2b422 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/content-with-config.astro @@ -0,0 +1,38 @@ +--- +import { getEntryBySlug } from "astro:content"; + +const post = await getEntryBySlug('blog', 'with-config'); +const { Content } = await post.render(); +--- + + + + + + + + Content - with config + + + + + diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/entry.json.js b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/entry.json.js new file mode 100644 index 000000000..842291826 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/src/pages/entry.json.js @@ -0,0 +1,10 @@ +import { getEntryBySlug } from 'astro:content'; +import { stringify } from 'devalue'; +import { stripRenderFn } from '../../utils.js'; + +export async function get() { + const post = await getEntryBySlug('blog', 'simple'); + return { + body: stringify(stripRenderFn(post)), + }; +} diff --git a/packages/integrations/markdoc/test/fixtures/content-collections/utils.js b/packages/integrations/markdoc/test/fixtures/content-collections/utils.js new file mode 100644 index 000000000..3a6244327 --- /dev/null +++ b/packages/integrations/markdoc/test/fixtures/content-collections/utils.js @@ -0,0 +1,8 @@ +export function stripRenderFn(entryWithRender) { + const { render, ...entry } = entryWithRender; + return entry; +} + +export function stripAllRenderFn(collection = []) { + return collection.map(stripRenderFn); +}