Open History to see the last commit
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}`]
10}
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 )
30}
31β
32export function createQueryClient(config?: QueryClientConfig): QueryClient {
33 jest.spyOn(console, 'error').mockImplementation(() => undefined)
34 return new QueryClient({ logger: mockLogger, ...config })
35}
36β
37export function mockVisibilityState(value: DocumentVisibilityState) {
38 return jest.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
39}
40β
41export function mockNavigatorOnLine(value: boolean) {
42 return jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
43}
44β
45export const mockLogger = {
46 log: jest.fn(),
47 warn: jest.fn(),
48 error: jest.fn(),
49}
50β
51export function sleep(timeout: number): Promise<void> {
52 return new Promise((resolve, _reject) => {
53 setTimeout(resolve, timeout)
54 })
55}
56β
57export function setActTimeout(fn: () => void, ms?: number) {
58 return setTimeout(() => {
59 fn()
60 }, ms)
61}
62β
63/**
64 * Assert the parameter is of a specific type.
65 */
66export function expectType<T>(_: T): void {
67 return undefined
68}
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
75}