Skip to content

Commit

Permalink
Publish experimental_renderToHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Aug 14, 2024
1 parent 0b0012e commit 1ea59a4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/react-markup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ npm install react react-markup
## Usage

```js
import { renderToHTML } from 'react-markup';
import { experimental_renderToHTML as renderToHTML } from 'react-markup';
import EmailTemplate from './my-email-template-component.js'

async function action(email, name) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-markup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
* @flow
*/

export * from './src/ReactMarkupClient';
export {renderToHTML as experimental_renderToHTML} from './src/ReactMarkupClient';
1 change: 0 additions & 1 deletion packages/react-markup/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "react-markup",
"version": "19.0.0",
"private": true,
"description": "React package generating embedded HTML markup such as e-mails using Server Components.",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-markup/react-markup.react-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
* @flow
*/

export * from './src/ReactMarkupServer';
export {renderToHTML as experimental_renderToHTML} from './src/ReactMarkupServer';
30 changes: 18 additions & 12 deletions packages/react-markup/src/__tests__/ReactMarkupClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (!__EXPERIMENTAL__) {
return <div>hello world</div>;
}

const html = await ReactMarkup.renderToHTML(<Component />);
const html = await ReactMarkup.experimental_renderToHTML(<Component />);
expect(html).toBe('<div>hello world</div>');
});

Expand All @@ -52,14 +52,14 @@ if (!__EXPERIMENTAL__) {
return <div>{'hello '.repeat(200)}world</div>;
}

const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
expect(html).toBe('<div>' + ('hello '.repeat(200) + 'world') + '</div>');
});

it('should prefix html tags with a doctype', async () => {
const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
<html>
<body>hello</body>
</html>,
Expand All @@ -76,8 +76,10 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(<Component />);
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(<Component />);
}).rejects.toThrow(
'Cannot use state or effect Hooks in renderToHTML because this component will never be hydrated.',
);
});

it('should error on refs passed to host components', async () => {
Expand All @@ -87,8 +89,10 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(<Component />);
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(<Component />);
}).rejects.toThrow(
'Cannot pass ref in renderToHTML because they will never be hydrated.',
);
});

it('should error on callbacks passed to event handlers', async () => {
Expand All @@ -100,8 +104,10 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(<Component />);
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(<Component />);
}).rejects.toThrow(
'Cannot pass event handlers (onClick) in renderToHTML because the HTML will never be hydrated so they can never get called.',
);
});

it('supports the useId Hook', async () => {
Expand Down Expand Up @@ -142,7 +148,7 @@ if (!__EXPERIMENTAL__) {
);
}

const html = await ReactMarkup.renderToHTML(<Component />);
const html = await ReactMarkup.experimental_renderToHTML(<Component />);
const container = document.createElement('div');
container.innerHTML = html;

Expand Down Expand Up @@ -176,7 +182,7 @@ if (!__EXPERIMENTAL__) {
);
}

const html = await ReactMarkup.renderToHTML(<Component />);
const html = await ReactMarkup.experimental_renderToHTML(<Component />);
expect(html).toBe('<div>01</div>');
});

Expand All @@ -199,7 +205,7 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(
await ReactMarkup.experimental_renderToHTML(
<div>
<Foo />
</div>,
Expand Down
38 changes: 25 additions & 13 deletions packages/react-markup/src/__tests__/ReactMarkupServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if (!__EXPERIMENTAL__) {
return React.createElement('div', null, 'hello world');
}

const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
expect(html).toBe('<div>hello world</div>');
Expand All @@ -76,15 +76,14 @@ if (!__EXPERIMENTAL__) {
return React.createElement('div', null, 'hello '.repeat(200) + 'world');
}

const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
expect(html).toBe('<div>' + ('hello '.repeat(200) + 'world') + '</div>');
});

it('should prefix html tags with a doctype', async () => {
const html = await ReactMarkup.renderToHTML(
// We can't use JSX because that's client-JSX in our tests.
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(
'html',
null,
Expand All @@ -104,8 +103,10 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(React.createElement(Component));
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
}).rejects.toThrow('React.useState is not a function');
});

it('should error on refs passed to host components', async () => {
Expand All @@ -116,8 +117,12 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(React.createElement(Component));
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
}).rejects.toThrow(
'Refs cannot be used in Server Components, nor passed to Client Components.',
);
});

it('should error on callbacks passed to event handlers', async () => {
Expand All @@ -130,8 +135,15 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(React.createElement(Component));
}).rejects.toThrow();
await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
}).rejects.toThrowError(
`Event handlers cannot be passed to Client Component props.\n` +
' <div onClick={function onClick}>\n' +
' ^^^^^^^^^^^^^^^^^^\n' +
'If you need interactivity, consider converting part of this to a Client Component.',
);
});

it('supports the useId Hook', async () => {
Expand Down Expand Up @@ -173,7 +185,7 @@ if (!__EXPERIMENTAL__) {
);
}

const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
const container = document.createElement('div');
Expand Down Expand Up @@ -204,7 +216,7 @@ if (!__EXPERIMENTAL__) {
return React.createElement('div', null, a, b);
}

const html = await ReactMarkup.renderToHTML(
const html = await ReactMarkup.experimental_renderToHTML(
React.createElement(Component),
);
expect(html).toBe('<div>00</div>');
Expand All @@ -225,7 +237,7 @@ if (!__EXPERIMENTAL__) {
}

await expect(async () => {
await ReactMarkup.renderToHTML(
await ReactMarkup.experimental_renderToHTML(
React.createElement('div', null, React.createElement(Foo)),
{
onError(error, errorInfo) {
Expand Down

0 comments on commit 1ea59a4

Please sign in to comment.