Skip to content
Myr

components.test.tsx

This file

Path

src/jsx/intrinsic-element/components.test.tsx

666 lines 路 23.6 KiB 路 TypeScript

Last changed by

Merge commit from forkTaku Amanobefore adoption2b8ed40

History of this file

Take it

1/** @jsxImportSource ../ */2import { useActionState } from '../'3import { HtmlEscapedCallbackPhase, resolveCallback } from '../../utils/html'45describe('intrinsic element', () => {6  describe('document metadata', () => {7    describe('title element', () => {8      it('should be hoisted title tag', async () => {9        const template = (10          <html>11            <head></head>12            <body>13              <title>Hello</title>14              <h1>World</h1>15            </body>16          </html>17        )18        expect(template.toString()).toBe(19          '<html><head><title>Hello</title></head><body><h1>World</h1></body></html>'20        )21      })2223      it('should hoist a title from an async component array', async () => {24        const Metadata = async () => [<title>Hello</title>]25        const template = (26          <html>27            <head></head>28            <body>29              <Metadata />30              <h1>World</h1>31            </body>32          </html>33        )34        const rendered = await template.toString()35        expect(await resolveCallback(rendered, HtmlEscapedCallbackPhase.Stringify, false, {})).toBe(36          '<html><head><title>Hello</title></head><body><h1>World</h1></body></html>'37        )38      })3940      it('should hoist a title with async children', async () => {41        const AsyncChild = async () => <>{'Hello'}</>42        const template = (43          <html>44            <head></head>45            <body>46              <title>47                <AsyncChild />48              </title>49              <h1>World</h1>50            </body>51          </html>52        )53        const rendered = await template.toString()54        expect(await resolveCallback(rendered, HtmlEscapedCallbackPhase.Stringify, false, {})).toBe(55          '<html><head><title>Hello</title></head><body><h1>World</h1></body></html>'56        )57      })5859      it('should render a title with async children without a head element', async () => {60        const AsyncChild = async () => <>{'Hello'}</>61        const template = (62          <html>63            <body>64              <title>65                <AsyncChild />66              </title>67              <h1>World</h1>68            </body>69          </html>70        )71        const rendered = await template.toString()72        const html = await resolveCallback(rendered, HtmlEscapedCallbackPhase.Stringify, false, {})73        expect(html.toString()).toBe('<html><body><title>Hello</title><h1>World</h1></body></html>')74        expect(html.toString()).not.toContain('[object Promise]')75      })7677      it('should render a title with itemProp and async children', async () => {78        const AsyncChild = async () => <>{'price < 100'}</>79        const template = <title itemProp='name'>{<AsyncChild />}</title>8081        expect(String(await template.toString())).toBe(82          '<title itemprop="name">price &lt; 100</title>'83        )84      })8586      it('should not interpret replacement patterns in a hoisted title', async () => {87        const template = (88          <html>89            <head></head>90            <body>91              <title>{'price $< 100'}</title>92            </body>93          </html>94        )95        const rendered = await template.toString()9697        expect(await resolveCallback(rendered, HtmlEscapedCallbackPhase.Stringify, false, {})).toBe(98          '<html><head><title>price $&lt; 100</title></head><body></body></html>'99        )100      })101    })102103    describe('link element', () => {104      it('should be hoisted link tag', async () => {105        const template = (106          <html>107            <head></head>108            <body>109              <link rel='stylesheet' href='style.css' precedence='default' />110              <h1>World</h1>111            </body>112          </html>113        )114        expect(template.toString()).toBe(115          '<html><head><link rel="stylesheet" href="style.css" data-precedence="default"/></head><body><h1>World</h1></body></html>'116        )117      })118119      it('should be ordered by precedence attribute', () => {120        const template = (121          <html>122            <head></head>123            <body>124              <link rel='stylesheet' href='style1.css' precedence='default' />125              <link rel='stylesheet' href='style2.css' precedence='high' />126              <link rel='stylesheet' href='style3.css' precedence='default' />127              <h1>World</h1>128            </body>129          </html>130        )131        expect(template.toString()).toBe(132          '<html><head><link rel="stylesheet" href="style1.css" data-precedence="default"/><link rel="stylesheet" href="style3.css" data-precedence="default"/><link rel="stylesheet" href="style2.css" data-precedence="high"/></head><body><h1>World</h1></body></html>'133        )134      })135136      it('should be de-duped by href', () => {137        const template = (138          <html>139            <head></head>140            <body>141              <link rel='stylesheet' href='style1.css' precedence='default' />142              <link rel='stylesheet' href='style2.css' precedence='high' />143              <link rel='stylesheet' href='style1.css' precedence='default' />144              <h1>World</h1>145            </body>146          </html>147        )148        expect(template.toString()).toBe(149          '<html><head><link rel="stylesheet" href="style1.css" data-precedence="default"/><link rel="stylesheet" href="style2.css" data-precedence="high"/></head><body><h1>World</h1></body></html>'150        )151      })152153      it('should be inserted as is if <head> is not present', () => {154        const template = (155          <html>156            <body>157              <link rel='stylesheet' href='style1.css' precedence='default' />158              <link rel='stylesheet' href='style2.css' precedence='high' />159              <h1>World</h1>160            </body>161          </html>162        )163        expect(template.toString()).toBe(164          '<html><body><link rel="stylesheet" href="style1.css" data-precedence="default"/><link rel="stylesheet" href="style2.css" data-precedence="high"/><h1>World</h1></body></html>'165        )166      })167168      it('should not do special behavior if disabled is present', () => {169        const template = (170          <html>171            <head></head>172            <body>173              <link rel='stylesheet' href='style1.css' precedence='default' />174              <link rel='stylesheet' href='style2.css' precedence='default' disabled />175              <h1>World</h1>176            </body>177          </html>178        )179        expect(template.toString()).toBe(180          '<html><head><link rel="stylesheet" href="style1.css" data-precedence="default"/></head><body><link rel="stylesheet" href="style2.css" precedence="default" disabled=""/><h1>World</h1></body></html>'181        )182      })183184      it('should not be hoisted if has no precedence attribute', () => {185        const template = (186          <html>187            <head></head>188            <body>189              <link rel='stylesheet' href='style1.css' />190              <h1>World</h1>191            </body>192          </html>193        )194        expect(template.toString()).toBe(195          '<html><head></head><body><link rel="stylesheet" href="style1.css"/><h1>World</h1></body></html>'196        )197      })198199      describe('React 19 compatibility', () => {200        const headContent = (html: string) => html.match(/<head>(.*)<\/head>/)?.[1] ?? ''201        const bodyContent = (html: string) => html.match(/<body>(.*)<\/body>/)?.[1] ?? ''202        const renderDocument = (children: unknown) =>203          (204            <html>205              <head></head>206              <body>{children}</body>207            </html>208          ).toString()209        const assertHeadAndBody = (children: unknown, expectedHead: string, expectedBody = '') => {210          const output = renderDocument(children)211          expect(headContent(output)).toBe(expectedHead)212          expect(bodyContent(output)).toBe(expectedBody)213        }214215        const canonical = () => <link rel='canonical' href='https://example.com/en/about' />216        const alternateEn = () => (217          <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />218        )219        const alternateJa = () => (220          <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />221        )222223        it('should keep canonical and alternates in source order', () => {224          assertHeadAndBody(225            <>226              {canonical()}227              {alternateEn()}228              {alternateJa()}229            </>,230            '<link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'231          )232        })233234        it('should keep alternate-canonical-alternate order', () => {235          assertHeadAndBody(236            <>237              {alternateEn()}238              {canonical()}239              {alternateJa()}240            </>,241            '<link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'242          )243        })244245        it('should not de-dupe canonical links', () => {246          assertHeadAndBody(247            <>248              {canonical()}249              {canonical()}250            </>,251            '<link rel="canonical" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/>'252          )253        })254255        it('should not de-dupe alternate links', () => {256          assertHeadAndBody(257            <>258              {alternateEn()}259              {alternateEn()}260            </>,261            '<link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/>'262          )263        })264265        it('should de-dupe stylesheet with precedence', () => {266          assertHeadAndBody(267            <>268              <link rel='stylesheet' href='/style.css' precedence='default' />269              <link rel='stylesheet' href='/style.css' precedence='default' />270            </>,271            '<link rel="stylesheet" href="/style.css" data-precedence="default"/>'272          )273        })274275        it('should not de-dupe stylesheet against preload with same href', () => {276          assertHeadAndBody(277            <>278              <link rel='preload' href='/style.css' as='style' />279              <link rel='stylesheet' href='/style.css' precedence='default' />280              <link rel='stylesheet' href='/style.css' precedence='default' />281            </>,282            '<link rel="stylesheet" href="/style.css" data-precedence="default"/><link rel="preload" href="/style.css" as="style"/>'283          )284        })285286        it('should not hoist stylesheet without precedence', () => {287          assertHeadAndBody(288            <>289              <link rel='stylesheet' href='/style.css' />290              <link rel='stylesheet' href='/style.css' />291            </>,292            '',293            '<link rel="stylesheet" href="/style.css"/><link rel="stylesheet" href="/style.css"/>'294          )295        })296297        it('should keep different stylesheets with same precedence', () => {298          assertHeadAndBody(299            <>300              <link rel='stylesheet' href='/a.css' precedence='default' />301              <link rel='stylesheet' href='/b.css' precedence='default' />302            </>,303            '<link rel="stylesheet" href="/a.css" data-precedence="default"/><link rel="stylesheet" href="/b.css" data-precedence="default"/>'304          )305        })306307        it('should not de-dupe preload links', () => {308          assertHeadAndBody(309            <>310              <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />311              <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />312            </>,313            '<link rel="preload" href="/font.woff2" as="font" crossorigin=""/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/>'314          )315        })316317        it('should not de-dupe modulepreload links', () => {318          assertHeadAndBody(319            <>320              <link rel='modulepreload' href='/module.js' />321              <link rel='modulepreload' href='/module.js' />322            </>,323            '<link rel="modulepreload" href="/module.js"/><link rel="modulepreload" href="/module.js"/>'324          )325        })326327        it('should keep links from two components', () => {328          const Head = () => (329            <>330              <link rel='canonical' href='https://example.com/en/about' />331              <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />332              <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />333            </>334          )335          const Body = () => (336            <>337              <link rel='canonical' href='https://example.com/en/about' />338              <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />339              <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />340            </>341          )342          assertHeadAndBody(343            <>344              <Head />345              <Body />346            </>,347            '<link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'348          )349        })350351        it('should hoist from deep nested component and keep duplicates', () => {352          const Nested = () => (353            <div>354              <div>355                <link rel='canonical' href='https://example.com/en/about' />356              </div>357            </div>358          )359          assertHeadAndBody(360            <>361              <link rel='canonical' href='https://example.com/en/about' />362              <Nested />363            </>,364            '<link rel="canonical" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/>',365            '<div><div></div></div>'366          )367        })368369        it('should keep mixed links and de-dupe only stylesheet', () => {370          assertHeadAndBody(371            <>372              <link rel='canonical' href='https://example.com/en/about' />373              <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />374              <link rel='stylesheet' href='/style.css' precedence='default' />375              <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />376              <link rel='canonical' href='https://example.com/en/about' />377              <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />378              <link rel='stylesheet' href='/style.css' precedence='default' />379              <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />380            </>,381            '<link rel="stylesheet" href="/style.css" data-precedence="default"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/>'382          )383        })384      })385    })386387    describe('meta element', () => {388      it('should be hoisted meta tag', async () => {389        const template = (390          <html>391            <head></head>392            <body>393              <meta name='description' content='Hello' />394              <h1>World</h1>395            </body>396          </html>397        )398        expect(template.toString()).toBe(399          '<html><head><meta name="description" content="Hello"/></head><body><h1>World</h1></body></html>'400        )401      })402403      it('should be de-duped by name', () => {404        const template = (405          <html>406            <head></head>407            <body>408              <meta name='description' content='Hello' />409              <meta name='description' content='World' />410              <h1>World</h1>411            </body>412          </html>413        )414        expect(template.toString()).toBe(415          '<html><head><meta name="description" content="Hello"/></head><body><h1>World</h1></body></html>'416        )417      })418419      it('should not do special behavior if itemProp is present', () => {420        const template = (421          <html>422            <head></head>423            <body>424              <meta name='description' content='Hello' itemProp='test' />425              <h1>World</h1>426            </body>427          </html>428        )429        expect(template.toString()).toBe(430          '<html><head></head><body><meta name="description" content="Hello" itemprop="test"/><h1>World</h1></body></html>'431        )432      })433    })434435    describe('script element', () => {436      it('should be hoisted script tag', async () => {437        const template = (438          <html>439            <head></head>440            <body>441              <script src='script.js' async={true} />442              <h1>World</h1>443            </body>444          </html>445        )446        expect(template.toString()).toBe(447          '<html><head><script src="script.js" async=""></script></head><body><h1>World</h1></body></html>'448        )449      })450451      it('should be de-duped by href with async={true}', () => {452        const template = (453          <html>454            <head></head>455            <body>456              <script src='script.js' async />457              <script src='script.js' async />458              <h1>World</h1>459            </body>460          </html>461        )462        expect(template.toString()).toBe(463          '<html><head><script src="script.js" async=""></script></head><body><h1>World</h1></body></html>'464        )465      })466467      it('should be omitted "blocking", "onLoad" and "onError" props', () => {468        const template = (469          <html>470            <head></head>471            <body>472              <script473                src='script.js'474                async={true}475                onLoad={() => {}}476                onError={() => {}}477                blocking='render'478              />479              <h1>World</h1>480            </body>481          </html>482        )483        expect(template.toString()).toBe(484          '<html><head><script src="script.js" async=""></script></head><body><h1>World</h1></body></html>'485        )486      })487488      it('should not do special behavior if async is not present', () => {489        const template = (490          <html>491            <head></head>492            <body>493              <script src='script.js' />494              <h1>World</h1>495            </body>496          </html>497        )498        expect(template.toString()).toBe(499          '<html><head></head><body><script src="script.js"></script><h1>World</h1></body></html>'500        )501      })502    })503504    describe('style element', () => {505      it('should be hoisted style tag', async () => {506        const template = (507          <html>508            <head></head>509            <body>510              <style href='red' precedence='default'>511                {'body { color: red; }'}512              </style>513              <h1>World</h1>514            </body>515          </html>516        )517        expect(template.toString()).toBe(518          '<html><head><style data-href="red" data-precedence="default">body { color: red; }</style></head><body><h1>World</h1></body></html>'519        )520      })521522      it('should be sorted by precedence attribute', async () => {523        const template = (524          <html>525            <head></head>526            <body>527              <style href='red' precedence='default'>528                {'body { color: red; }'}529              </style>530              <style href='green' precedence='high'>531                {'body { color: green; }'}532              </style>533              <style href='blue' precedence='default'>534                {'body { color: blue; }'}535              </style>536              <h1>World</h1>537            </body>538          </html>539        )540        expect(template.toString()).toBe(541          '<html><head><style data-href="red" data-precedence="default">body { color: red; }</style><style data-href="blue" data-precedence="default">body { color: blue; }</style><style data-href="green" data-precedence="high">body { color: green; }</style></head><body><h1>World</h1></body></html>'542        )543      })544545      it('should not be hoisted if href is not present', () => {546        const template = (547          <html>548            <head></head>549            <body>550              <style>{'body { color: red; }'}</style>551              <h1>World</h1>552            </body>553          </html>554        )555        expect(template.toString()).toBe(556          '<html><head></head><body><style>body { color: red; }</style><h1>World</h1></body></html>'557        )558      })559    })560  })561562  describe('form element', () => {563    it('should be omitted "action" prop if it is a function', () => {564      const template = (565        <html>566          <head></head>567          <body>568            <form action={() => {}} method='get'>569              <input type='text' />570            </form>571          </body>572        </html>573      )574      expect(template.toString()).toBe(575        '<html><head></head><body><form method="get"><input type="text"/></form></body></html>'576      )577    })578579    it('should be rendered permalink', () => {580      const [, action] = useActionState(() => {}, {}, 'permalink')581      const template = (582        <html>583          <head></head>584          <body>585            <form action={action} method='get'>586              <input type='text' />587            </form>588          </body>589        </html>590      )591      expect(template.toString()).toBe(592        '<html><head></head><body><form action="permalink" method="get"><input type="text"/></form></body></html>'593      )594    })595596    it('should not do special behavior if action is a string', () => {597      const template = (598        <html>599          <head></head>600          <body>601            <form action='/entries' method='get'>602              <input type='text' />603            </form>604          </body>605        </html>606      )607      expect(template.toString()).toBe(608        '<html><head></head><body><form action="/entries" method="get"><input type="text"/></form></body></html>'609      )610    })611612    it('should not do special behavior if no action prop', () => {613      const template = (614        <html>615          <head></head>616          <body>617            <form>618              <input type='text' />619            </form>620          </body>621        </html>622      )623      expect(template.toString()).toBe(624        '<html><head></head><body><form><input type="text"/></form></body></html>'625      )626    })627628    describe('input element', () => {629      it('should be rendered as is', () => {630        const template = <input type='text' />631        expect(template.toString()).toBe('<input type="text"/>')632      })633634      it('should be omitted "formAction" prop if it is a function', () => {635        const template = (636          <html>637            <head></head>638            <body>639              <input type='text' formAction={() => {}} />640            </body>641          </html>642        )643        expect(template.toString()).toBe(644          '<html><head></head><body><input type="text"/></body></html>'645        )646      })647648      it('should be rendered permalink', () => {649        const [, formAction] = useActionState(() => {}, {}, 'permalink')650        const template = (651          <html>652            <head></head>653            <body>654              <input type='text' formAction={formAction} />655            </body>656          </html>657        )658        expect(template.toString()).toBe(659          '<html><head></head><body><input type="text" formaction="permalink"/></body></html>'660        )661      })662    })663  })664})665666export {}