Skip to content
Myr

client.test.tsx

This file

Path

src/jsx/dom/client.test.tsx

128 lines · 3.8 KiB · TypeScript

Last changed by

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–3why, one step back
1/** @jsxImportSource ../ */
2import { JSDOM } from 'jsdom'3import { useEffect } from '.'

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 4why, one step back
4import DefaultExport, { createRoot, hydrateRoot } from './client'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 5–128why, one step back
5
6describe('createRoot', () => {7 beforeAll(() => {8 global.requestAnimationFrame = (cb) => setTimeout(cb)9 })1011 let dom: JSDOM12 let rootElement: HTMLElement13 beforeEach(() => {14 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {15 runScripts: 'dangerously',16 })17 global.document = dom.window.document18 global.HTMLElement = dom.window.HTMLElement19 global.SVGElement = dom.window.SVGElement20 global.Text = dom.window.Text21 rootElement = document.getElementById('root') as HTMLElement22 })2324 it('render / unmount', async () => {25 const cleanup = vi.fn()26 const App = () => {27 useEffect(() => cleanup, [])28 return <h1>Hello</h1>29 }30 const root = createRoot(rootElement)31 root.render(<App />)32 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')33 await new Promise((resolve) => setTimeout(resolve))34 root.unmount()35 await Promise.resolve()36 expect(rootElement.innerHTML).toBe('')37 expect(cleanup).toHaveBeenCalled()38 })3940 it('call render twice', async () => {41 const App = <h1>Hello</h1>42 const App2 = <h1>World</h1>43 const root = createRoot(rootElement)44 root.render(App)45 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')4647 const createElementSpy = vi.spyOn(dom.window.document, 'createElement')4849 root.render(App2)50 await Promise.resolve()51 expect(rootElement.innerHTML).toBe('<h1>World</h1>')5253 expect(createElementSpy).not.toHaveBeenCalled()54 })5556 it('call render after unmount', async () => {57 const App = <h1>Hello</h1>58 const App2 = <h1>World</h1>59 const root = createRoot(rootElement)60 root.render(App)61 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')62 root.unmount()63 expect(() => root.render(App2)).toThrow('Cannot update an unmounted root')64 })65})6667describe('hydrateRoot', () => {68 let dom: JSDOM69 let rootElement: HTMLElement70 beforeEach(() => {71 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {72 runScripts: 'dangerously',73 })74 global.document = dom.window.document75 global.HTMLElement = dom.window.HTMLElement76 global.SVGElement = dom.window.SVGElement77 global.Text = dom.window.Text78 rootElement = document.getElementById('root') as HTMLElement79 })8081 it('should return root object', async () => {82 const cleanup = vi.fn()83 const App = () => {84 useEffect(() => cleanup, [])85 return <h1>Hello</h1>86 }87 const root = hydrateRoot(rootElement, <App />)88 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')89 await new Promise((resolve) => setTimeout(resolve))90 root.unmount()91 await Promise.resolve()92 expect(rootElement.innerHTML).toBe('')93 expect(cleanup).toHaveBeenCalled()94 })9596 it('call render', async () => {97 const App = <h1>Hello</h1>98 const App2 = <h1>World</h1>99 const root = hydrateRoot(rootElement, App)100 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')101102 const createElementSpy = vi.spyOn(dom.window.document, 'createElement')103104 root.render(App2)105 await Promise.resolve()106 expect(rootElement.innerHTML).toBe('<h1>World</h1>')107108 expect(createElementSpy).not.toHaveBeenCalled()109 })110111 it('call render after unmount', async () => {112 const App = <h1>Hello</h1>113 const App2 = <h1>World</h1>114 const root = hydrateRoot(rootElement, App)115 expect(rootElement.innerHTML).toBe('<h1>Hello</h1>')116 root.unmount()117 expect(() => root.render(App2)).toThrow('Cannot update an unmounted root')118 })119})120121describe('default export', () => {122 ;['createRoot', 'hydrateRoot'].forEach((key) => {123 it(key, () => {124 // eslint-disable-next-line @typescript-eslint/no-explicit-any125 expect((DefaultExport as any)[key]).toBeDefined()126 })127 })128})