Skip to content
Myr

common.case.test.tsx

This file

Path

src/helper/css/common.case.test.tsx

506 lines 路 14.8 KiB 路 TypeScript

Last changed by

Not known.

History of this file

Take it

1/** @jsxImportSource ../../jsx */2import type {3  Style as StyleComponent,4  css as cssHelper,5  keyframes as keyframesHelper,6  rawCssString as rawCssStringHelper,7  viewTransition as viewTransitionHelper,8} from './index'910interface Support {11  nest: boolean12}13export const renderTest = (14  getEnv: () => {15    css: typeof cssHelper16    keyframes: typeof keyframesHelper17    viewTransition: typeof viewTransitionHelper18    rawCssString: typeof rawCssStringHelper19    // eslint-disable-next-line @typescript-eslint/no-explicit-any20    toString: (template: any) => Promise<string>21    Style: typeof StyleComponent22    support: Support23  }24) => {25  const { support } = getEnv()2627  let css: typeof cssHelper28  let keyframes: typeof keyframesHelper29  let viewTransition: typeof viewTransitionHelper30  let rawCssString: typeof rawCssStringHelper31  // eslint-disable-next-line @typescript-eslint/no-explicit-any32  let toString: (template: any) => Promise<string>33  let Style: typeof StyleComponent34  beforeEach(() => {35    ;({ css, keyframes, viewTransition, rawCssString, toString, Style } = getEnv())36  })3738  describe('render css', () => {39    it('Should render CSS styles with JSX', async () => {40      const headerClass = css`41        background-color: blue;42      `43      const template = (44        <>45          <Style />46          <h1 class={headerClass}>Hello!</h1>47        </>48      )49      expect(await toString(template)).toBe(50        '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'51      )52    })5354    it('Should render CSS with keyframes', async () => {55      const animation = keyframes`56        from {57          opacity: 0;58        }59        to {60          opacity: 1;61        }62      `63      const headerClass = css`64        background-color: blue;65        animation: ${animation} 1s ease-in-out;66      `67      const template = (68        <>69          <Style />70          <h1 class={headerClass}>Hello!</h1>71        </>72      )73      expect(await toString(template)).toBe(74        '<style id="hono-css">.css-1580801783{background-color:blue;animation:css-9294673 1s ease-in-out}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-1580801783">Hello!</h1>'75      )76    })7778    it('Should not output the same class name multiple times.', async () => {79      const headerClass = css`80        background-color: blue;81      `82      const headerClass2 = css`83        background-color: blue;84      `85      const template = (86        <>87          <Style />88          <h1 class={headerClass}>Hello!</h1>89          <h1 class={headerClass2}>Hello2!</h1>90        </>91      )92      expect(await toString(template)).toBe(93        '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1><h1 class="css-2458908649">Hello2!</h1>'94      )95    })9697    it('Should render CSS with variable', async () => {98      const headerClass = css`99        background-color: blue;100        content: '${"I'm a variable!"}';101      `102      const template = (103        <>104          <Style />105          <h1 class={headerClass}>Hello!</h1>106        </>107      )108      expect(await toString(template)).toBe(109        '<style id="hono-css">.css-4027435072{background-color:blue;content:\'I\\\'m a variable!\'}</style><h1 class="css-4027435072">Hello!</h1>'110      )111    })112113    it('Should escape </style>', async () => {114      const headerClass = css`115        background-color: blue;116        content: '${'</style>'}';117      `118      const template = (119        <>120          <Style />121          <h1 class={headerClass}>Hello!</h1>122        </>123      )124      expect(await toString(template)).toBe(125        '<style id="hono-css">.css-372954897{background-color:blue;content:\'<\\/style>\'}</style><h1 class="css-372954897">Hello!</h1>'126      )127    })128129    it('Should not escape URL', async () => {130      const headerClass = css`131        background-color: blue;132        background: url('${'http://www.example.com/path/to/file.jpg'}');133      `134      const template = (135        <>136          <Style />137          <h1 class={headerClass}>Hello!</h1>138        </>139      )140      expect(await toString(template)).toBe(141        '<style id="hono-css">.css-1321888780{background-color:blue;background:url(\'http://www.example.com/path/to/file.jpg\')}</style><h1 class="css-1321888780">Hello!</h1>'142      )143    })144145    it('Should render CSS with escaped variable', async () => {146      const headerClass = css`147        background-color: blue;148        content: '${rawCssString('say "Hello!"')}';149      `150      const template = (151        <>152          <Style />153          <h1 class={headerClass}>Hello!</h1>154        </>155      )156      expect(await toString(template)).toBe(157        '<style id="hono-css">.css-2238574885{background-color:blue;content:\'say "Hello!"\'}</style><h1 class="css-2238574885">Hello!</h1>'158      )159    })160161    it('Should render CSS with number', async () => {162      const headerClass = css`163        background-color: blue;164        font-size: ${1}rem;165      `166      const template = (167        <>168          <Style />169          <h1 class={headerClass}>Hello!</h1>170        </>171      )172      expect(await toString(template)).toBe(173        '<style id="hono-css">.css-1847536026{background-color:blue;font-size:1rem}</style><h1 class="css-1847536026">Hello!</h1>'174      )175    })176177    it('Should render CSS with array', async () => {178      const animation = keyframes`179        from {180          opacity: 0;181        }182        to {183          opacity: 1;184        }185      `186      const headerClass = css`187        background-color: blue;188        animation: ${animation} 1s ease-in-out;189      `190      const extendedHeaderClass = css`191        ${headerClass}192        color: red;193      `194      const template = (195        <>196          <Style />197          <h1 class={extendedHeaderClass}>Hello!</h1>198        </>199      )200      expect(await toString(template)).toBe(201        '<style id="hono-css">.css-2558359670{background-color:blue;animation:css-9294673 1s ease-in-out;color:red}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-2558359670">Hello!</h1>'202      )203    })204205    it.runIf(support.nest)(206      'Should be used as a class name for syntax `${className} {`',207      async () => {208        const headerClass = css`209          font-weight: bold;210        `211        const containerClass = css`212          ${headerClass} {213            h1 {214              color: red;215            }216          }217        `218        const template = (219          <>220            <Style />221            <div class={containerClass}>222              <h1 class={headerClass}>Hello!</h1>223            </div>224          </>225        )226        expect(await toString(template)).toBe(227          '<style id="hono-css">.css-4220297002{.css-1032195302{h1{color:red}}}.css-1032195302{font-weight:bold}</style><div class="css-4220297002"><h1 class="css-1032195302">Hello!</h1></div>'228        )229      }230    )231232    it('Should be inserted to global if style string starts with :-hono-root', async () => {233      const globalClass = css`234        :-hono-global {235          html {236            color: red;237          }238          body {239            display: flex;240          }241        }242      `243      const template = (244        <>245          <Style />246          <div class={globalClass}>247            <h1>Hello!</h1>248          </div>249        </>250      )251      expect(await toString(template)).toBe(252        '<style id="hono-css">html{color:red}body{display:flex}</style><div class=""><h1>Hello!</h1></div>'253      )254    })255256    it.runIf(support.nest)(257      'Should be inserted to global if style string starts with :-hono-root and extends class name',258      async () => {259        const headerClass = css`260          display: flex;261        `262        const specialHeaderClass = css`263          :-hono-global {264            ${headerClass} {265              h1 {266                color: red;267              }268            }269          }270        `271        const template = (272          <>273            <Style />274            <div class={specialHeaderClass}>275              <h1>Hello!</h1>276            </div>277          </>278        )279        expect(await toString(template)).toBe(280          '<style id="hono-css">.css-3980466870{h1{color:red}}.css-3980466870{display:flex}</style><div class="css-3980466870"><h1>Hello!</h1></div>'281        )282      }283    )284285    it('Should be inserted as global css if passed css`` to Style component', async () => {286      const headerClass = css`287        font-size: 1rem;288      `289      const template = (290        <>291          <Style>{css`292            html {293              color: red;294            }295            body {296              display: flex;297            }298          `}</Style>299          <div>300            <h1 class={headerClass}>Hello!</h1>301          </div>302        </>303      )304      expect(await toString(template)).toBe(305        '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'306      )307    })308309    it('Should be ignored :-hono-root inside Style component', async () => {310      const headerClass = css`311        font-size: 1rem;312      `313      const template = (314        <>315          <Style>{css`316            :-hono-global {317              html {318                color: red;319              }320              body {321                display: flex;322              }323            }324          `}</Style>325          <div>326            <h1 class={headerClass}>Hello!</h1>327          </div>328        </>329      )330      expect(await toString(template)).toBe(331        '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'332      )333    })334335    describe('viewTransition', () => {336      it('Should render CSS with unique view-transition-name', async () => {337        const transition = viewTransition()338        const template = (339          <>340            <Style />341            <h1 class={transition}>Hello!</h1>342          </>343        )344        expect(await toString(template)).toBe(345          '<style id="hono-css">.css-1644952339{view-transition-name:css-1644952339}</style><h1 class="css-1644952339">Hello!</h1>'346        )347      })348349      it('Should render CSS with css and keyframes', async () => {350        const kf = keyframes`351        from {352          opacity: 0;353        }354        to {355          opacity: 1;356        }357      `358        const transition = viewTransition(css`359          ::view-transition-old() {360            animation-name: ${kf};361          }362          ::view-transition-new() {363            animation-name: ${kf};364          }365        `)366        const headerClass = css`367          ${transition}368          background-color: blue;369        `370        const template = (371          <>372            <Style />373            <h1 class={headerClass}>Hello!</h1>374          </>375        )376        expect(await toString(template)).toBe(377          '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'378        )379      })380381      it('Should works as a template tag function', async () => {382        const kf = keyframes`383        from {384          opacity: 0;385        }386        to {387          opacity: 1;388        }389      `390        const transition = viewTransition`391          ::view-transition-old() {392            animation-name: ${kf};393          }394          ::view-transition-new() {395            animation-name: ${kf};396          }397        `398        const headerClass = css`399          ${transition}400          background-color: blue;401        `402        const template = (403          <>404            <Style />405            <h1 class={headerClass}>Hello!</h1>406          </>407        )408        expect(await toString(template)).toBe(409          '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'410        )411      })412    })413414    it.runIf(support.nest)('Should render sub CSS with keyframe', async () => {415      const headerClass = css`416        background-color: blue;417        ${[1, 2].map(418          (i) => css`419            :nth-child(${i}) {420              color: red;421            }422          `423        )}424      `425      const template = (426        <>427          <Style />428          <h1 class={headerClass}>Hello!</h1>429        </>430      )431      expect(await toString(template)).toBe(432        '<style id="hono-css">.css-1539881271{background-color:blue;:nth-child(1){color:red}:nth-child(2){color:red}}</style><h1 class="css-1539881271">Hello!</h1>'433      )434    })435436    it('Should be generated deferent class name for deferent first line comment even if the content is the same', async () => {437      const headerClassA = css`438        /* class A */439        display: flex;440      `441      const headerClassB = css`442        /* class B */443        display: flex;444      `445      const template = (446        <>447          <Style />448          <h1 class={headerClassA}>Hello!</h1>449          <h1 class={headerClassB}>Hello!</h1>450        </>451      )452      expect(await toString(template)).toBe(453        '<style id="hono-css">.css-3170754153{display:flex}.css-896513246{display:flex}</style><h1 class="css-3170754153">Hello!</h1><h1 class="css-896513246">Hello!</h1>'454      )455    })456457    describe('Booleans, Null, and Undefined Are Ignored', () => {458      it.each([true, false, undefined, null])('%s', async (value) => {459        const headerClass = css`460          ${value}461          background-color: blue;462        `463        const template = (464          <>465            <Style />466            <h1 class={headerClass}>Hello!</h1>467          </>468        )469        expect(await toString(template)).toBe(470          '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'471        )472      })473474      it('falsy value', async () => {475        const value = 0476        const headerClass = css`477          padding: ${value};478        `479        const template = (480          <>481            <Style />482            <h1 class={headerClass}>Hello!</h1>483          </>484        )485        expect(await toString(template)).toBe(486          '<style id="hono-css">.css-478287868{padding:0}</style><h1 class="css-478287868">Hello!</h1>'487        )488      })489490      it('Should render CSS styles with CSP nonce', async () => {491        const headerClass = css`492          background-color: blue;493        `494        const template = (495          <>496            <Style nonce='1234' />497            <h1 class={headerClass}>Hello!</h1>498          </>499        )500        expect(await toString(template)).toBe(501          '<style id="hono-css" nonce="1234">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'502        )503      })504    })505  })506}