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.
Merge pull request #549 from pagopa/feat/OI-249-idps-loading-status
feat: [OI-249] login data status
- Loading branch information
Showing
24 changed files
with
1,020 additions
and
541 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
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
110 changes: 110 additions & 0 deletions
110
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,110 @@ | ||
import { describe, it, expect, vi, Mock } from 'vitest'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { useLoginData } from './useLoginData'; | ||
import { ENV } from '../utils/env'; | ||
import { fetchBannerContent, getIdpList, getClientData } from '../services/api'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
|
||
// Mock API functions | ||
vi.mock('../services/api', () => ({ | ||
fetchBannerContent: vi.fn(), | ||
getIdpList: vi.fn(), | ||
getClientData: vi.fn(), | ||
})); | ||
|
||
// Mock ENV | ||
vi.mock('../utils/env', () => ({ | ||
ENV: { | ||
JSON_URL: { | ||
ALERT: 'mock-alert-url', | ||
IDP_LIST: 'mock-idp-list-url', | ||
CLIENT_BASE_URL: 'mock-client-base-url', | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useLoginData', () => { | ||
const wrapper = ({ children }: { children: React.ReactNode }) => { | ||
const queryClient = new QueryClient(); | ||
return ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('fetches banner content successfully', async () => { | ||
const mockBannerContent = [ | ||
{ title: 'Test Banner', description: 'Test Description' }, | ||
]; | ||
(fetchBannerContent as Mock).mockResolvedValue(mockBannerContent); | ||
|
||
const { result } = renderHook(useLoginData, { wrapper }); | ||
|
||
await waitFor(() => | ||
expect(result.current.bannerQuery.isSuccess).toBe(true) | ||
); | ||
expect(fetchBannerContent).toHaveBeenCalledWith(ENV.JSON_URL.ALERT); | ||
expect(result.current.bannerQuery.data).toEqual(mockBannerContent); | ||
}); | ||
|
||
it('fetches identity providers list successfully', async () => { | ||
const mockIdpList = { | ||
providers: [{ name: 'Test IDP', url: 'test-idp-url' }], | ||
}; | ||
(getIdpList as Mock).mockResolvedValue(mockIdpList); | ||
|
||
const { result } = renderHook(useLoginData, { wrapper }); | ||
|
||
await waitFor(() => expect(result.current.idpQuery.isSuccess).toBe(true)); | ||
expect(getIdpList).toHaveBeenCalledWith(ENV.JSON_URL.IDP_LIST); | ||
expect(result.current.idpQuery.data).toEqual(mockIdpList); | ||
}); | ||
|
||
it('fetches client data successfully', async () => { | ||
const mockClientData = { | ||
clientId: 'test-client-id', | ||
clientSecret: 'test-client-secret', | ||
}; | ||
(getClientData as Mock).mockResolvedValue(mockClientData); | ||
|
||
const { result } = renderHook(useLoginData, { wrapper }); | ||
|
||
await waitFor(() => | ||
expect(result.current.clientQuery.isSuccess).toBe(true) | ||
); | ||
expect(getClientData).toHaveBeenCalledWith(ENV.JSON_URL.CLIENT_BASE_URL); | ||
expect(result.current.clientQuery.data).toEqual(mockClientData); | ||
}); | ||
|
||
it('handles errors correctly', async () => { | ||
(fetchBannerContent as Mock).mockRejectedValue( | ||
new Error('Banner content error') | ||
); | ||
(getIdpList as Mock).mockRejectedValue(new Error('IDP list error')); | ||
(getClientData as Mock).mockRejectedValue(new Error('Client data error')); | ||
|
||
const { result } = renderHook(useLoginData, { wrapper }); | ||
|
||
await waitFor(() => expect(result.current.bannerQuery.isError).toBe(true), { | ||
timeout: 10000, | ||
}); | ||
expect(result.current.bannerQuery.error).toEqual( | ||
new Error('Banner content error') | ||
); | ||
|
||
await waitFor(() => expect(result.current.idpQuery.isError).toBe(true), { | ||
timeout: 10000, | ||
}); | ||
expect(result.current.idpQuery.error).toEqual(new Error('IDP list error')); | ||
|
||
await waitFor(() => expect(result.current.clientQuery.isError).toBe(true), { | ||
timeout: 10000, | ||
}); | ||
expect(result.current.clientQuery.error).toEqual( | ||
new Error('Client data error') | ||
); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ENV } from '../utils/env'; | ||
import { IdentityProviders } from '../utils/IDPS'; | ||
import { | ||
type BannerContent, | ||
type Client, | ||
fetchBannerContent, | ||
getIdpList, | ||
getClientData, | ||
} from '../services/api'; | ||
|
||
const staleTime = 5 * 60 * 1000; | ||
const retry = 2; | ||
|
||
export const useLoginData = () => { | ||
const bannerQuery = useQuery<Array<BannerContent>, Error>({ | ||
queryKey: ['bannerContent'], | ||
queryFn: () => fetchBannerContent(ENV.JSON_URL.ALERT), | ||
staleTime, | ||
retry, | ||
}); | ||
|
||
const idpQuery = useQuery<IdentityProviders, Error>({ | ||
queryKey: ['idpList'], | ||
queryFn: () => getIdpList(ENV.JSON_URL.IDP_LIST), | ||
staleTime, | ||
retry, | ||
}); | ||
|
||
const clientQuery = useQuery<Client, Error>({ | ||
queryKey: ['clientData'], | ||
queryFn: () => getClientData(ENV.JSON_URL.CLIENT_BASE_URL), | ||
staleTime, | ||
retry, | ||
}); | ||
|
||
return { bannerQuery, idpQuery, clientQuery }; | ||
}; |
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
Oops, something went wrong.