Skip to content

test: consolidate duplicated live-query queryFn into a shared helper#144

Open
mbret wants to merge 1 commit into
mainfrom
chore/consolidation-2026-07-20
Open

test: consolidate duplicated live-query queryFn into a shared helper#144
mbret wants to merge 1 commit into
mainfrom
chore/consolidation-2026-07-20

Conversation

@mbret

@mbret mbret commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Summary

A behavior-preserving consolidation of duplicated live-query test scaffolding, in the same spirit as #137. All gates pass (biome check, tsc, vite build, vitest run — 129 tests).

Consolidation 1 — repeated live-query queryFn pipe

What was duplicated: the identical Observable queryFn pipe appeared 10 times across src/lib/queries/useQuery$.reactivity.test.tsx (6×) and src/lib/queries/useQuery$.unmount.test.tsx (4×):

queryFn: () =>
  db$.pipe(
    filter(isDefined),
    switchMap(() => liveQuery$),
    map((items) => [...items]),
  ),

What it became: a single createLiveQueryFn(db$, liveQuery$) helper in the shared src/tests/liveQuery.tsx, invoked at every call site as queryFn: createLiveQueryFn(db$, liveQuery$).

Why they're the same concept: every live-query suite models the same shape — a db$ gate that must be defined before the query yields, feeding a liveQuery$ list whose latest items are emitted. The pipe was byte-identical in all 10 sites; only the surrounding queryKey and assertions differ, and those stay inline.

Consolidation 2 — duplicated isDefined

What was duplicated: src/tests/liveQuery.tsx defined its own isDefined type-guard, a reimplementation of the library's src/lib/utils/isDefined.ts (runtime-identical null/undefined check).

What it became: the test helper now imports isDefined from ../lib/utils/isDefined (and re-exports it to keep the module's surface stable). The local copy is deleted.

Why they're the same concept: both are the "value is not null/undefined" guard; there is no reason for the test tree to carry a second implementation.

Net LOC

34 insertions, 67 deletionsnet −33 lines across 3 files.

Notes / opportunities left for a future sweep

  • The Host toggle harness (let toggle + useState visibility wrapper) is repeated 4× in useQuery$.unmount.test.tsx, but each copy closes over a locally-defined List component, so extracting it cleanly needs a small factory and was left out to keep this PR's story tight.

🤖 Generated with Claude Code


Generated by Claude Code

The live-query test suites (useQuery$.reactivity and useQuery$.unmount)
repeated the exact same Observable queryFn pipe in 10 places:

    db$.pipe(
      filter(isDefined),
      switchMap(() => liveQuery$),
      map((items) => [...items]),
    )

Extract it into `createLiveQueryFn` in the shared test helper so both
suites build it the same way. Also drop the local `isDefined` copy in
liveQuery.tsx, which duplicated the library's `isDefined` util, and
source it from `../lib/utils/isDefined` instead.

Behavior-preserving: the generated queryFn and its emissions are
identical; only the call sites changed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PMgtP4GREPoEq9LdTBcHky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants