Skip to content

Add tests for the useLocalState hook including SSR-safe hydration #15

@mikewheeleer

Description

@mikewheeleer

Test the useLocalState hook hydration and write paths

Description

src/lib/useLocalState.ts backs state with localStorage and swallows JSON parse/write errors, but it has no test coverage — the hydration effect, the malformed-JSON fallback, and the write-through behaviour are all unverified. This issue adds a test suite mirroring the style of src/lib/__tests__/useDebounce.test.tsx.

Requirements and context

  • Repository scope: Agentpay-Org/Agentpay-frontend only.
  • Assert: initial value is the fallback before hydration, the stored value is read after the effect, write updates both state and localStorage, and malformed stored JSON falls back without throwing.
  • Use a real or mocked window.localStorage; clear it between tests.
  • Cover the try/catch branches that currently /* ignore */.

Suggested execution

  • Fork the repo and create a branch
  • git checkout -b test/testing-16-uselocalstate
  • Implement changes
    • Write comprehensive tests in: create src/lib/__tests__/useLocalState.test.tsx.
    • Add documentation: none beyond test descriptions.
    • Validate no act() warnings and deterministic teardown.
  • Test and commit

Test and commit

  • Run npm run lint, npm run typecheck, and npm test.
  • Cover edge cases: missing key, corrupt JSON, and a setItem throw (quota) being swallowed.
  • Include the npm test output and the coverage summary for the hook.

Example commit message

test(lib): cover useLocalState hydration, write-through, and error fallbacks

Guidelines

  • Minimum 95 percent test coverage for useLocalState.ts.
  • Clear, reviewer-focused documentation.
  • Timeframe: 96 hours.

Community & contribution rewards

  • 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
  • ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions