Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.tsx

58 lines1.4 KB

1import { act, render } from '@testing-library/preact'
2import type { ComponentChildren, VNode } from 'preact'
3import { useEffect, useState } from 'preact/hooks'
4import { vi } from 'vitest'
5import type { MockInstance } from 'vitest'
6​
7import { QueryClientProvider, onlineManager } from '..'
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
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</>
45​
46export function mockOnlineManagerIsOnline(
47 value: boolean,
48): MockInstance<() => boolean> {
49 return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
51​
52export function setActTimeout(fn: () => void, ms?: number) {
53 return setTimeout(() => {
54 act(() => {
55 fn()
56 })
57 }, ms)