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

utils.tsx

75 lines1.9 KB

1import { QueryClient } from '@tanstack/query-core'
2import { Show, createEffect, createSignal, onCleanup } from 'solid-js'
3import type { QueryClientConfig } from '@tanstack/query-core'
4import type { ParentProps } from 'solid-js'
5​
6let queryKeyCount = 0
7export function queryKey(): () => Array<string> {
8 const localQueryKeyCount = queryKeyCount++
9 return () => [`query_${localQueryKeyCount}`]
11​
12export const Blink = (
13 props: {
14 duration: number
15 } & ParentProps,
16) => {
17 const [shouldShow, setShouldShow] = createSignal<boolean>(true)
18​
19 createEffect(() => {
20 setShouldShow(true)
21 const timeout = setActTimeout(() => setShouldShow(false), props.duration)
22 onCleanup(() => clearTimeout(timeout))
23 })
24​
25 return (
26 <Show when={shouldShow()} fallback={<>off</>}>
27 <>{props.children}</>
28 </Show>
29 )
31​
32export function createQueryClient(config?: QueryClientConfig): QueryClient {
33 jest.spyOn(console, 'error').mockImplementation(() => undefined)
34 return new QueryClient({ logger: mockLogger, ...config })
36​
37export function mockVisibilityState(value: DocumentVisibilityState) {
38 return jest.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
40​
41export function mockNavigatorOnLine(value: boolean) {
42 return jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
44​
45export const mockLogger = {
46 log: jest.fn(),
47 warn: jest.fn(),
48 error: jest.fn(),
50​
51export function sleep(timeout: number): Promise<void> {
52 return new Promise((resolve, _reject) => {
53 setTimeout(resolve, timeout)
54 })
56​
57export function setActTimeout(fn: () => void, ms?: number) {
58 return setTimeout(() => {
59 fn()
60 }, ms)
62​
63/**
64 * Assert the parameter is of a specific type.
65 */
66export function expectType<T>(_: T): void {
67 return undefined
69​
70/**
71 * Assert the parameter is not typed as `any`
72 */
73export function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void {
74 return undefined