From c709b8dcf0c6c98b388e94062bd928cfa4914a80 Mon Sep 17 00:00:00 2001 From: Jacques Larique Date: Tue, 26 Nov 2024 10:42:27 +0100 Subject: [PATCH] fix(mrc): ensure useAuthorizationIam query option enabled is a boolean Signed-off-by: Jacques Larique --- .../src/hooks/iam/useOvhIam.spec.ts | 9 --- .../src/hooks/iam/useOvhIam.spec.tsx | 68 +++++++++++++++++++ .../src/hooks/iam/useOvhIam.tsx | 6 +- 3 files changed, 73 insertions(+), 10 deletions(-) delete mode 100644 packages/manager-react-components/src/hooks/iam/useOvhIam.spec.ts create mode 100644 packages/manager-react-components/src/hooks/iam/useOvhIam.spec.tsx diff --git a/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.ts b/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.ts deleted file mode 100644 index 7a8b4f43e7fe..000000000000 --- a/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { getAuthorizationCheckUrl } from './useOvhIam'; - -describe('getAuthorizationCheckUrl', () => { - it('encodes the urn if it contains /', () => { - expect(getAuthorizationCheckUrl('test/urn')).toBe( - '/iam/resource/test%2Furn/authorization/check', - ); - }); -}); diff --git a/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.tsx b/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.tsx new file mode 100644 index 000000000000..3925bd593af0 --- /dev/null +++ b/packages/manager-react-components/src/hooks/iam/useOvhIam.spec.tsx @@ -0,0 +1,68 @@ +import { renderHook } from '@testing-library/react'; +import { vi } from 'vitest'; +import { QueryClientProvider, QueryClient } from '@tanstack/react-query'; +import { + ShellContext, + ShellContextType, +} from '@ovh-ux/manager-react-shell-client'; +import { getAuthorizationCheckUrl, useAuthorizationIam } from './useOvhIam'; + +const shellContext = { + environment: { + getUser: () => ({ ovhSubsidiary: 'mocked_ovhSubsidiary' }), + }, + shell: { + navigation: { + getURL: vi.fn(), + }, + }, +}; + +const queryClient = new QueryClient(); +const wrapper = ({ children }) => ( + + + {children} + + +); + +vi.mock('@ovh-ux/manager-core-api', () => ({ + apiClient: { + v2: { + get: vi.fn(), + }, + }, +})); + +describe('getAuthorizationCheckUrl', () => { + it('encodes the urn if it contains /', () => { + expect(getAuthorizationCheckUrl('test/urn')).toBe( + '/iam/resource/test%2Furn/authorization/check', + ); + }); +}); + +describe('useAuthorizationIam', () => { + it('should not fetch data if urn is nil', () => { + const { result } = renderHook( + () => useAuthorizationIam(['test'], undefined), + { + wrapper, + }, + ); + expect(result.current?.isFetching).toBe(false); + }); + it('should not fetch data if actions is nil', () => { + const { result } = renderHook(() => useAuthorizationIam(undefined, 'urn'), { + wrapper, + }); + expect(result.current?.isFetching).toBe(false); + }); + it('should fetch data if both actions and urn are not nil', () => { + const { result } = renderHook(() => useAuthorizationIam(['test'], 'urn'), { + wrapper, + }); + expect(result.current?.isFetching).toBe(true); + }); +}); diff --git a/packages/manager-react-components/src/hooks/iam/useOvhIam.tsx b/packages/manager-react-components/src/hooks/iam/useOvhIam.tsx index c03f58511aef..32a2533aef61 100644 --- a/packages/manager-react-components/src/hooks/iam/useOvhIam.tsx +++ b/packages/manager-react-components/src/hooks/iam/useOvhIam.tsx @@ -51,7 +51,11 @@ export function useAuthorizationIam( queryKey: [urn, actions], queryFn: () => fetchAuthorizationCheck(actions, urn), enabled: - urn && urn.length > 0 && actions && actions.length > 0 && isTrigger, + Boolean(urn) && + urn.length > 0 && + Boolean(actions) && + actions.length > 0 && + isTrigger, placeholderData: keepPreviousData, });