Skip to content

Commit

Permalink
Change tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-zhukov committed Mar 28, 2022
1 parent cd9b32f commit 1d00a4d
Showing 1 changed file with 32 additions and 49 deletions.
81 changes: 32 additions & 49 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,38 @@ test('compile', async () => {
)
}

try {
renderToStaticMarkup(
React.createElement(
await run(compileSync('export const a = {}\n\n<a.b />'))
)
)
assert.unreachable()
} catch (/** @type {unknown} */ error) {
const exception = /** @type {Error} */ (error)
assert.match(
exception.message,
/Expected component `a.b` to be defined/,
'should throw if a required member is not passed'
)
}

try {
renderToStaticMarkup(
React.createElement(
await run(compileSync('<a render={(x) => <x.y />} />'))
)
)
assert.unreachable()
} catch (/** @type {unknown} */ error) {
const exception = /** @type {Error} */ (error)
assert.match(
exception.message,
/x is not defined/,
'should throw if a required member is not passed'
)
}

try {
renderToStaticMarkup(
React.createElement(await run(compileSync('<X />', {development: true})))
Expand Down Expand Up @@ -791,55 +823,6 @@ test('jsx', async () => {
'should serialize fragments, namespaces, members'
)

assert.equal(
String(
compileSync('export const a = {}\n\n<a.b />', {
jsx: true
})
),
[
'/*@jsxRuntime automatic @jsxImportSource react*/',
'export const a = {};',
'function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent /></MDXLayout> : _createMdxContent();',
' function _createMdxContent() {',
' if (!a) _missingMdxReference("a", false);',
' if (!a.b) _missingMdxReference("a.b", true);',
' return <a.b />;',
' }',
'}',
'export default MDXContent;',
'function _missingMdxReference(id, component) {',
' throw new Error("Expected " + (component ? "component" : "object") + " `" + id + "` to be defined: you likely forgot to import, pass, or provide it.");',
'}',
''
].join('\n'),
'should always inject _missingMdxReference checks'
)

assert.equal(
String(compileSync('<a render={(x) => <x.y />} />', {jsx: true})),
[
'/*@jsxRuntime automatic @jsxImportSource react*/',
'function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent /></MDXLayout> : _createMdxContent();',
' function _createMdxContent() {',
' if (!x) _missingMdxReference("x", false);',
' if (!x.y) _missingMdxReference("x.y", true);',
' return <a render={x => <x.y />} />;',
' }',
'}',
'export default MDXContent;',
'function _missingMdxReference(id, component) {',
' throw new Error("Expected " + (component ? "component" : "object") + " `" + id + "` to be defined: you likely forgot to import, pass, or provide it.");',
'}',
''
].join('\n'),
'should serialize members inside expressions'
)

assert.equal(
String(compileSync('<>a {/* 1 */} b</>', {jsx: true})),
[
Expand Down

0 comments on commit 1d00a4d

Please sign in to comment.