diff --git a/src/lib/queries/useQuery$.reactivity.test.tsx b/src/lib/queries/useQuery$.reactivity.test.tsx index 5f62df3..192dd09 100644 --- a/src/lib/queries/useQuery$.reactivity.test.tsx +++ b/src/lib/queries/useQuery$.reactivity.test.tsx @@ -1,42 +1,15 @@ -import { QueryClient, QueryClientProvider } from "@tanstack/react-query" import { act, render, screen } from "@testing-library/react" -import type React from "react" import { BehaviorSubject, filter, map, switchMap } from "rxjs" import { describe, expect, it } from "vitest" +import { + createQueryClient, + createWrapper, + isDefined, + liveQueryOptions, +} from "../../tests/liveQuery" import { waitForTimeout } from "../../tests/utils" -import { QueryClientProvider$ } from "./QueryClientProvider$" import { useQuery$ } from "./useQuery$" -function isDefined(value: T | undefined | null): value is T { - return value != null -} - -function createQueryClient() { - return new QueryClient({ - defaultOptions: { - queries: { - gcTime: 0, - }, - }, - }) -} - -const liveQueryOptions = { - networkMode: "always" as const, - gcTime: 0, - staleTime: Number.POSITIVE_INFINITY, -} - -function createWrapper(queryClient: QueryClient) { - return function Wrapper({ children }: { children: React.ReactNode }) { - return ( - - {children} - - ) - } -} - describe("useQuery$ live-query reactivity", () => { it("re-renders when a new item is added", async () => { const liveQuery$ = new BehaviorSubject(["a", "b"]) diff --git a/src/lib/queries/useQuery$.unmount.test.tsx b/src/lib/queries/useQuery$.unmount.test.tsx index e1fb062..e553718 100644 --- a/src/lib/queries/useQuery$.unmount.test.tsx +++ b/src/lib/queries/useQuery$.unmount.test.tsx @@ -1,43 +1,16 @@ -import { QueryClient, QueryClientProvider } from "@tanstack/react-query" import { act, render, screen } from "@testing-library/react" -import type React from "react" import { useState } from "react" import { BehaviorSubject, filter, map, switchMap } from "rxjs" import { describe, expect, it } from "vitest" +import { + createQueryClient, + createWrapper, + isDefined, + liveQueryOptions, +} from "../../tests/liveQuery" import { waitForTimeout } from "../../tests/utils" -import { QueryClientProvider$ } from "./QueryClientProvider$" import { useQuery$ } from "./useQuery$" -function isDefined(value: T | undefined | null): value is T { - return value != null -} - -function createQueryClient() { - return new QueryClient({ - defaultOptions: { - queries: { - gcTime: 0, - }, - }, - }) -} - -const liveQueryOptions = { - networkMode: "always" as const, - gcTime: 0, - staleTime: Number.POSITIVE_INFINITY, -} - -function createWrapper(queryClient: QueryClient) { - return function Wrapper({ children }: { children: React.ReactNode }) { - return ( - - {children} - - ) - } -} - describe("useQuery$ unmount / remount", () => { it("stays reactive after hide/show then adding an item", { timeout: 3000, diff --git a/src/tests/liveQuery.tsx b/src/tests/liveQuery.tsx new file mode 100644 index 0000000..51f5e54 --- /dev/null +++ b/src/tests/liveQuery.tsx @@ -0,0 +1,33 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query" +import type React from "react" +import { QueryClientProvider$ } from "../lib/queries/QueryClientProvider$" + +export function isDefined(value: T | undefined | null): value is T { + return value != null +} + +export function createQueryClient() { + return new QueryClient({ + defaultOptions: { + queries: { + gcTime: 0, + }, + }, + }) +} + +export const liveQueryOptions = { + networkMode: "always" as const, + gcTime: 0, + staleTime: Number.POSITIVE_INFINITY, +} + +export function createWrapper(queryClient: QueryClient) { + return function Wrapper({ children }: { children: React.ReactNode }) { + return ( + + {children} + + ) + } +}