Skip to content

Commit

Permalink
tests: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Jan 15, 2025
1 parent bef5d6f commit c539551
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
33 changes: 18 additions & 15 deletions __tests__/custom-components/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { execute } from '../helpers';

describe('Custom Components', () => {
const Example = { default: () => <div>It works!</div> };
const Composite = {
default: () => (
<>
<div>Does it work?</div>
<Example.default />
</>
),
};
describe('Custom Components', async () => {
const Example = await execute(`It works!`, {}, {}, { getDefault: false });
const Multiple = await execute(
`
export const First = () => <div>First</div>;
export const Second = () => <div>Second</div>;
`,
{},
{},
{ getDefault: false },
);

it('renders custom components', async () => {
const doc = `
Expand All @@ -24,14 +25,16 @@ describe('Custom Components', () => {
expect(screen.getByText('It works!')).toBeVisible();
});

it('renders custom components recursively', async () => {
it('renders a custom component with multiple exports', async () => {
const doc = `
<Composite />
`;
<First />
const Page = await execute(doc, undefined, { components: { Example, Composite } });
<Second />
`;
const Page = await execute(doc, undefined, { components: { Multiple } });
render(<Page />);

expect(screen.getByText('It works!')).toBeVisible();
expect(screen.getByText('First')).toBeVisible();
expect(screen.getByText('Second')).toBeVisible();
});
});
7 changes: 4 additions & 3 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export const silenceConsole =
}
};

export const execute = async (doc: string, compileOpts = {}, runOpts = {}) => {
export const execute = async (doc: string, compileOpts = {}, runOpts = {}, { getDefault = true } = {}) => {
const code = compile(doc, compileOpts);
const module = await run(code, runOpts);
return module.default;
const mod = await run(code, runOpts);

return getDefault ? mod.default : mod;
};

export const migrate = (doc: string) => {
Expand Down
14 changes: 8 additions & 6 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ const makeUseMDXComponents = (more: ReturnType<UseMdxComponents> = {}): UseMdxCo
const run = async (string: string, _opts: RunOpts = {}) => {
const { Fragment } = runtime as any;
const { components = {}, terms, variables, baseUrl, ...opts } = _opts;
const defaults = Object.entries(components).reduce((memo, [tag, mod]) => {
const executedComponents = Object.entries(components).reduce((memo, [tag, mod]) => {
memo[tag] = mod.default;

Object.entries(mod.exports).forEach(([subTag, component]) => {
memo[subTag] = component;
});
if (mod.exports) {
Object.entries(mod.exports).forEach(([subTag, component]) => {
memo[subTag] = component;
});
}

return memo;
}, {});

const exec = (text: string, { useMDXComponents = makeUseMDXComponents(defaults) }: RunOpts = {}) => {
const exec = (text: string, { useMDXComponents = makeUseMDXComponents(executedComponents) }: RunOpts = {}) => {
return mdxRun(text, {
...runtime,
Fragment,
Expand All @@ -75,7 +77,7 @@ const run = async (string: string, _opts: RunOpts = {}) => {
}) as Promise<RMDXModule>;
};

const { toc, default: Content, ...exports } = await exec(string);
const { Toc: _Toc, toc, default: Content, ...exports } = await exec(string);

const tocMdx = tocToMdx(toc, components);
const { default: Toc } = await exec(compile(tocMdx), { useMDXComponents: () => ({ p: Fragment }) });
Expand Down

0 comments on commit c539551

Please sign in to comment.