Skip to content

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

Merged
otavio merged 4 commits into
masterfrom
test/ui/fix-generated-client-mocks
Aug 24, 2026
Merged

test(ui): migrate test mocks from hooks to @/client/sdk.gen#6933
otavio merged 4 commits into
masterfrom
test/ui/fix-generated-client-mocks

Conversation

@luizhf42

@luizhf42 luizhf42 commented Aug 20, 2026

Copy link
Copy Markdown
Member

What

Migrated all 93 test files from mocking the @/client barrel or @/hooks/use* to mocking @/client/sdk.gen directly, and consolidated the per-file mock boilerplate into a shared mockSdkGen helper.

Closes shellhub-io/team#216.

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. Moving the vi.mock factory into a global helper (mockSdkGen) eliminates ~1200 lines of duplicated boilerplate across 93 files.

Changes

Shared test infrastructure (src/tests/)

  • mockSdkGen global (defined in setup.ts, typed in globals.d.ts): receives an object of {sdkFn: vi.fn()} entries, calls vi.doMock internally, returns the mocks. Usage: const sdk = vi.hoisted(() => mockSdkGen({...})) — one call replaces the per-file vi.hoisted + vi.mock boilerplate
  • seedAuthStore(overrides?): seeds the Zustand auth store from mockUserAuth() factory data; replaces all useAuthStore custom mocks and useHasPermission mocks
  • paginatedResponse(items[], total?): wraps list data with X-Total-Count header; replaces 6 local paginatedSdkResponse helpers
  • createTestWrapper(opts?): QueryClientProvider + optional MemoryRouter with retry: false; replaces ~25 createWrapper()/makeWrapper() copies
  • ~20 factories in factories.ts: mockDevice, mockContainer, mockNamespace, mockTag, mockFirewallRule, mockPublicKey, mockWebEndpoint, mockAccessPolicy, mockSubscription, mockCustomer, mockLicense, mockInvitation, mockServiceAccount, mockSshIdentity, mockInstallKey, mockUserAuth, mockAnnouncementFull, mockStats

Mock seam migration (93 files)

  • Hook/store tests (19 files): migrated from @/client barrel mocks to @/client/sdk.gen via mockSdkGen
  • Component tests (26 files): replaced useDevices, useNamespaces, useHasPermission, useBilling, useStats, useLatestAnnouncement, etc. with SDK-level mocks + seedAuthStore()
  • Page tests (48 files): replaced hook mocks with SDK-level mocks; fixed AcceptDevice from inline sync factory to mockSdkGen pattern

Cleanup

  • Removed all as never casts — mock data satisfies types via factories
  • Removed banner/changelog comments
  • Consolidated 6 duplicate paginatedSdkResponse definitions
  • Fixed useInvitations.test.ts relative import (../../client@/tests/sdk)

What stays mocked at the hook level

Pure utility hooks with no API call are still mocked directly: useDeviceActions, useContainerActions, useSidebarLayout, useOtpInput, useCopy, useResetOnOpen, useDebouncedValue, useBrowserKey. Non-TQ store mocks also stay: useTerminalStore, useVaultStore, signUpStore.

@luizhf42 luizhf42 self-assigned this Aug 20, 2026
@luizhf42
luizhf42 requested a review from a team as a code owner August 20, 2026 20:51
@luizhf42
luizhf42 marked this pull request as draft August 20, 2026 20:51
@luizhf42
luizhf42 force-pushed the test/ui/fix-generated-client-mocks branch 2 times, most recently from f93b3df to 8ae3351 Compare August 24, 2026 11:56
Add entity factories (`mockDevice`, `mockContainer`, `mockNamespace`,
etc.), `seedAuthStore` for populating the auth Zustand store from
factory data, `mockTags`/`mockNamespaces` helpers, and the
`paginatedResponse` SDK helper to `src/tests/`.
@luizhf42
luizhf42 force-pushed the test/ui/fix-generated-client-mocks branch from 8ae3351 to c2adde7 Compare August 24, 2026 17:04
@luizhf42
luizhf42 marked this pull request as ready for review August 24, 2026 17:07
@otavio

otavio commented Aug 24, 2026

Copy link
Copy Markdown
Member

/review

1 similar comment
@otavio

otavio commented Aug 24, 2026

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

Claude finished @otavio's task in 9m 35s —— View job


Code Review Complete

Reviewed the test-migration diff (100+ files) across code quality, security, testing, language patterns, and architecture — no new issues found. The code looks good as-is.

A few notes for context (not blocking, no action required):

  • The core migration mechanism is sound: vi.hoisted(() => mockSdkGen(...)) registers vi.doMock("@/client/sdk.gen", importOriginal) before the test module's static imports evaluate, correctly intercepting direct SDK calls, the @/client barrel, and the generated *Options/*Mutation wrappers — which is exactly what the old barrel mock missed.
  • mockSdkGen is registered via setupFiles in vite.config.ts and typed in src/tests/globals.d.ts (inside tsconfig's include), so it type-checks and runs before every test.
  • Minor consistency opportunities the team may want to fold in later: LicenseBanner, LicenseGuard, and DeviceLimitBanner still keep a local makeLicense helper that duplicates the new shared mockLicense factory (AdminSidebar, migrated in the same PR, already uses the shared one), and src/tests/mockNamespaces.ts is not yet imported anywhere. Neither affects correctness.

If you push additional changes and want a new review, tag @shellhub-io/admin and a team member can trigger it.
· test/ui/fix-generated-client-mocks

@otavio
otavio merged commit 2db51bb into master Aug 24, 2026
65 checks passed
@otavio
otavio deleted the test/ui/fix-generated-client-mocks branch August 24, 2026 17:47
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.

2 participants