-
Notifications
You must be signed in to change notification settings - Fork 7
refactor(react): sso domain tab hook architecture separation #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harishsundar-okta
wants to merge
6
commits into
main
Choose a base branch
from
refactor/sso-domain-tab-hook-architecture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ae107e
refactor(react): sso domain tab hook architecture separation
harishsundar-okta d40bd8f
fix(react): resolve type-check errors in sso-domain-tab
harishsundar-okta a9a898f
refactor(react): move service tests to shared/__tests__
harishsundar-okta 9f40ede
refactor(react): consolidate service tests into services/__tests__
harishsundar-okta 3c26af7
chore: merge main into refactor/sso-domain-tab-hook-architecture
harishsundar-okta eb3304f
refactor(react): remove section comments from test files
harishsundar-okta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
591 changes: 141 additions & 450 deletions
591
packages/react/src/hooks/my-organization/__tests__/use-sso-domain-tab.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
305 changes: 305 additions & 0 deletions
305
...ct/src/hooks/my-organization/shared/services/__tests__/use-sso-domain-tab-service.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| import { BusinessError } from '@auth0/universal-components-core'; | ||
| import { renderHook, act, waitFor } from '@testing-library/react'; | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| import { useSsoDomainTabService } from '@/hooks/my-organization/shared/services/use-sso-domain-tab-service'; | ||
| import * as useCoreClientModule from '@/hooks/shared/use-core-client'; | ||
| import * as useErrorHandlerModule from '@/hooks/shared/use-error-handler'; | ||
| import * as useTranslatorModule from '@/hooks/shared/use-translator'; | ||
| import { | ||
| mockCore, | ||
| createMockSsoDomain, | ||
| createMockVerifiedSsoDomain, | ||
| createMockSsoProvider, | ||
| setupAllCommonMocks, | ||
| setupMockUseCoreClientNull, | ||
| } from '@/tests/utils'; | ||
| import { createTestQueryClientWrapper } from '@/tests/utils/test-provider'; | ||
|
|
||
| // ===== Mock packages ===== | ||
|
|
||
| const { initMockCoreClient } = mockCore(); | ||
|
|
||
| // Test data | ||
| const mockDomain = createMockSsoDomain(); | ||
| const mockVerifiedDomain = createMockVerifiedSsoDomain(); | ||
| const mockProvider = createMockSsoProvider(); | ||
|
|
||
| describe('useSsoDomainTabService', () => { | ||
| let mockCoreClient: ReturnType<typeof initMockCoreClient>; | ||
| let mockHandleError: ReturnType<typeof vi.fn>; | ||
|
|
||
| let mockDomainVerifyCreate: ReturnType<typeof vi.fn>; | ||
| let mockIdentityProviderDomainsCreate: ReturnType<typeof vi.fn>; | ||
| let mockIdentityProviderDomainsDelete: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| mockCoreClient = initMockCoreClient(); | ||
| mockHandleError = vi.fn(); | ||
|
|
||
| const apiService = mockCoreClient.getMyOrganizationApiClient(); | ||
|
|
||
| (apiService.organization.domains.list as ReturnType<typeof vi.fn>).mockResolvedValue({ | ||
| response: { organization_domains: [mockDomain] }, | ||
| }); | ||
| (apiService.organization.domains.create as ReturnType<typeof vi.fn>).mockResolvedValue( | ||
| mockDomain, | ||
| ); | ||
| (apiService.organization.domains.delete as ReturnType<typeof vi.fn>).mockResolvedValue({}); | ||
|
|
||
| mockDomainVerifyCreate = apiService.organization.domains.verify.create as ReturnType< | ||
| typeof vi.fn | ||
| >; | ||
| mockDomainVerifyCreate.mockResolvedValue(mockVerifiedDomain); | ||
|
|
||
| mockIdentityProviderDomainsCreate = apiService.organization.identityProviders.domains | ||
| .create as ReturnType<typeof vi.fn>; | ||
| mockIdentityProviderDomainsCreate.mockResolvedValue({}); | ||
|
|
||
| mockIdentityProviderDomainsDelete = apiService.organization.identityProviders.domains | ||
| .delete as ReturnType<typeof vi.fn>; | ||
| mockIdentityProviderDomainsDelete.mockResolvedValue({}); | ||
|
|
||
| const { mockHandleError: setupMockHandleError } = setupAllCommonMocks({ | ||
| coreClient: mockCoreClient, | ||
| useCoreClientModule, | ||
| useTranslatorModule, | ||
| useErrorHandlerModule, | ||
| }); | ||
|
|
||
| mockHandleError = setupMockHandleError; | ||
| }); | ||
|
|
||
| const defaultProvider = { ...mockProvider, domains: [mockDomain.domain] }; | ||
|
|
||
| const renderService = async ( | ||
| idpId: string, | ||
| options?: Parameters<typeof useSsoDomainTabService>[1], | ||
| ) => { | ||
| const { wrapper, queryClient } = createTestQueryClientWrapper(); | ||
| const mergedOptions = { provider: defaultProvider, ...options }; | ||
| const hook = renderHook(() => useSsoDomainTabService(idpId, mergedOptions), { wrapper }); | ||
| await waitFor(() => expect(hook.result.current.isLoading).toBe(false)); | ||
| return { queryClient, ...hook }; | ||
| }; | ||
|
|
||
| describe('domain listing', () => { | ||
| it('should fetch domains on mount', async () => { | ||
| await renderService('idp-1'); | ||
|
|
||
| expect( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.list, | ||
| ).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('should return domains list after successful load', async () => { | ||
| const { result } = await renderService('idp-1'); | ||
|
|
||
| expect(result.current.isLoading).toBe(false); | ||
| expect(result.current.domainsList).toEqual([mockDomain]); | ||
| }); | ||
|
|
||
| it('should handle domain listing error', async () => { | ||
| const error = new Error('API Error'); | ||
| ( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.list as ReturnType< | ||
| typeof vi.fn | ||
| > | ||
| ).mockRejectedValue(error); | ||
|
|
||
| const { wrapper } = createTestQueryClientWrapper(); | ||
| renderHook(() => useSsoDomainTabService('idp-1'), { wrapper }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockHandleError).toHaveBeenCalledWith(error, { | ||
| fallbackMessage: 'general_error', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should derive idpDomains from provider prop', async () => { | ||
| const { result } = await renderService('idp-1', { | ||
| provider: { ...mockProvider, domains: [mockDomain.domain] }, | ||
| }); | ||
|
|
||
| expect(result.current.idpDomains).toContain(mockDomain.id); | ||
| }); | ||
|
|
||
| it('should not fetch domains if idpId is not provided', () => { | ||
| const { wrapper } = createTestQueryClientWrapper(); | ||
| renderHook(() => useSsoDomainTabService(''), { wrapper }); | ||
|
|
||
| expect( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.list, | ||
| ).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('domain creation', () => { | ||
| it('should create domain successfully', async () => { | ||
| const { result } = await renderService('idp-1'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.createDomain({ domain: 'newdomain.com' }); | ||
| }); | ||
|
|
||
| expect( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.create, | ||
| ).toHaveBeenCalledWith({ domain: 'newdomain.com' }); | ||
| }); | ||
|
|
||
| it('should call onBefore and proceed if returns true', async () => { | ||
| const onBefore = vi.fn().mockReturnValue(true); | ||
| const onAfter = vi.fn(); | ||
|
|
||
| const { result } = await renderService('idp-1', { | ||
| domains: { createAction: { onBefore, onAfter } }, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.createDomain({ domain: 'newdomain.com' }); | ||
| }); | ||
|
|
||
| expect(onBefore).toHaveBeenCalled(); | ||
| expect(onAfter).toHaveBeenCalledWith(mockDomain); | ||
| }); | ||
|
|
||
| it('should throw BusinessError if onBefore returns false', async () => { | ||
| const onBefore = vi.fn().mockReturnValue(false); | ||
|
|
||
| const { result } = await renderService('idp-1', { | ||
| domains: { createAction: { onBefore } }, | ||
| }); | ||
|
|
||
| await expect( | ||
| act(async () => { | ||
| await result.current.createDomain({ domain: 'newdomain.com' }); | ||
| }), | ||
| ).rejects.toThrow(BusinessError); | ||
| }); | ||
| }); | ||
|
|
||
| describe('domain verification', () => { | ||
| it('should verify domain successfully', async () => { | ||
| const { result } = await renderService('idp-1'); | ||
|
|
||
| let verifyResult: { isVerified: boolean } | undefined; | ||
| await act(async () => { | ||
| verifyResult = await result.current.verifyDomain(mockDomain); | ||
| }); | ||
|
|
||
| expect(mockDomainVerifyCreate).toHaveBeenCalledWith(mockDomain.id); | ||
| expect(verifyResult?.isVerified).toBe(true); | ||
| }); | ||
|
|
||
| it('should call verification callbacks', async () => { | ||
| const onBefore = vi.fn().mockReturnValue(true); | ||
| const onAfter = vi.fn(); | ||
|
|
||
| const { result } = await renderService('idp-1', { | ||
| domains: { verifyAction: { onBefore, onAfter } }, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.verifyDomain(mockDomain); | ||
| }); | ||
|
|
||
| expect(onBefore).toHaveBeenCalledWith(mockDomain); | ||
| expect(onAfter).toHaveBeenCalledWith(mockDomain); | ||
| }); | ||
| }); | ||
|
|
||
| describe('domain deletion', () => { | ||
| it('should delete domain successfully', async () => { | ||
| const { result } = await renderService('idp-1'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.deleteDomain(mockDomain); | ||
| }); | ||
|
|
||
| expect( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.delete, | ||
| ).toHaveBeenCalledWith(mockDomain.id); | ||
| }); | ||
|
|
||
| it('should call deletion callbacks', async () => { | ||
| const onBefore = vi.fn().mockReturnValue(true); | ||
| const onAfter = vi.fn(); | ||
|
|
||
| const { result } = await renderService('idp-1', { | ||
| domains: { deleteAction: { onBefore, onAfter } }, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.deleteDomain(mockDomain); | ||
| }); | ||
|
|
||
| expect(onBefore).toHaveBeenCalledWith(mockDomain); | ||
| expect(onAfter).toHaveBeenCalledWith(mockDomain); | ||
| }); | ||
| }); | ||
|
|
||
| describe('provider association', () => { | ||
| it('should associate domain to provider', async () => { | ||
| const { result } = await renderService('idp-1', { | ||
| provider: mockProvider, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.associateToProvider(mockDomain); | ||
| }); | ||
|
|
||
| expect(mockIdentityProviderDomainsCreate).toHaveBeenCalledWith('idp-1', { | ||
| domain: mockDomain.domain, | ||
| }); | ||
| }); | ||
|
|
||
| it('should delete domain from provider', async () => { | ||
| const { result } = await renderService('idp-1', { | ||
| provider: mockProvider, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.deleteFromProvider(mockDomain); | ||
| }); | ||
|
|
||
| expect(mockIdentityProviderDomainsDelete).toHaveBeenCalledWith( | ||
| mockProvider.id, | ||
| mockDomain.domain, | ||
| ); | ||
| }); | ||
|
|
||
| it('should call provider association callbacks', async () => { | ||
| const onBefore = vi.fn().mockReturnValue(true); | ||
| const onAfter = vi.fn(); | ||
|
|
||
| const { result } = await renderService('idp-1', { | ||
| provider: mockProvider, | ||
| domains: { associateToProviderAction: { onBefore, onAfter } }, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.associateToProvider(mockDomain); | ||
| }); | ||
|
|
||
| expect(onBefore).toHaveBeenCalledWith(mockDomain, mockProvider); | ||
| expect(onAfter).toHaveBeenCalledWith(mockDomain, mockProvider); | ||
| }); | ||
| }); | ||
|
|
||
| describe('edge cases', () => { | ||
| it('should handle missing coreClient gracefully', () => { | ||
| setupMockUseCoreClientNull(useCoreClientModule); | ||
|
|
||
| const { wrapper } = createTestQueryClientWrapper(); | ||
| const { result } = renderHook(() => useSsoDomainTabService('idp-1'), { wrapper }); | ||
|
|
||
| expect( | ||
| mockCoreClient.getMyOrganizationApiClient().organization.domains.list, | ||
| ).not.toHaveBeenCalled(); | ||
| expect(result.current.domainsList).toEqual([]); | ||
| }); | ||
| }); | ||
| }); | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove these comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed all comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still we have these comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the remaining comments