0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

test: jsToTreeNode basic and export injection

This commit is contained in:
bholmesdev 2022-08-01 16:42:05 -04:00
parent c1c63f218c
commit 82351f88c3

View file

@ -0,0 +1,53 @@
import { jsToTreeNode } from '@astrojs/mdx/utils';
import { compile } from '@mdx-js/mdx';
import { expect } from 'chai';
import { parse as parseESM } from 'es-module-lexer';
import { rehypeReadingTime } from './test-utils.js'
const sampleMdx = `# I'm MDX!`
describe('jsToTreeNode', () => {
it('handles basic JS injection', async () => {
const js = `console.log('working')`
function injectConsoleLog() {
return function (tree) {
tree.children.unshift(
jsToTreeNode(js)
)
};
}
const compiled = await compile(sampleMdx, {
rehypePlugins: [injectConsoleLog],
})
expect(String(compiled.value)).to.contain(js)
})
it('handles export injection', async () => {
function injectExport() {
return function (tree) {
tree.children.unshift(
jsToTreeNode(`export const working = true`)
)
};
}
const compiled = await compile(sampleMdx, {
rehypePlugins: [injectExport],
})
const [, moduleExports] = parseESM(compiled.value)
expect(moduleExports).to.contain('working')
})
it('handles readingTime example', async () => {
const compiled = await compile(sampleMdx, {
rehypePlugins: [rehypeReadingTime],
})
const [, moduleExports] = parseESM(compiled.value)
expect(moduleExports).to.contain('readingTime')
expect(String(compiled.value)).to.match(/\d+ min read/)
})
})