Skip to content

Commit 3dcb9ec

Browse files
committed
test: add tests for preact and hono
1 parent 0558872 commit 3dcb9ec

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"esbuild": "0.*"
5454
},
5555
"devDependencies": {
56+
"@testing-library/preact": "3.2.4",
5657
"@testing-library/react": "^14.1.0",
5758
"@types/jsdom": "^21.1.5",
5859
"@types/mdx": "^2.0.10",
@@ -63,10 +64,12 @@
6364
"c8": "^8.0.1",
6465
"cross-env": "^7.0.3",
6566
"esbuild": "^0.19.5",
67+
"hono": "4.6.14",
6668
"jsdom": "^22.1.0",
6769
"kcd-scripts": "^14.0.1",
6870
"left-pad": "^1.3.0",
6971
"mdx-test-data": "^1.0.1",
72+
"preact": "10.25.3",
7073
"react": "^18.2.0",
7174
"react-dom": "^18.2.0",
7275
"remark-mdx-images": "^3.0.0",

src/__tests__/hono.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import './setup-tests.js'
2+
import { Hono } from "hono";
3+
import * as HonoJSX from "hono/jsx";
4+
import * as HonoDOM from "hono/jsx/dom";
5+
import * as _jsx_runtime from "hono/jsx/jsx-runtime";
6+
import {test} from 'uvu'
7+
import * as assert from 'uvu/assert'
8+
import {bundleMDX} from '../index.js'
9+
import {getMDXComponent} from '../client/jsx.js'
10+
11+
12+
13+
const jsxBundlerConfig = {
14+
jsxLib: {
15+
varName: 'HonoJSX',
16+
package: 'hono/jsx',
17+
},
18+
jsxDom: {
19+
varName: 'HonoDOM',
20+
package: 'hono/jsx/dom',
21+
},
22+
jsxRuntime: {
23+
varName: '_jsx_runtime',
24+
package: 'hono/jsx/jsx-runtime',
25+
},
26+
}
27+
const jsxComponentConfig = { HonoJSX, HonoDOM, _jsx_runtime }
28+
29+
const mdxSource = `
30+
---
31+
title: Example Post
32+
published: 2021-02-13
33+
description: This is some meta-data
34+
---
35+
import Demo from './demo'
36+
37+
# This is the title
38+
39+
Here's a **neat** demo:
40+
<Demo />
41+
`.trim();
42+
43+
const demoTsx = `
44+
export default function Demo() {
45+
return <div>mdx-bundler with hono's runtime!</div>
46+
}
47+
`.trim();
48+
49+
50+
test('smoke test for hono', async () => {
51+
52+
const result = await bundleMDX({
53+
source: mdxSource,
54+
jsxConfig: jsxBundlerConfig,
55+
files: {
56+
'./demo.tsx': demoTsx
57+
}
58+
});
59+
60+
const SpanBold = ({ children }) => {
61+
return HonoJSX.createElement('span', { className: "strong" }, children)
62+
}
63+
64+
const app = new Hono()
65+
.get("/", (c) => {
66+
const Component = getMDXComponent(result.code, jsxComponentConfig);
67+
return c.html(HonoJSX.jsx(Component, { components: { strong: SpanBold } }).toString());
68+
});
69+
70+
const req = new Request("http://localhost/");
71+
const res = await app.fetch(req);
72+
assert.equal(await res.text(), `<h1>This is the title</h1>
73+
<p>Here&#39;s a <span class="strong">neat</span> demo:</p>
74+
<div>mdx-bundler with hono&#39;s runtime!</div>`);
75+
})
76+
77+
test.run()

src/__tests__/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import leftPad from 'left-pad'
88
import remarkMdxImages from 'remark-mdx-images'
99
import {VFile} from 'vfile'
1010
import {bundleMDX} from '../index.js'
11-
import {getMDXComponent, getMDXExport} from '../client.js'
11+
import {getMDXComponent, getMDXExport} from '../client/react.js'
1212

1313
const {render} = rtl
1414

src/__tests__/preact.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import './setup-tests.js'
2+
import * as Preact from "preact";
3+
import * as PreactDOM from "preact/compat";
4+
import * as _jsx_runtime from 'preact/jsx-runtime';
5+
import {test} from 'uvu'
6+
import * as assert from 'uvu/assert'
7+
import { render } from '@testing-library/preact'
8+
import {bundleMDX} from '../index.js'
9+
import {getMDXComponent} from '../client/jsx.js'
10+
11+
12+
13+
const jsxBundlerConfig = {
14+
jsxLib: {
15+
varName: 'Preact',
16+
package: 'preact',
17+
},
18+
jsxDom: {
19+
varName: 'PreactDom',
20+
package: 'preact/compat',
21+
},
22+
jsxRuntime: {
23+
varName: '_jsx_runtime',
24+
package: 'preact/jsx-runtime',
25+
},
26+
}
27+
const jsxComponentConfig = { Preact, PreactDOM, _jsx_runtime }
28+
29+
const mdxSource = `
30+
---
31+
title: Example Post
32+
published: 2021-02-13
33+
description: This is some meta-data
34+
---
35+
import Demo from './demo'
36+
37+
# This is the title
38+
39+
Here's a **neat** demo:
40+
<Demo />
41+
`.trim();
42+
43+
const demoTsx = `
44+
export default function Demo() {
45+
return <div>mdx-bundler with Preact's runtime!</div>
46+
}
47+
`.trim();
48+
49+
50+
test('smoke test for preact', async () => {
51+
52+
const result = await bundleMDX({
53+
source: mdxSource,
54+
jsxConfig: jsxBundlerConfig,
55+
files: {
56+
'./demo.tsx': demoTsx
57+
}
58+
});
59+
60+
const Component = getMDXComponent(result.code, jsxComponentConfig)
61+
62+
/** @param {Preact.JSX.HTMLAttributes<HTMLSpanElement>} props */
63+
const SpanBold = ({children}) => {
64+
return Preact.createElement('span', { className: "strong" }, children)
65+
}
66+
67+
assert.equal(result.frontmatter, {
68+
title: 'Example Post',
69+
published: new Date('2021-02-13'),
70+
description: 'This is some meta-data',
71+
})
72+
73+
const {container} = render(
74+
Preact.h(Component, {components: {strong: SpanBold}})
75+
)
76+
77+
assert.equal(
78+
container.innerHTML,
79+
`<h1>This is the title</h1>
80+
<p>Here's a <span class="strong">neat</span> demo:</p>
81+
<div>mdx-bundler with Preact's runtime!</div>`,
82+
)
83+
})
84+
85+
test.run()

0 commit comments

Comments
 (0)