refactor: domain table hook architecture separation#296
refactor: domain table hook architecture separation#296harishsundar-okta wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #296 +/- ##
==========================================
+ Coverage 89.47% 89.49% +0.02%
==========================================
Files 156 156
Lines 13070 13102 +32
Branches 1419 1798 +379
==========================================
+ Hits 11694 11726 +32
Misses 1376 1376 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…nto refactor/domain-table-hook-architecture
| vi.mock('@/hooks/my-organization/shared/services/use-domain-table-service', () => ({ | ||
| useDomainTableService: vi.fn(() => ({ | ||
| domains: [], | ||
| providers: [], | ||
| isFetching: false, | ||
| isCreating: false, | ||
| isDeleting: false, | ||
| isVerifying: false, | ||
| isLoadingProviders: false, | ||
| fetchProviders: vi.fn(), | ||
| fetchDomains: vi.fn(), | ||
| onCreateDomain: vi.fn(), | ||
| onVerifyDomain: vi.fn(), | ||
| onDeleteDomain: vi.fn(), | ||
| onAssociateToProvider: vi.fn(), | ||
| onDeleteFromProvider: vi.fn(), | ||
| })), | ||
| })); |
There was a problem hiding this comment.
const createMockServiceReturn = (overrides = {}) => ({
domains: [],
providers: [],
isFetching: false,
isCreating: false,
isDeleting: false,
isVerifying: false,
isLoadingProviders: false,
fetchProviders: vi.fn(),
fetchDomains: vi.fn(),
onCreateDomain: vi.fn(),
onVerifyDomain: vi.fn(),
onDeleteDomain: vi.fn(),
onAssociateToProvider: vi.fn(),
onDeleteFromProvider: vi.fn(),
...overrides,
});
// Get mocked function reference once
import { useDomainTableService } from '@/hooks/my-organization/shared/services/use-domain-table-service';
const mockUseDomainTableService = vi.mocked(useDomainTableService);
// In tests - much cleaner:
it('should create domain...', async () => {
const mockOnCreateDomain = vi.fn().mockResolvedValue(mockDomain);
mockUseDomainTableService.mockReturnValue(
createMockServiceReturn({ onCreateDomain: mockOnCreateDomain })
);
// ... test
});
can we use this pattern instead of repeating same mock object multiple places? also have the base mock in separate mocks file
There was a problem hiding this comment.
Done, created a reusable createMockDomainTableServiceReturn helper with override support.
| import { createTestQueryClientWrapper } from '@/tests/utils/test-provider'; | ||
| import type { UseDomainTableServiceOptions } from '@/types/my-organization/domain-management/domain-table-types'; | ||
|
|
||
| // ===== Mock packages ===== |
There was a problem hiding this comment.
lets not have these auto generated comments as its public repo
There was a problem hiding this comment.
Done, removed all section separator comments.
| const createMockOptions = ( | ||
| overrides?: Partial<UseDomainTableServiceOptions>, | ||
| ): UseDomainTableServiceOptions => ({ | ||
| createAction: { | ||
| onBefore: vi.fn().mockReturnValue(true), | ||
| onAfter: vi.fn(), | ||
| }, | ||
| deleteAction: { | ||
| onBefore: vi.fn().mockReturnValue(true), | ||
| onAfter: vi.fn(), | ||
| }, | ||
| verifyAction: { | ||
| onBefore: vi.fn().mockReturnValue(true), | ||
| onAfter: vi.fn(), | ||
| }, | ||
| associateToProviderAction: { | ||
| onBefore: vi.fn().mockReturnValue(true), | ||
| onAfter: vi.fn(), | ||
| }, | ||
| deleteFromProviderAction: { | ||
| onBefore: vi.fn().mockReturnValue(true), | ||
| onAfter: vi.fn(), | ||
| }, | ||
| customMessages: {}, | ||
| ...overrides, | ||
| }); |
There was a problem hiding this comment.
can we have them in separate mock file?
There was a problem hiding this comment.
Done, moved to the shared domain mocks file.
| vi.mock('@/hooks/shared/use-translator', () => ({ | ||
| useTranslator: () => ({ t: (key: string) => key }), | ||
| })); | ||
|
|
||
| vi.mock('@/hooks/shared/use-error-handler', () => ({ | ||
| useErrorHandler: () => mockHandleError, | ||
| })); | ||
|
|
||
| vi.mock('@/components/auth0/shared/toast', () => ({ | ||
| showToast: vi.fn(), | ||
| })); |
There was a problem hiding this comment.
can we use existing modular approach of mocking like
mockToast
useTranslatorModule
useErrorHandlerModule
There was a problem hiding this comment.
Done, replaced inline toast mock with mockToast() from test utilities.
|
|
||
| const domainQueryKeys = { | ||
| all: ['domains'] as const, | ||
| list: () => [...domainQueryKeys.all, 'list'] as const, |
There was a problem hiding this comment.
you we should have all the queries extracted and reuse from the core for each module
Summary
Refactors the domain table hooks to follow the hook architecture pattern — two hooks internally, only one exposed publicly.
Why
The domain table component previously required consumers to manually wire together useDomainTable (data) and useDomainTableLogic (UI state), creating a fragile and complex public API. This standardizes on a single public hook pattern, consistent with the SSO provider table implementation.
What
Packages
packages/corepackages/reactexamplesReferences
Testing
How can this be verified? Note anything intentionally not covered by tests and why.
Checklist
Contributing