From 2bf00cc68da6be1e591b8eb157c010bbdc2a35f4 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 27 Sep 2022 10:33:15 -0400 Subject: [PATCH] test: inject components export --- .../astro.config.mjs | 23 +++++++++++++++++++ .../mdx-utils-jsToMdxTreeNode/package.json | 7 ++++++ .../src/components/Title.astro | 1 + .../src/pages/index.mdx | 3 +++ .../test/mdx-utils-jsToMdxTreeNode.test.js | 22 ++++++++++++++++++ pnpm-lock.yaml | 8 +++++++ 6 files changed, 64 insertions(+) create mode 100644 packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/astro.config.mjs create mode 100644 packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/package.json create mode 100644 packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/components/Title.astro create mode 100644 packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/pages/index.mdx create mode 100644 packages/integrations/mdx/test/mdx-utils-jsToMdxTreeNode.test.js diff --git a/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/astro.config.mjs b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/astro.config.mjs new file mode 100644 index 000000000..b0774efd6 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/astro.config.mjs @@ -0,0 +1,23 @@ +import mdx from '@astrojs/mdx'; +import { jsToMdxTreeNode } from '@astrojs/mdx/utils'; + +export default { + site: 'https://mdx-is-neat.com/', + markdown: { + syntaxHighlight: false, + }, + integrations: [mdx({ + remarkPlugins: [ + function injectComponentsExport() { + const titlePath = new URL('./src/components/Title.astro', import.meta.url).pathname; + return (tree) => { + tree.children.unshift( + jsToMdxTreeNode(` + import Title from ${JSON.stringify(titlePath)}; + export const components = { h1: Title }; + `)); + }; + } + ] + })], +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/package.json b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/package.json new file mode 100644 index 000000000..6a83be3e3 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/package.json @@ -0,0 +1,7 @@ +{ + "name": "@test/mdx-utils-jstomdxtreenode", + "dependencies": { + "astro": "workspace:*", + "@astrojs/mdx": "workspace:*" + } +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/components/Title.astro b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/components/Title.astro new file mode 100644 index 000000000..e6dc381e2 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/components/Title.astro @@ -0,0 +1 @@ +

diff --git a/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/pages/index.mdx b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/pages/index.mdx new file mode 100644 index 000000000..c6c91f4ec --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode/src/pages/index.mdx @@ -0,0 +1,3 @@ +# Injected header test + +Let's see if that header maps to our Astro component! diff --git a/packages/integrations/mdx/test/mdx-utils-jsToMdxTreeNode.test.js b/packages/integrations/mdx/test/mdx-utils-jsToMdxTreeNode.test.js new file mode 100644 index 000000000..373fd08c1 --- /dev/null +++ b/packages/integrations/mdx/test/mdx-utils-jsToMdxTreeNode.test.js @@ -0,0 +1,22 @@ +import { expect } from 'chai'; +import { parseHTML } from 'linkedom'; +import { loadFixture } from '../../../astro/test/test-utils.js'; + +describe('MDX utils - jsToMdxTreeNode', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/mdx-utils-jsToMdxTreeNode/', import.meta.url), + }); + + await fixture.build(); + }); + + it('Successfully injects title component export', async () => { + const html = await fixture.readFile('/index.html'); + const { document } = parseHTML(html); + + expect(document.querySelector('[data-is-title-component]')).to.not.be.null; + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 116192d3c..0f827adbf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2539,6 +2539,14 @@ importers: '@astrojs/react': link:../../../../react astro: link:../../../../../astro + packages/integrations/mdx/test/fixtures/mdx-utils-jsToMdxTreeNode: + specifiers: + '@astrojs/mdx': workspace:* + astro: workspace:* + dependencies: + '@astrojs/mdx': link:../../.. + astro: link:../../../../../astro + packages/integrations/mdx/test/fixtures/mdx-vite-env-vars: specifiers: '@astrojs/mdx': workspace:*