Open History to see the last commit
1import { environmentManager } from '@tanstack/query-core'
2import { act, render } from '@testing-library/preact'
3import { useEffect, useState } from 'preact/hooks'
4import { vi } from 'vitest'
5import { QueryClientProvider, onlineManager } from '..'
6import type { ComponentChildren, VNode } from 'preact'
7import type { MockInstance } from 'vitest'
8import type { QueryClient } from '..'
9β
10export function renderWithClient(
11 client: QueryClient,
12 ui: VNode,
13): ReturnType<typeof render> {
14 const { rerender, ...result } = render(
15 <QueryClientProvider client={client}>{ui}</QueryClientProvider>,
16 )
17 return {
18 ...result,
19 rerender: (rerenderUi: VNode) =>
20 rerender(
21 <QueryClientProvider client={client}>{rerenderUi}</QueryClientProvider>,
22 ),
23 } as any
24}
25β
26export function Blink({
27 duration,
28 children,
29}: {
30 duration: number
31 children: ComponentChildren
32}) {
33 const [shouldShow, setShouldShow] = useState<boolean>(true)
34β
35 useEffect(() => {
36 setShouldShow(true)
37 const timeout = setActTimeout(() => setShouldShow(false), duration)
38 return () => {
39 clearTimeout(timeout)
40 }
41 }, [duration, children])
42β
43 return shouldShow ? <>{children}</> : <>off</>
44}
45β
46export function mockOnlineManagerIsOnline(
47 value: boolean,
48): MockInstance<() => boolean> {
49 return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
50}
51β
52export function setActTimeout(fn: () => void, ms?: number) {
53 return setTimeout(() => {
54 act(() => {
55 fn()
56 })
57 }, ms)
58}
59β
60export function setIsServer(value: boolean) {
61 const originalIsServer = environmentManager.isServer()
62 environmentManager.setIsServer(() => value)
63 return () => {
64 environmentManager.setIsServer(() => originalIsServer)
65 }
66}