From 0d6dd9d6ffa1a1e4f9c1cb91c6321c0d26ca64ab Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 19 Jul 2026 11:15:31 +0000 Subject: [PATCH] test: consolidate duplicated live-query test helpers The `useQuery$.reactivity` and `useQuery$.unmount` specs each carried a byte-identical 38-line preamble (isDefined, createQueryClient, liveQueryOptions, createWrapper). Extract the shared helpers into src/tests/liveQuery.tsx and import them in both specs. No behavior change: the helpers are moved verbatim. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01U7gH7fnCjWFBRgC15JrYtA --- src/lib/queries/useQuery$.reactivity.test.tsx | 39 +++---------------- src/lib/queries/useQuery$.unmount.test.tsx | 39 +++---------------- src/tests/liveQuery.tsx | 33 ++++++++++++++++ 3 files changed, 45 insertions(+), 66 deletions(-) create mode 100644 src/tests/liveQuery.tsx 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} + + ) + } +}