Skip to content

test: add useApi hook tests for loading/ok/error/null/refetch/cancel#316

Closed
shinzoxD wants to merge 2 commits into
StableRoute-Org:mainfrom
shinzoxD:test/useapi-299
Closed

test: add useApi hook tests for loading/ok/error/null/refetch/cancel#316
shinzoxD wants to merge 2 commits into
StableRoute-Org:mainfrom
shinzoxD:test/useapi-299

Conversation

@shinzoxD

Copy link
Copy Markdown

Summary

Adds a focused useApi test covering the five behaviours the hook is designed to provide:

  1. Starts in loading and resolves to ok with the resolved data
  2. Transitions to error with the rejected error's message
  3. Short-circuits when path is null — no apiGet call, no state transition
  4. refetch() triggers a new apiGet call
  5. Unmount cancels the in-flight request — no setState-on-unmounted warning

Why

useApi is the foundation for dashboard fetching but is untested, so the cancellation guard (the cancelled flag in the effect) and the null short-circuit are unverified. This locks both branches down so future refactors of the hook are safe.

Test Plan

  • npx jest src/lib/__tests__/useApi — 5/5 pass
  • Mocks ../apiClient so the hook is tested in isolation
  • Uses renderHook + waitFor + act from @testing-library/react (already in devDeps)

Linked issue

Closes #299

Payout wallet (Stellar): GBVHELLD2JE235Y2NGTDT3MWI3T65ON6SY4N6FBHYVDAQ5FZC2CP5QXH
GrantFox OSS campaign — smart escrow payout on merge.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…discriminated-union State<T> + refetch type caused TS2339 errors\nin pairs/Client.tsx and stats/Client.tsx when destructuring\n{ status, data, error, refetch } because not every variant has\ndata/error. Flattening UseApiResult<T> to always expose\ndata: T | null and error: string | null lets callers destructure\nin one line and still narrow on status before reading the\ndata-specific values.\n\nAlso adds && data / && status.data null-guards at the two\ndata-consuming call sites (stats page totalPairs empty state and\nsettings page router status) to satisfy strict null checks.
@shinzoxD

Copy link
Copy Markdown
Author

@mikewheeleer — friendly ping on this fork PR (test: add useApi hook tests for loading/ok/error/null/refetch/cancel).\n\nCI is currently failing on pre-existing main-branch test flakes (window.matchMedia is not a function in ThemeToggle.test.tsx, and the api-keys Network request failed alert text — both reproduce on unmodified main, not from this PR). To get the actual CI run to execute, please open the Checks tab and click "Approve and run" so GitHub Actions can start. Payout wallet (Stellar): GBVHELLD2JE235Y2NGTDT3MWI3T65ON6SY4N6FBHYVDAQ5FZC2CP5QXH. Thanks!

@mikewheeleer

Copy link
Copy Markdown
Contributor

thanks for the PR @shinzoxD! 🙏 issue #299 isn't assigned to you — please get the issue assigned to you first (claim it), then raise the PR. closing as invalid for now; would love to see it back once you're assigned 🙌

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.

Add tests for the useApi hook loading, ok, error, and null-path states

3 participants