fix(test): type MetricsBar's useAsync mock against the real hook shape (Closes #429) - #437
Open
AnnieIj wants to merge 1 commit into
Open
fix(test): type MetricsBar's useAsync mock against the real hook shape (Closes #429)#437AnnieIj wants to merge 1 commit into
AnnieIj wants to merge 1 commit into
Conversation
Closes AnchorNet-Org#429) MetricsBar.test.tsx mocked useAsync with only { state, refresh }, omitting reload and mutate, so tsc reported TS2345 at all four call sites and the component was verified against a contract the real hook never returns. Replace the hand-written mocks with a typed mockUseAsync factory whose return type is ReturnType<typeof useAsync>; a future change to the hook's shape now breaks compilation instead of silently testing a fabricated interface. No as any, @ts-expect-error or Partial cast used. MetricsBar itself only consumes state and refresh (reload/mutate are intentionally unused: the manual refresh must stay silent, which is refresh's contract), so no behavioural gap was found. Repo-wide tsc error count drops from 9 to 5; the remaining 5 are the out-of-scope SettlementTable.test.tsx errors. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #429 by making
MetricsBar.test.tsxmockuseAsyncagainst the hook's real return type instead of a fabricated{ state, refresh }shape.The four errors before (and the repo-wide count)
npx tsc --noEmitreported 9error TSlines onmain:src/components/MetricsBar.test.tsx(21,41),(34,41),(47,41),(64,41)—TS2345: ... is missing the following properties from type ...: reload, mutatesrc/components/SettlementTable.test.tsx(72,19),(269,21),(270,21),(271,21),(272,21)— out of scope per the issueAfter this PR: 5 errors (repo-wide count drops by exactly 4), and
grep MetricsBaron the tsc output is empty. The remaining 5 are the out-of-scopeSettlementTable.test.tsxerrors.Which hook members MetricsBar actually uses
Only
stateandrefresh.src/components/MetricsBar.tsxdestructures exactly those two (const { state, refresh } = useAsync(load)). The manual "Refresh" button callsrefresh(), and the auto-refresh interval drivesrefreshviauseInterval.Mock-factory decision
Added an in-file
mockUseAsync(overrides)factory whose return type isReturnType<typeof useAsync>— i.e. the full four-member contract (state,reload,refresh,mutate) — with no-op defaults and per-test overrides:as any,@ts-expect-error, orPartial<>cast on the mock was used. ThePartial<UseAsyncResult>is only the override input to the factory; the factory always returns the complete, fully-typed result.useAsync; if that changes, it should be promoted to a shared test util.Behavioural gap
None found.
reload/mutateare legitimately unused byMetricsBar: a manual reload must stay silent (existing data stays visible, per the test suite's contract), which is exactlyrefresh's job —reload()would flash the loading skeleton, andmutateis for optimistic local updates this component doesn't do. The mock now includes them as no-ops so the component is tested against the contract it actually receives.Assertion changes
None. Two mock-honesty cleanups only (no assertion semantics changed):
mockReloadlocal tomockRefresh(it was always passed asrefresh).pendingSettlementsto the ready-state mock data so it matches the realMetricsshape the component consumes.Notes
mainwas ~a month behind the org repo, so the branch was rebased onto the org's currentmain(feb9b67) before fixing; the diff contains onlyMetricsBar.test.tsx.tsc --noEmitstep (the add a typecheck #418 "add a typecheck" change was reverted to unblock merge); this drift was invisible for that reason. This PR fixes the drift itself; a CI typecheck gate remains the separate tracked issue.useAsync's own tests are a separate issue; the factory here pins the four-member contract (state,reload,refresh,mutate) those tests should assert.Checklist
CHANGELOG.mdentry under the next## [x.y.z]section(see the Format note at the top of
CHANGELOG.md), or this PRis docs-only / test-only / internal tooling and doesn't change
user-facing behavior. — test-only, no entry needed per the changelog's own note.
Closes #429