Skip to content

test(ui): migrate test mocks from hooks to @/client/sdk.gen - #6933

Draft
luizhf42 wants to merge 3 commits into
masterfrom
test/ui/fix-generated-client-mocks
Draft

test(ui): migrate test mocks from hooks to @/client/sdk.gen#6933
luizhf42 wants to merge 3 commits into
masterfrom
test/ui/fix-generated-client-mocks

Conversation

@luizhf42

Copy link
Copy Markdown
Member

Warning

WIP — covers 20 of ~63 test files. The remaining ~43 mock hooks (not the barrel) and need deeper refactoring where each hook mock is traced to its underlying SDK call. This branch will incrementally receive the remaining changes.

What

Migrated 20 test files from mocking @/client barrel or @/hooks/use* to mocking @/client/sdk.gen directly, which is the correct seam for the generated SDK layer.

Why

The @/client barrel mock replaces the entire module, but generated *Options helpers import from ../sdk.gen internally — so barrel mocks don't intercept them. Mocking @/client/sdk.gen with importOriginal ensures all code paths hit the mock.
Part of shellhub-io/team#216.

Changes

  • test helpers: consolidated sdkError.ts into makeSdkError, renamed userAuth.ts to factories.ts, added mockNamespace/mockTag factories and shared mockTags helper
  • barrel imports: moved all hook imports from internal submodules (@tanstack/react-query.gen, types.gen) to the barrel (@/client)
  • 6 component tests: migrated TagFilterDropdown, TagsPopover, TagsSection, FormTagsSelector, BillingWarning, UserMenu from hook mocks to sdk.gen mocks; replaced useHasPermission mocks with useAuthStore.setState({ role })
  • 14 page/hook tests: migrated ConfirmAccount, ForgotPassword, Login, MfaRecover, Setup, SignUp, UpdatePassword, Authentication, SamlConfigDrawer, Profile, SSHApproval, Settings, useDevicePolling, useSessionRecording from barrel mocks to sdk.gen mocks; removed banner comments and as never casts

Remaining (~43 files)

  • 35 page tests that mock multiple hooks (devices, containers, admin, firewall, keys, etc.)
  • 13 hook tests that mock generated *Options/*Mutation/*QueryKey factories + paginatedQueryFn
  • 1 hook test (useChatwoot) that mocks other hooks

@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

Code Review Complete

The automated review ran but did not post an updated summary — this usually means no new issues were found since the previous review. If you've pushed changes and want a fresh pass, comment /review.

View job

Replace `vi.mock("@/client")` and `vi.mock("@/hooks/use*")` with
`vi.mock("@/client/sdk.gen")` so real generated factories and
`paginatedQueryFn` run through the mocked SDK layer. Drop redundant
`vi.mock("@/api/pagination")` and `vi.mock("@/api/errors")` calls.
Replace `vi.mock("@/hooks/use*")` with `vi.mock("@/client/sdk.gen")` so
real hooks run through React Query against the mocked SDK layer. Add
shared tag factories in `mockTags.ts` and extend `factories.ts`.
Replace hook mocks (`vi.mock("@/hooks/useXxx")`) with SDK-level mocks
(`vi.mock("@/client/sdk.gen")`) across 16 page test files so the real
hooks execute and only the network boundary is faked.

Key changes beyond the mock-seam swap:

- Shared `paginatedResponse` helper in `@/tests/sdk.ts` replaces the
  repeated `mockSdkResponse(data, { "X-Total-Count": "N" })` pattern
- `mockTags` from `@/tests/mockTags` reused in RuleDrawer and KeyDrawer
- Factories: `mockDevice`/`mockSession` compose (shared uid, tenant_id),
  no `as` casts, all required fields filled
- `useHasPermission` mocks → `useAuthStore.setState({ role })` (3 files)
- `useSessionRecording` mock → `getSessionRecord` SDK mock
- Imports moved above `vi.mock()` blocks (vitest hoists automatically)
- `getBy` → `findBy` for assertions on async-rendered data
@luizhf42
luizhf42 force-pushed the test/ui/fix-generated-client-mocks branch from a5fa94b to f93b3df Compare August 21, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant