css.test.tsx
This file
Path
src/jsx/dom/css.test.tsx
149 lines · 3.9 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–4why, one step back1/** @jsxImportSource ../ */2import { JSDOM } from 'jsdom'3// run tests by old style jsx default4// hono/jsx/jsx-runtime and hono/jsx/dom/jsx-runtime are tested in their respective settingschore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 5why, one step back5import { render } from '.'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 6–149why, one step back6import type { JSXNode } from '..'7import { Style, createCssContext, css, rawCssString } from '../../helper/css'8import { minify } from '../../helper/css/common'9import { renderTest } from '../../helper/css/common.case.test'1011describe('Style and css for jsx/dom', () => {12 beforeAll(() => {13 global.requestAnimationFrame = (cb) => setTimeout(cb)14 })1516 let dom: JSDOM17 let root: HTMLElement18 beforeEach(() => {19 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {20 runScripts: 'dangerously',21 })22 global.document = dom.window.document23 global.HTMLElement = dom.window.HTMLElement24 global.SVGElement = dom.window.SVGElement25 global.Text = dom.window.Text26 root = document.getElementById('root') as HTMLElement27 })2829 it('<Style />', async () => {30 const App = () => {31 return (32 <div>33 <Style />34 <div35 class={css`36 color: red;37 `}38 >39 red40 </div>41 </div>42 )43 }44 render(<App />, root)45 expect(root.innerHTML).toBe(46 '<div><style id="hono-css"></style><div class="css-3142110215">red</div></div>'47 )48 await Promise.resolve()49 expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe(50 '.css-3142110215 {color: red;}'51 )52 })5354 it('<Style nonce="1234" />', async () => {55 const App = () => {56 return (57 <div>58 <Style nonce='1234' />59 </div>60 )61 }62 render(<App />, root)63 expect(root.innerHTML).toBe('<div><style id="hono-css" nonce="1234"></style></div>')64 })6566 it('<Style>{css`global`}</Style>', async () => {67 const App = () => {68 return (69 <div>70 <Style>{css`71 color: red;72 `}</Style>73 <div74 class={css`75 color: red;76 `}77 >78 red79 </div>80 </div>81 )82 }83 render(<App />, root)84 expect(root.innerHTML).toBe(85 '<div><style id="hono-css">color:red</style><div class="css-3142110215">red</div></div>'86 )87 })8889 it('classNameSlug with createCssContext', async () => {90 const { css: customCss, Style: CustomStyle } = createCssContext({91 id: 'custom-dom',92 classNameSlug: (hash, label) => (label ? `h-${label}` : hash),93 })94 const App = () => {95 return (96 <div>97 <CustomStyle />98 <div99 class={customCss`100 /* hero */101 color: blue;102 `}103 >104 blue105 </div>106 </div>107 )108 }109 render(<App />, root)110 expect(root.innerHTML).toBe(111 '<div><style id="custom-dom"></style><div class="h-hero">blue</div></div>'112 )113 await Promise.resolve()114 expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe('.h-hero {color: blue;}')115 })116})117118describe('render', () => {119 renderTest(() => {120 const cssContext = createCssContext({ id: 'hono-css' })121122 const dom = new JSDOM('<html><body><div id="root"></div></body></html>', {123 runScripts: 'dangerously',124 })125 global.document = dom.window.document126 global.HTMLElement = dom.window.HTMLElement127 global.Text = dom.window.Text128 const root = document.getElementById('root') as HTMLElement129130 const toString = async (node: JSXNode) => {131 render(node, root)132 await Promise.resolve()133 const style = root.querySelector('style')134 if (style) {135 style.textContent = minify(136 [...(style.sheet?.cssRules || [])].map((r) => r.cssText).join('') || ''137 )138 }139 return root.innerHTML140 }141142 return {143 toString,144 rawCssString,145 ...cssContext,146 support: { nest: false },147 }148 })149})