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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150/** @jsxImportSource ../ */import { JSDOM } from 'jsdom'// run tests by old style jsx default// hono/jsx/jsx-runtime and hono/jsx/dom/jsx-runtime are tested in their respective settingsimport { render } from '.'import type { JSXNode } from '..'import { Style, createCssContext, css, rawCssString } from '../../helper/css'import { minify } from '../../helper/css/common'import { renderTest } from '../../helper/css/common.case.test'
describe('Style and css for jsx/dom', () => { beforeAll(() => { global.requestAnimationFrame = (cb) => setTimeout(cb) })
let dom: JSDOM let root: HTMLElement beforeEach(() => { dom = new JSDOM('<html><body><div id="root"></div></body></html>', { runScripts: 'dangerously', }) global.document = dom.window.document global.HTMLElement = dom.window.HTMLElement global.SVGElement = dom.window.SVGElement global.Text = dom.window.Text root = document.getElementById('root') as HTMLElement })
it('<Style />', async () => { const App = () => { return ( <div> <Style /> <div class={css` color: red; `} > red </div> </div> ) } render(<App />, root) expect(root.innerHTML).toBe( '<div><style id="hono-css"></style><div class="css-3142110215">red</div></div>' ) await Promise.resolve() expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe( '.css-3142110215 {color: red;}' ) })
it('<Style nonce="1234" />', async () => { const App = () => { return ( <div> <Style nonce='1234' /> </div> ) } render(<App />, root) expect(root.innerHTML).toBe('<div><style id="hono-css" nonce="1234"></style></div>') })
it('<Style>{css`global`}</Style>', async () => { const App = () => { return ( <div> <Style>{css` color: red; `}</Style> <div class={css` color: red; `} > red </div> </div> ) } render(<App />, root) expect(root.innerHTML).toBe( '<div><style id="hono-css">color:red</style><div class="css-3142110215">red</div></div>' ) })
it('classNameSlug with createCssContext', async () => { const { css: customCss, Style: CustomStyle } = createCssContext({ id: 'custom-dom', classNameSlug: (hash, label) => (label ? `h-${label}` : hash), }) const App = () => { return ( <div> <CustomStyle /> <div class={customCss` /* hero */ color: blue; `} > blue </div> </div> ) } render(<App />, root) expect(root.innerHTML).toBe( '<div><style id="custom-dom"></style><div class="h-hero">blue</div></div>' ) await Promise.resolve() expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe('.h-hero {color: blue;}') })})
describe('render', () => { renderTest(() => { const cssContext = createCssContext({ id: 'hono-css' })
const dom = new JSDOM('<html><body><div id="root"></div></body></html>', { runScripts: 'dangerously', }) global.document = dom.window.document global.HTMLElement = dom.window.HTMLElement global.Text = dom.window.Text const root = document.getElementById('root') as HTMLElement
const toString = async (node: JSXNode) => { render(node, root) await Promise.resolve() const style = root.querySelector('style') if (style) { style.textContent = minify( [...(style.sheet?.cssRules || [])].map((r) => r.cssText).join('') || '' ) } return root.innerHTML }
return { toString, rawCssString, ...cssContext, support: { nest: false }, } })})