Skip to content

Commit

Permalink
fix(jsx-email): properly handle different preview props. fixes #132 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Jan 11, 2024
1 parent a8dd381 commit 9f9d208
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 2 deletions.
3 changes: 2 additions & 1 deletion apps/preview/app/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const templateRoutes = templates.map(async (template) => {
let props: any;

if (TemplateStruct) props = create({}, TemplateStruct);
else if (PreviewProps) props = PreviewProps();
else if (typeof PreviewProps === 'function') props = PreviewProps();
else if (PreviewProps) props = PreviewProps;
else if ((Template as any).PreviewProps) {
warn(
`jsx-email: ${Name} → PreviewProps as a property of a component is deprecated. Please used a named export.`
Expand Down
16 changes: 16 additions & 0 deletions apps/test/fixtures/default-export-props-fn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Body, Html } from 'jsx-email';

export const TemplateName = 'default-export-props-fn';

const Template = ({ test }: { test: string }) => (
<Html>
<Body>{test}</Body>
</Html>
);

Template.PreviewProps = () => {
return { test: 'batman' };
};

// eslint-disable-next-line import/no-default-export
export default Template;
14 changes: 14 additions & 0 deletions apps/test/fixtures/default-export.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Body, Html } from 'jsx-email';

export const TemplateName = 'default-export';

const Template = ({ test }: { test: string }) => (
<Html>
<Body>{test}</Body>
</Html>
);

Template.PreviewProps = { test: 'batman' };

// eslint-disable-next-line import/no-default-export
export default Template;
13 changes: 13 additions & 0 deletions apps/test/fixtures/preview-props-fn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Body, Html } from 'jsx-email';

export const TemplateName = 'preview-props-fn';

export const Template = ({ test }: { test: string }) => (
<Html>
<Body>{test}</Body>
</Html>
);

Template.PreviewProps = () => {
return { test: 'batman' };
};
11 changes: 11 additions & 0 deletions apps/test/fixtures/preview-props-named.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Body, Html } from 'jsx-email';

export const TemplateName = 'preview-props';

export const Template = ({ test }: { test: string }) => (
<Html>
<Body>{test}</Body>
</Html>
);

export const PreviewProps = { test: 'batman' };
11 changes: 11 additions & 0 deletions apps/test/fixtures/preview-props.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Body, Html } from 'jsx-email';

export const TemplateName = 'preview-props';

export const Template = ({ test }: { test: string }) => (
<Html>
<Body>{test}</Body>
</Html>
);

Template.PreviewProps = { test: 'batman' };
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html
lang="en"
dir="ltr"
>
<head>
</head>
<body>
batman
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html
lang="en"
dir="ltr"
>
<head>
</head>
<body>
batman
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html
lang="en"
dir="ltr"
>
<head>
</head>
<body>
batman
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html
lang="en"
dir="ltr"
>
<head>
</head>
<body>
batman
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html
lang="en"
dir="ltr"
>
<head>
</head>
<body>
batman
</body>
</html>
12 changes: 11 additions & 1 deletion apps/test/tests/smoke.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ test('landing', async ({ page }) => {
await expect(page.locator('#link-Base')).toBeVisible();
});

const pages = ['Base', 'Code', 'Local-Assets', 'Tailwind'];
const pages = [
'Base',
'Code',
'Default-Export',
'Default-Export-Props-Fn',
'Local-Assets',
'Preview-Props',
'Preview-Props-Fn',
'Preview-Props-Named',
'Tailwind'
];

pages.forEach((name) => {
test(`page: ${name}`, async ({ page }) => {
Expand Down

0 comments on commit 9f9d208

Please sign in to comment.