|
1 | 1 | import { render } from '@react-email/render'; |
| 2 | +import { Html } from '../html/index.js'; |
| 3 | +import { Tailwind } from '../tailwind/index.js'; |
2 | 4 | import { Body } from './index.js'; |
3 | 5 | import { marginProperties, paddingProperties } from './margin-properties.js'; |
4 | 6 |
|
@@ -55,6 +57,103 @@ describe('<Body> component', () => { |
55 | 57 | expect(tdStyle).toContain('padding:20px'); |
56 | 58 | }); |
57 | 59 |
|
| 60 | + describe('lang and dir inheritance from <Html>', () => { |
| 61 | + const getAttributes = (html: string, tag: 'html' | 'body' | 'td') => { |
| 62 | + const attributes = html.match(new RegExp(`<${tag}([^>]*)>`))?.[1] ?? ''; |
| 63 | + return { |
| 64 | + lang: attributes.match(/lang="([^"]*)"/)?.[1], |
| 65 | + dir: attributes.match(/dir="([^"]*)"/)?.[1], |
| 66 | + }; |
| 67 | + }; |
| 68 | + |
| 69 | + it('defaults to lang="en" dir="ltr" without an <Html> parent', async () => { |
| 70 | + const html = await render(<Body>Test</Body>); |
| 71 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'en', dir: 'ltr' }); |
| 72 | + expect(getAttributes(html, 'td')).toEqual({ lang: 'en', dir: 'ltr' }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('inherits lang and dir set on <Html>', async () => { |
| 76 | + const html = await render( |
| 77 | + <Html lang="ar" dir="rtl"> |
| 78 | + <Body>Test</Body> |
| 79 | + </Html>, |
| 80 | + ); |
| 81 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'ar', dir: 'rtl' }); |
| 82 | + expect(getAttributes(html, 'td')).toEqual({ lang: 'ar', dir: 'rtl' }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("inherits <Html>'s defaults when it has no explicit lang/dir", async () => { |
| 86 | + const html = await render( |
| 87 | + <Html> |
| 88 | + <Body>Test</Body> |
| 89 | + </Html>, |
| 90 | + ); |
| 91 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'en', dir: 'ltr' }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('lets explicit lang and dir on <Body> win over <Html>', async () => { |
| 95 | + const html = await render( |
| 96 | + <Html lang="pl"> |
| 97 | + <Body lang="en" dir="rtl"> |
| 98 | + Test |
| 99 | + </Body> |
| 100 | + </Html>, |
| 101 | + ); |
| 102 | + expect(getAttributes(html, 'html')).toEqual({ lang: 'pl', dir: 'ltr' }); |
| 103 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'en', dir: 'rtl' }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('inherits lang and dir through custom components', async () => { |
| 107 | + const Content = ({ children }: { children: React.ReactNode }) => { |
| 108 | + return <Body>{children}</Body>; |
| 109 | + }; |
| 110 | + const Layout = ({ children }: { children: React.ReactNode }) => { |
| 111 | + return <Content>{children}</Content>; |
| 112 | + }; |
| 113 | + |
| 114 | + const html = await render( |
| 115 | + <Html lang="pl"> |
| 116 | + <Layout>Cześć</Layout> |
| 117 | + </Html>, |
| 118 | + ); |
| 119 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'pl', dir: 'ltr' }); |
| 120 | + expect(getAttributes(html, 'td')).toEqual({ lang: 'pl', dir: 'ltr' }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('inherits lang and dir with <Tailwind> in between', async () => { |
| 124 | + const html = await render( |
| 125 | + <Html lang="pl"> |
| 126 | + <Tailwind> |
| 127 | + <Body className="bg-red-500">Cześć</Body> |
| 128 | + </Tailwind> |
| 129 | + </Html>, |
| 130 | + ); |
| 131 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'pl', dir: 'ltr' }); |
| 132 | + expect(html).toContain('background-color'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('inherits lang and dir with <Tailwind> around <Html>', async () => { |
| 136 | + const html = await render( |
| 137 | + <Tailwind> |
| 138 | + <Html lang="pl"> |
| 139 | + <Body className="bg-red-500">Cześć</Body> |
| 140 | + </Html> |
| 141 | + </Tailwind>, |
| 142 | + ); |
| 143 | + expect(getAttributes(html, 'body')).toEqual({ lang: 'pl', dir: 'ltr' }); |
| 144 | + expect(html).toContain('background-color'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('does not leak the private context prop into the markup', async () => { |
| 148 | + const html = await render( |
| 149 | + <Html lang="pl"> |
| 150 | + <Body>Test</Body> |
| 151 | + </Html>, |
| 152 | + ); |
| 153 | + expect(html.toLowerCase()).not.toContain('reactemailcontexts'); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
58 | 157 | describe('padding resetting behavior', () => { |
59 | 158 | for (const property of paddingProperties) { |
60 | 159 | it(`resets the ${property} property on body when it comes from props`, async () => { |
|
0 commit comments