Skip to content

test(follow): unit tests for useFollowList query and cache behaviour #342

@DSanich

Description

@DSanich

Summary

Add tests for useFollowList (src/hooks/useFollowList.ts), which loads the current user’s kind 3 contact list from relays with followListCache (localStorage / IndexedDB). followListCache already has dedicated tests — this issue targets the hook’s query function, ordering, empty relay, and newer-vs-cache branches.

Problem

  • The hook contains non-trivial branching: no user, zero relay events with optional cache fallback, sort by created_at, isNewerThan vs relay data, extraction of p tags.
  • Production logs (debugLog) suggest this path was debugged manually — automated tests prevent regressions when relay behaviour or cache TTL logic changes.

Expected outcome

  • renderHook with mocked useNostr, useCurrentUser, and followListCache module:
    • Assert nostr.query filter: kinds: [3], authors: [user.pubkey], limit: 1.
    • When relay returns one kind 3 event: p tags parsed to pubkey list in tag order (or as implemented).
    • When relay returns []: empty array or cached follows when cache exists (match implementation).
    • When relay event is older than cache: cached follows returned (mirror isNewerThan / getCached logic).

What needs to be tested

  • Disabled / no user: query returns [] or does not call relay — match enabled semantics in source.
  • Success path: sorted events, latest wins.
  • IndexedDB effect: optionally assert followListCache.loadFromIndexedDB called when localStorage empty — may require vi.spyOn on followListCache (keep test deterministic).

Mocking strategy

  • vi.mock('@/lib/followListCache') with in-memory behaviour or spy real module with controlled return values.
  • vi.mock('@nostrify/react'){ nostr: { query: vi.fn().mockResolvedValue([...]) } }.

Acceptance criteria

  • src/hooks/useFollowList.test.ts added.
  • No real IndexedDB in unit tests unless project already uses idb mocks globally (src/test/setup.ts).
  • npm run test + npx tsc --noEmit pass.

Related files

File Role
src/hooks/useFollowList.ts Hook under test
src/lib/followListCache.ts + followListCache.test.ts Existing cache tests

Notes

  • useFollowRelationship already has tests — do not conflate; this issue is kind 3 list ingestion only.
  • Trim overly verbose debugLog in production later via separate chore if maintainers want — not required to close this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions