test: inject components export

This commit is contained in:
bholmesdev 2022-09-27 10:33:15 -04:00
parent e05ef2d492
commit 2bf00cc68d
6 changed files with 64 additions and 0 deletions

View file

@ -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 };
`));
};
}
]
})],
}

View file

@ -0,0 +1,7 @@
{
"name": "@test/mdx-utils-jstomdxtreenode",
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*"
}
}

View file

@ -0,0 +1 @@
<h1 data-is-title-component><slot></slot></h1>

View file

@ -0,0 +1,3 @@
# Injected header test
Let's see if that header maps to our Astro component!

View file

@ -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;
});
});

View file

@ -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:*