dom.test.tsx
This file
Path
src/jsx/hooks/dom.test.tsx
859 lines 路 27.2 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
1/** @jsxImportSource ../ */2import { JSDOM } from 'jsdom'3import {4 createRef,5 forwardRef,6 startTransition,7 startViewTransition,8 use,9 useDebugValue,10 useDeferredValue,11 useId,12 useImperativeHandle,13 useLayoutEffect,14 useReducer,15 useState,16 useSyncExternalStore,17 useTransition,18 useViewTransition,19} from '.'20// run tests by old style jsx default21// hono/jsx/jsx-runtime and hono/jsx/dom/jsx-runtime are tested in their respective settings22import { ErrorBoundary, Suspense, render } from '../dom'2324describe('Hooks', () => {25 beforeAll(() => {26 global.requestAnimationFrame = (cb) => setTimeout(cb)27 })2829 let dom: JSDOM30 let root: HTMLElement31 beforeEach(() => {32 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {33 runScripts: 'dangerously',34 })35 global.document = dom.window.document36 global.HTMLElement = dom.window.HTMLElement37 global.SVGElement = dom.window.SVGElement38 global.Text = dom.window.Text39 root = document.getElementById('root') as HTMLElement40 })4142 describe('useReducer()', () => {43 it('simple', async () => {44 const reducer = (state: number, action: number) => state + action45 const functions: Function[] = []46 const App = () => {47 const [state, dispatch] = useReducer(reducer, 0)48 functions.push(dispatch)49 return (50 <div>51 <button onClick={() => dispatch(1)}>{state}</button>52 </div>53 )54 }55 render(<App />, root)56 expect(root.innerHTML).toBe('<div><button>0</button></div>')57 root.querySelector('button')?.click()58 await Promise.resolve()59 expect(root.innerHTML).toBe('<div><button>1</button></div>')60 expect(functions[0]).toBe(functions[1])61 })62 })6364 describe('startTransition()', () => {65 it('no error', async () => {66 const App = () => {67 const [count, setCount] = useState(0)68 return (69 <Suspense fallback={<div>Loading...</div>}>70 <div>71 <button72 onClick={() => {73 startTransition(() => {74 setCount((c) => c + 1)75 })76 }}77 >78 {count}79 </button>80 </div>81 </Suspense>82 )83 }84 render(<App />, root)85 expect(root.innerHTML).toBe('<div><button>0</button></div>')86 root.querySelector('button')?.click()87 await Promise.resolve()88 expect(root.innerHTML).toBe('<div><button>1</button></div>')89 })9091 it('got an error', async () => {92 let resolve: () => void93 const promise = new Promise<void>((r) => (resolve = r))9495 const Counter = ({ count }: { count: number }) => {96 use(promise)97 return <div>{count}</div>98 }99100 const App = () => {101 const [count, setCount] = useState(0)102 return (103 <Suspense fallback={<div>Loading...</div>}>104 <div>105 <button106 onClick={() => {107 startTransition(() => {108 setCount((c) => c + 1)109 })110 }}111 >112 {count ? <Counter count={count} /> : count}113 </button>114 </div>115 </Suspense>116 )117 }118 render(<App />, root)119 expect(root.innerHTML).toBe('<div><button>0</button></div>')120 root.querySelector('button')?.click()121 expect(root.innerHTML).toBe('<div><button>0</button></div>')122 resolve!()123 await new Promise((r) => setTimeout(r))124 expect(root.innerHTML).toBe('<div><button><div>1</div></button></div>')125 })126 })127128 describe('useTransition()', () => {129 it('pending', async () => {130 let called = 0131 const App = () => {132 const [count, setCount] = useState(0)133 const [isPending, startTransition] = useTransition()134 called++135136 return (137 <div>138 <button139 onClick={() => {140 startTransition(() => {141 setCount((c) => c + 1)142 })143 }}144 >145 {isPending ? 'Pending...' : count}146 </button>147 </div>148 )149 }150 render(<App />, root)151 expect(root.innerHTML).toBe('<div><button>0</button></div>')152 root.querySelector('button')?.click()153 await Promise.resolve()154 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')155 expect(called).toBe(2)156 await new Promise((r) => setTimeout(r))157 await new Promise((r) => setTimeout(r))158 await new Promise((r) => setTimeout(r))159 expect(root.innerHTML).toBe('<div><button>1</button></div>')160 expect(called).toBe(3)161 })162163 it('pending', async () => {164 let resolve: (() => void) | undefined165 const promise = new Promise<void>((r) => (resolve = r))166 let called = 0167 const App = () => {168 const [isPending, startTransition] = useTransition()169 called++170171 return (172 <div>173 <button174 onClick={() => {175 startTransition(async () => await promise)176 }}177 >178 {isPending ? 'Pending...' : 'Click me'}179 </button>180 </div>181 )182 }183 render(<App />, root)184 expect(root.innerHTML).toBe('<div><button>Click me</button></div>')185 root.querySelector('button')?.click()186 await Promise.resolve()187 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')188 expect(called).toBe(2)189 resolve!()190 await new Promise((r) => setTimeout(r))191 await new Promise((r) => setTimeout(r))192 await new Promise((r) => setTimeout(r))193 expect(root.innerHTML).toBe('<div><button>Click me</button></div>')194 expect(called).toBe(3)195 })196197 it('pending - error', async () => {198 let reject: (() => void) | undefined199 const promise = new Promise<void>((_, r) => (reject = r))200 let called = 0201 const Component = () => {202 const [isPending, startTransition] = useTransition()203 called++204205 return (206 <div>207 <button208 onClick={() => {209 startTransition(async () => await promise)210 }}211 >212 {isPending ? 'Pending...' : 'Click me'}213 </button>214 </div>215 )216 }217 const App = () => (218 <ErrorBoundary fallback={<div>Error</div>}>219 <Component />220 </ErrorBoundary>221 )222 render(<App />, root)223 expect(root.innerHTML).toBe('<div><button>Click me</button></div>')224 root.querySelector('button')?.click()225 await Promise.resolve()226 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')227 expect(called).toBe(2)228 reject!()229 await new Promise((r) => setTimeout(r))230 await new Promise((r) => setTimeout(r))231 await new Promise((r) => setTimeout(r))232 expect(root.innerHTML).toBe('<div>Error</div>')233 expect(called).toBe(2)234 })235236 it('multiple setState at once', async () => {237 let called = 0238 const App = () => {239 const [count1, setCount1] = useState(0)240 const [count2, setCount2] = useState(0)241 const [isPending, startTransition] = useTransition()242 called++243244 return (245 <div>246 <button247 onClick={() => {248 startTransition(() => {249 setCount1((c) => c + 1)250 setCount2((c) => c + 2)251 })252 }}253 >254 {isPending ? 'Pending...' : count1 + count2}255 </button>256 </div>257 )258 }259 render(<App />, root)260 expect(root.innerHTML).toBe('<div><button>0</button></div>')261 root.querySelector('button')?.click()262 await Promise.resolve()263 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')264 expect(called).toBe(2)265 await new Promise((r) => setTimeout(r))266 await new Promise((r) => setTimeout(r))267 await new Promise((r) => setTimeout(r))268 expect(root.innerHTML).toBe('<div><button>3</button></div>')269 expect(called).toBe(3)270 })271272 it('multiple startTransaction at once', async () => {273 let called = 0274 const App = () => {275 const [count1, setCount1] = useState(0)276 const [count2, setCount2] = useState(0)277 const [isPending, startTransition] = useTransition()278 called++279280 return (281 <div>282 <button283 onClick={() => {284 startTransition(() => {285 setCount1((c) => c + 1)286 })287 startTransition(() => {288 setCount2((c) => c + 2)289 })290 }}291 >292 {isPending ? 'Pending...' : count1 + count2}293 </button>294 </div>295 )296 }297 render(<App />, root)298 expect(root.innerHTML).toBe('<div><button>0</button></div>')299 expect(called).toBe(1)300 root.querySelector('button')?.click()301 await Promise.resolve()302 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')303 expect(called).toBe(2)304 await new Promise((r) => setTimeout(r))305 await new Promise((r) => setTimeout(r))306 await new Promise((r) => setTimeout(r))307 expect(root.innerHTML).toBe('<div><button>3</button></div>')308 expect(called).toBe(3) // + isPending=true + isPending=false309 })310 })311312 describe('useDeferredValue()', () => {313 it('deferred', async () => {314 const promiseMap = {} as Record<number, Promise<number>>315 const getPromise = (count: number) => {316 return (promiseMap[count] ||= new Promise((r) => setTimeout(() => r(count + 1))))317 }318 const ShowCount = ({ count }: { count: number }) => {319 if (count === 0) {320 return <div>0</div>321 }322323 const c = use(getPromise(count))324 return <div>{c}</div>325 }326327 const App = () => {328 const [count, setCount] = useState(0)329 const c = useDeferredValue(count)330 return (331 <>332 <div>333 <button onClick={() => setCount((c) => c + 1)}>+1</button>334 </div>335 <Suspense fallback={<div>Loading...</div>}>336 <ShowCount count={c} />337 </Suspense>338 </>339 )340 }341 render(<App />, root)342 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')343 root.querySelector('button')?.click()344 await Promise.resolve()345 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')346 await new Promise((r) => setTimeout(r))347 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')348 await new Promise((r) => setTimeout(r))349 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')350 await new Promise((r) => setTimeout(r))351 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')352 await new Promise((r) => setTimeout(r))353 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>2</div>')354 })355356 it('initial value', async () => {357 const promiseMap = {} as Record<number, Promise<number>>358 const getPromise = (count: number) => {359 return (promiseMap[count] ||= new Promise((r) => setTimeout(() => r(count + 1))))360 }361 const ShowCount = ({ count }: { count: number }) => {362 if (count === 0 || count === 99) {363 return <div>{count}</div>364 }365366 const c = use(getPromise(count))367 return <div>{c}</div>368 }369370 const App = () => {371 const [count, setCount] = useState(1)372 const c = useDeferredValue(count, 99)373 return (374 <>375 <div>376 <button onClick={() => setCount((c) => c + 1)}>+1</button>377 </div>378 <Suspense fallback={<div>Loading...</div>}>379 <ShowCount count={c} />380 </Suspense>381 </>382 )383 }384 render(<App />, root)385 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>99</div>')386 await new Promise((r) => setTimeout(r))387 await new Promise((r) => setTimeout(r))388 await new Promise((r) => setTimeout(r))389 await new Promise((r) => setTimeout(r))390 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>2</div>')391 root.querySelector('button')?.click()392 await new Promise((r) => setTimeout(r))393 await new Promise((r) => setTimeout(r))394 await new Promise((r) => setTimeout(r))395 await new Promise((r) => setTimeout(r))396 expect(root.innerHTML).toBe('<div><button>+1</button></div><div>3</div>')397 })398 })399400 describe('startViewTransition()', () => {401 afterEach(() => {402 // eslint-disable-next-line @typescript-eslint/no-explicit-any403 delete (dom.window.document as any).startViewTransition404 })405406 it('supported browser', async () => {407 // eslint-disable-next-line @typescript-eslint/no-explicit-any408 ;(dom.window.document as any).startViewTransition = vi.fn((cb: Function) => {409 Promise.resolve().then(() => cb())410 return { finished: Promise.resolve() }411 })412413 const App = () => {414 const [count, setCount] = useState(0)415 return (416 <Suspense fallback={<div>Loading...</div>}>417 <div>418 <button419 onClick={() => {420 startViewTransition(() => {421 setCount((c) => c + 1)422 })423 }}424 >425 {count}426 </button>427 </div>428 </Suspense>429 )430 }431 render(<App />, root)432 expect(root.innerHTML).toBe('<div><button>0</button></div>')433 root.querySelector('button')?.click()434 await Promise.resolve()435 expect(root.innerHTML).toBe('<div><button>0</button></div>')436 await Promise.resolve() // updated in microtask437 expect(root.innerHTML).toBe('<div><button>1</button></div>')438 })439440 it('unsupported browser', async () => {441 const App = () => {442 const [count, setCount] = useState(0)443 return (444 <Suspense fallback={<div>Loading...</div>}>445 <div>446 <button447 onClick={() => {448 startViewTransition(() => {449 setCount((c) => c + 1)450 })451 }}452 >453 {count}454 </button>455 </div>456 </Suspense>457 )458 }459 render(<App />, root)460 expect(root.innerHTML).toBe('<div><button>0</button></div>')461 root.querySelector('button')?.click()462 await Promise.resolve()463 expect(root.innerHTML).toBe('<div><button>1</button></div>')464 })465466 it('with useTransition()', async () => {467 // eslint-disable-next-line @typescript-eslint/no-explicit-any468 ;(dom.window.document as any).startViewTransition = vi.fn((cb: Function) => {469 Promise.resolve().then(() => cb())470 return { finished: Promise.resolve() }471 })472473 let called = 0474 const App = () => {475 const [count, setCount] = useState(0)476 const [isPending, startTransition] = useTransition()477 called++478479 return (480 <div>481 <button482 onClick={() => {483 startViewTransition(() => {484 startTransition(() => {485 setCount((c) => c + 1)486 })487 })488 }}489 >490 {isPending ? 'Pending...' : count}491 </button>492 </div>493 )494 }495 render(<App />, root)496 expect(root.innerHTML).toBe('<div><button>0</button></div>')497 root.querySelector('button')?.click()498 await new Promise((r) => setTimeout(r))499 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')500 expect(called).toBe(2)501 await new Promise((r) => setTimeout(r))502 await new Promise((r) => setTimeout(r))503 expect(root.innerHTML).toBe('<div><button>1</button></div>')504 expect(called).toBe(3)505 })506 })507508 describe('useViewTransition()', () => {509 afterEach(() => {510 // eslint-disable-next-line @typescript-eslint/no-explicit-any511 delete (dom.window.document as any).startViewTransition512 })513514 it('supported browser', async () => {515 let resolved: (() => void) | undefined516 const promise = new Promise<void>((r) => (resolved = r))517 let called = 0518 // eslint-disable-next-line @typescript-eslint/no-explicit-any519 ;(global.document as any).startViewTransition = vi.fn((cb: Function) => {520 Promise.resolve().then(() => cb())521 return { finished: promise }522 })523524 const App = () => {525 const [count, setCount] = useState(0)526 const [isUpdating, startViewTransition] = useViewTransition()527 called++528529 return (530 <div>531 <button532 onClick={() => {533 startViewTransition(() => {534 setCount((c) => c + 1)535 })536 }}537 >538 {isUpdating ? 'Pending...' : count}539 </button>540 </div>541 )542 }543 render(<App />, root)544 expect(root.innerHTML).toBe('<div><button>0</button></div>')545 root.querySelector('button')?.click()546 await new Promise((r) => setTimeout(r))547 expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')548 expect(called).toBe(2)549 resolved?.()550 await new Promise((r) => setTimeout(r))551 expect(root.innerHTML).toBe('<div><button>1</button></div>')552 expect(called).toBe(3)553 })554 })555556 describe('useId()', () => {557 let dom: JSDOM558 let root: HTMLElement559 beforeEach(() => {560 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {561 runScripts: 'dangerously',562 })563 global.document = dom.window.document564 global.HTMLElement = dom.window.HTMLElement565 global.Text = dom.window.Text566 root = document.getElementById('root') as HTMLElement567 })568569 it('simple', () => {570 const App = () => {571 const id = useId()572 return <div id={id} />573 }574 render(<App />, root)575 expect(root.innerHTML).toBe('<div id=":r0:"></div>')576 })577578 it('memoized', async () => {579 let setCount: (c: number) => void = () => {}580 const App = () => {581 const id = useId()582 const [count, _setCount] = useState(0)583 setCount = _setCount584 return <div id={id}>{count}</div>585 }586 render(<App />, root)587 expect(root.innerHTML).toBe('<div id=":r1:">0</div>')588 setCount(1)589 await Promise.resolve()590 expect(root.innerHTML).toBe('<div id=":r1:">1</div>')591 })592 })593594 describe('useDebugValue()', () => {595 it('simple', () => {596 const spy = vi.fn()597 const App = () => {598 useDebugValue('hello', spy)599 return <div />600 }601 render(<App />, root)602 expect(root.innerHTML).toBe('<div></div>')603 expect(spy).not.toBeCalled()604 })605 })606607 describe('createRef()', () => {608 it('simple', () => {609 const ref: { current: HTMLElement | null } = createRef<HTMLDivElement>()610 const App = () => {611 return <div ref={ref} />612 }613 render(<App />, root)614 expect(root.innerHTML).toBe('<div></div>')615 expect(ref.current).toBeInstanceOf(HTMLElement)616 })617 })618619 describe('forwardRef()', () => {620 it('simple', () => {621 const ref: { current: HTMLElement | null } = createRef<HTMLDivElement>()622 const App = forwardRef((props, ref) => {623 return <div {...props} ref={ref} />624 })625 render(<App ref={ref} />, root)626 expect(root.innerHTML).toBe('<div></div>')627 expect(ref.current).toBeInstanceOf(HTMLElement)628 })629630 it('can run without ref', () => {631 const App = forwardRef((props) => {632 return <div {...props} />633 })634 render(<App />, root)635 expect(root.innerHTML).toBe('<div></div>')636 })637 })638639 describe('useImperativeHandle()', () => {640 it('simple', async () => {641 const ref: { current: { focus: () => void } | null } = createRef()642 const SubApp = () => {643 useImperativeHandle(644 ref,645 () => ({646 focus: () => {647 console.log('focus')648 },649 }),650 []651 )652 return <div />653 }654 const App = () => {655 const [show, setShow] = useState(true)656 return (657 <>658 {show && <SubApp />}659 <button onClick={() => setShow((s) => !s)}>toggle</button>660 </>661 )662 }663 render(<App />, root)664 expect(ref.current).toBe(null)665 await new Promise((r) => setTimeout(r))666 expect(ref.current).toEqual({ focus: expect.any(Function) })667 root.querySelector('button')?.click()668 await new Promise((r) => setTimeout(r))669 expect(ref.current).toBe(null)670 })671 })672673 describe('useSyncExternalStore()', () => {674 it('simple', async () => {675 let count = 0676 const unsubscribe = vi.fn()677 const subscribe = vi.fn(() => unsubscribe)678 const getSnapshot = vi.fn(() => count)679 const SubApp = () => {680 const count = useSyncExternalStore(subscribe, getSnapshot)681 return <div>{count}</div>682 }683 const App = () => {684 const [show, setShow] = useState(true)685 return (686 <>687 {show && <SubApp />}688 <button onClick={() => setShow((s) => !s)}>toggle</button>689 </>690 )691 }692 render(<App />, root)693 expect(root.innerHTML).toBe('<div>0</div><button>toggle</button>')694 await new Promise((r) => setTimeout(r))695 root.querySelector('button')?.click()696 await new Promise((r) => setTimeout(r))697 expect(root.innerHTML).toBe('<button>toggle</button>')698 expect(unsubscribe).toBeCalled()699 count = 1700 root.querySelector('button')?.click()701 await new Promise((r) => setTimeout(r))702 expect(root.innerHTML).toBe('<div>1</div><button>toggle</button>')703 })704705 it('updates the snapshot when subscribe changes', async () => {706 const unsubscribeA = vi.fn()707 const subscribeA = vi.fn(() => unsubscribeA)708 const unsubscribeB = vi.fn()709 const subscribeB = vi.fn(() => unsubscribeB)710 const stores = [711 { subscribe: subscribeA, getSnapshot: () => 'a1' },712 { subscribe: subscribeB, getSnapshot: () => 'b0' },713 ]714715 const App = () => {716 const [index, setIndex] = useState(0)717 const store = stores[index]718 const value = useSyncExternalStore(store.subscribe, store.getSnapshot)719 return <button onClick={() => setIndex(1)}>{value}</button>720 }721722 render(<App />, root)723 await new Promise((r) => setTimeout(r))724 expect(root.innerHTML).toBe('<button>a1</button>')725 expect(subscribeA).toBeCalledTimes(1)726727 root.querySelector('button')?.click()728 await Promise.resolve()729 expect(root.innerHTML).toBe('<button>b0</button>')730 // the old store stays subscribed until the new subscription is set up at effect flush731 expect(unsubscribeA).not.toBeCalled()732 await new Promise((r) => setTimeout(r))733 expect(unsubscribeA).toBeCalledTimes(1)734 expect(subscribeB).toBeCalledTimes(1)735 expect(unsubscribeB).not.toBeCalled()736 })737738 it('updates the snapshot when getSnapshot changes', async () => {739 const state: Record<string, string> = { a: 'a0', b: 'b0' }740 const listeners = new Set<() => void>()741 const subscribe = vi.fn((listener: () => void) => {742 listeners.add(listener)743 return () => listeners.delete(listener)744 })745746 const App = () => {747 const [key, setKey] = useState('a')748 const value = useSyncExternalStore(subscribe, () => state[key])749 return <button onClick={() => setKey('b')}>{value}</button>750 }751752 render(<App />, root)753 await new Promise((r) => setTimeout(r))754 expect(root.innerHTML).toBe('<button>a0</button>')755 expect(subscribe).toBeCalledTimes(1)756757 root.querySelector('button')?.click()758 await new Promise((r) => setTimeout(r))759 expect(root.innerHTML).toBe('<button>b0</button>')760 expect(subscribe).toBeCalledTimes(1)761762 state.b = 'b1'763 listeners.forEach((listener) => listener())764 await new Promise((r) => setTimeout(r))765 expect(root.innerHTML).toBe('<button>b1</button>')766 })767768 it('uses the latest getSnapshot when subscribing after a layout effect update', async () => {769 const state = { a: 'a0', b: 'b0' }770 const subscribe = vi.fn((_listener: () => void) => vi.fn())771772 const App = () => {773 const [key, setKey] = useState<keyof typeof state>('a')774 const value = useSyncExternalStore(subscribe, () => state[key])775776 useLayoutEffect(() => {777 setKey('b')778 queueMicrotask(() => {779 state.b = 'b1'780 })781 }, [])782783 return <div>{value}</div>784 }785786 render(<App />, root)787 expect(root.innerHTML).toBe('<div>a0</div>')788789 await new Promise((r) => setTimeout(r))790 expect(root.innerHTML).toBe('<div>b1</div>')791 })792793 it('does not loop when subscribe changes on every render', async () => {794 const unsubscribes: ReturnType<typeof vi.fn>[] = []795 const subscribe = vi.fn((_listener: () => void) => {796 const unsubscribe = vi.fn()797 unsubscribes.push(unsubscribe)798 return unsubscribe799 })800 const getSnapshot = () => 'snapshot'801 let renderCount = 0802803 const App = () => {804 const [count, setCount] = useState(0)805 renderCount++806 const value = useSyncExternalStore((listener) => subscribe(listener), getSnapshot)807 return <button onClick={() => setCount((count) => count + 1)}>{`${value}:${count}`}</button>808 }809810 render(<App />, root)811 await new Promise((r) => setTimeout(r))812 expect(root.innerHTML).toBe('<button>snapshot:0</button>')813 expect(renderCount).toBe(1)814 expect(subscribe).toBeCalledTimes(1)815816 root.querySelector('button')?.click()817 await Promise.resolve()818 await new Promise((r) => setTimeout(r))819 expect(root.innerHTML).toBe('<button>snapshot:1</button>')820 expect(renderCount).toBe(2)821 expect(subscribe).toBeCalledTimes(2)822 expect(unsubscribes[0]).toBeCalledTimes(1)823 expect(unsubscribes[1]).not.toBeCalled()824 })825826 it('with getServerSnapshot', async () => {827 let count = 0828 const unsubscribe = vi.fn()829 const subscribe = vi.fn(() => unsubscribe)830 const getSnapshot = vi.fn(() => count)831 const getServerSnapshot = vi.fn(() => 100)832 const SubApp = () => {833 const count = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)834 return <div>{count}</div>835 }836 const App = () => {837 const [show, setShow] = useState(true)838 return (839 <>840 {show && <SubApp />}841 <button onClick={() => setShow((s) => !s)}>toggle</button>842 </>843 )844 }845 render(<App />, root)846 expect(root.innerHTML).toBe('<div>100</div><button>toggle</button>')847 await new Promise((r) => setTimeout(r))848 expect(root.innerHTML).toBe('<div>0</div><button>toggle</button>')849 root.querySelector('button')?.click()850 await new Promise((r) => setTimeout(r))851 expect(root.innerHTML).toBe('<button>toggle</button>')852 expect(unsubscribe).toBeCalled()853 count = 1854 root.querySelector('button')?.click()855 await new Promise((r) => setTimeout(r))856 expect(root.innerHTML).toBe('<div>1</div><button>toggle</button>')857 })858 })859})