generated from pagopa/template-aws-infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- separete api functions - separet login data hook - separetd login buttons - unit test
- Loading branch information
Showing
16 changed files
with
628 additions
and
223 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
104 changes: 104 additions & 0 deletions
104
src/oneid/oneid-ecs-core/src/main/webui/src/hooks/useLoginData.test.tsx
This file contains 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,104 @@ | ||
import { Mock, vi } from 'vitest'; | ||
import { useLoginData } from './useLoginData'; | ||
import { fetchBannerContent, getIdpList, getClientData } from '../services/api'; | ||
import { ENV } from '../utils/env'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
vi.mock('../services/api', () => ({ | ||
fetchBannerContent: vi.fn(), | ||
getIdpList: vi.fn(), | ||
getClientData: vi.fn(), | ||
})); | ||
|
||
describe('useLoginData', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should fetch and set bannerContent, idpList, and clientData on mount', async () => { | ||
const mockBanner = [ | ||
{ enable: true, severity: 'info', description: 'Test Banner' }, | ||
]; | ||
const mockIdpList = { | ||
identityProviders: [{ entityID: 'test-idp' }], | ||
richiediSpid: 'https://example.com/spid', | ||
}; | ||
const mockClientData = { | ||
clientID: 'test-client-id', | ||
friendlyName: 'Test Client', | ||
logoUri: 'https://example.com/logo.png', | ||
policyUri: 'https://example.com/policy', | ||
tosUri: 'https://example.com/tos', | ||
}; | ||
|
||
// Mock API responses | ||
(fetchBannerContent as Mock).mockResolvedValueOnce(mockBanner); | ||
(getIdpList as Mock).mockResolvedValueOnce({ idps: mockIdpList }); | ||
(getClientData as Mock).mockResolvedValueOnce({ | ||
clientData: mockClientData, | ||
}); | ||
|
||
// Render the hook | ||
const { result } = renderHook(useLoginData); | ||
|
||
await waitFor(() => { | ||
expect(fetchBannerContent).toHaveBeenCalledWith(ENV.JSON_URL.ALERT); | ||
expect(getIdpList).toHaveBeenCalledWith(ENV.JSON_URL.IDP_LIST); | ||
expect(getClientData).toHaveBeenCalledWith(ENV.JSON_URL.CLIENT_BASE_URL); | ||
}); | ||
|
||
// Check if the state is updated correctly | ||
expect(result.current.bannerContent).toEqual(mockBanner); | ||
expect(result.current.idpList).toEqual(mockIdpList); | ||
expect(result.current.clientData).toEqual(mockClientData); | ||
}); | ||
|
||
it('should not set state if API calls fail', async () => { | ||
// Mock failed API responses | ||
(fetchBannerContent as Mock).mockRejectedValueOnce( | ||
new Error('Failed to fetch banner') | ||
); | ||
(getIdpList as Mock).mockRejectedValueOnce( | ||
new Error('Failed to fetch IDP list') | ||
); | ||
(getClientData as Mock).mockRejectedValueOnce( | ||
new Error('Failed to fetch client data') | ||
); | ||
|
||
const { result } = renderHook(useLoginData); | ||
|
||
await waitFor(() => { | ||
// Check that the state has not been set | ||
expect(result.current.bannerContent).toBeUndefined(); | ||
expect(result.current.idpList).toEqual({ | ||
identityProviders: [], | ||
richiediSpid: '', | ||
}); | ||
expect(result.current.clientData).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('should handle partial data successfully', async () => { | ||
const mockBanner = [ | ||
{ enable: true, severity: 'info', description: 'Test Banner' }, | ||
]; | ||
const mockIdpList = { | ||
identityProviders: [{ entityID: 'test-idp' }], | ||
richiediSpid: 'https://example.com/spid', | ||
}; | ||
|
||
// Mock API responses | ||
(fetchBannerContent as Mock).mockResolvedValueOnce(mockBanner); | ||
(getIdpList as Mock).mockResolvedValueOnce({ idps: mockIdpList }); | ||
(getClientData as Mock).mockResolvedValueOnce({ clientData: undefined }); // No client data | ||
|
||
const { result } = renderHook(useLoginData); | ||
|
||
await waitFor(() => { | ||
// Verify that bannerContent and idpList are correctly set | ||
expect(result.current.bannerContent).toEqual(mockBanner); | ||
expect(result.current.idpList).toEqual(mockIdpList); | ||
expect(result.current.clientData).toBeUndefined(); // clientData should be undefined | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
src/oneid/oneid-ecs-core/src/main/webui/src/hooks/useLoginData.tsx
This file contains 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,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { ENV } from '../utils/env'; | ||
import { IdentityProviders } from '../utils/IDPS'; | ||
import { | ||
type BannerContent, | ||
type Client, | ||
fetchBannerContent, | ||
getIdpList, | ||
getClientData, | ||
} from '../services/api'; | ||
|
||
export const useLoginData = () => { | ||
const [bannerContent, setBannerContent] = useState<Array<BannerContent>>(); | ||
const [idpList, setIdpList] = useState<IdentityProviders>({ | ||
identityProviders: [], | ||
richiediSpid: '', | ||
}); | ||
const [clientData, setClientData] = useState<Client>(); | ||
|
||
useEffect(() => { | ||
const bannerRequest = fetchBannerContent(ENV.JSON_URL.ALERT); | ||
const idpsRequest = getIdpList(ENV.JSON_URL.IDP_LIST); | ||
const clientDataRequest = getClientData(ENV.JSON_URL.CLIENT_BASE_URL); | ||
|
||
Promise.allSettled([idpsRequest, clientDataRequest, bannerRequest]).then( | ||
([idpsResult, clientDataResult, bannerResult]) => { | ||
if (idpsResult.status === 'fulfilled' && idpsResult.value.idps) { | ||
setIdpList(idpsResult.value.idps); | ||
} else { | ||
console.error('Failed to fetch IDP list:', idpsResult.status); | ||
} | ||
|
||
if ( | ||
clientDataResult.status === 'fulfilled' && | ||
clientDataResult.value.clientData | ||
) { | ||
setClientData(clientDataResult.value.clientData); | ||
} else { | ||
console.error( | ||
'Failed to fetch client data:', | ||
clientDataResult.status | ||
); | ||
} | ||
|
||
if (bannerResult.status === 'fulfilled' && bannerResult.value?.length) { | ||
setBannerContent(bannerResult.value); | ||
} else { | ||
console.error('Failed to fetch banner content:', bannerResult.status); | ||
} | ||
} | ||
); | ||
}, []); | ||
|
||
return { bannerContent, idpList, clientData }; | ||
}; |
This file contains 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
36 changes: 36 additions & 0 deletions
36
src/oneid/oneid-ecs-core/src/main/webui/src/pages/login/components/CieButton.test.tsx
This file contains 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,36 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { describe, it, vi } from 'vitest'; | ||
import { CieButton, CieButtonProps } from './CieButton'; | ||
import { i18nTestSetup } from '../../../__tests__/i18nTestSetup'; | ||
|
||
describe('CieButton', () => { | ||
const BUTTON_TEXT = 'CIE Login'; | ||
const onClickMock = vi.fn(); | ||
|
||
const renderComponent = (props: Partial<CieButtonProps> = {}) => { | ||
i18nTestSetup({ 'loginPage.loginBox.cieLogin': BUTTON_TEXT }); | ||
render(<CieButton onClick={onClickMock} {...props} />); | ||
}; | ||
|
||
it('renders the button with the correct text', () => { | ||
renderComponent(); | ||
expect( | ||
screen.getByRole('button', { name: BUTTON_TEXT }) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays the CIE icon', () => { | ||
renderComponent(); | ||
const icon = screen.getByAltText('CIE Icon'); | ||
expect(icon).toBeInTheDocument(); | ||
expect(icon).toHaveAttribute('src', expect.stringContaining('CIEIcon')); | ||
}); | ||
|
||
it('calls the onClick handler when clicked', async () => { | ||
renderComponent(); | ||
const button = screen.getByRole('button', { name: BUTTON_TEXT }); | ||
await userEvent.click(button); | ||
expect(onClickMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.