Skip to content
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

feat(mrc): allow useFeatureAvailability query options override #14587

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { renderHook } from '@testing-library/react';
import { vi } from 'vitest';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
import { useFeatureAvailability } from './useFeatureAvailability';

const wrapper = ({ children }) => {
const queryClient = new QueryClient();
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

vi.mock('@ovh-ux/manager-core-api', () => ({
apiClient: {
aapi: {
get: vi.fn(),
},
},
}));

describe('useFeatureAvailability', () => {
it('should fetch data if no options is given', () => {
const { result } = renderHook(() => useFeatureAvailability(['test']), {
wrapper,
});
expect(result.current?.isFetching).toBe(true);
});

it('should not fetch data if enabled option is false', () => {
const { result } = renderHook(
() => useFeatureAvailability(['test'], { enabled: false }),
{
wrapper,
},
);
expect(result.current?.isFetching).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { apiClient, ApiError } from '@ovh-ux/manager-core-api';
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import {
DefinedInitialDataOptions,
useQuery,
UseQueryResult,
} from '@tanstack/react-query';

export type UseFeatureAvailabilityResult<T = Record<string, boolean>> =
UseQueryResult<T, ApiError>;
Expand Down Expand Up @@ -34,10 +38,14 @@ export const getFeatureAvailabilityQueryKey = <T extends string[]>(
*/
export const useFeatureAvailability = <T extends string[]>(
featureList: [...T],
options: Partial<
DefinedInitialDataOptions<Record<(typeof featureList)[number], boolean>>
> = {},
): UseFeatureAvailabilityResult<
Record<(typeof featureList)[number], boolean>
> =>
useQuery<Record<(typeof featureList)[number], boolean>, ApiError>({
...options,
queryKey: getFeatureAvailabilityQueryKey(featureList),
queryFn: () => fetchFeatureAvailabilityData(featureList),
});
Loading