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

changed table to use system name and not id #497 #505

Merged
merged 10 commits into from
May 7, 2024
31 changes: 31 additions & 0 deletions src/api/systems.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useEditSystem,
useMoveToSystem,
useSystem,
useSystemIds,
useSystems,
useSystemsBreadcrumbs,
} from './systems';
Expand Down Expand Up @@ -106,6 +107,36 @@ describe('System api functions', () => {
});
});

describe('useSystemIds', () => {
it('sends a request to fetch system data and returns a successful response', async () => {
const { result } = renderHook(
() =>
useSystemIds([
'65328f34a40ff5301575a4e3',
'656ef565ed0773f82e44bc6d',
]),
{
wrapper: hooksWrapperWithProviders(),
}
);

await waitFor(() => {
result.current.forEach((query) => expect(query.isSuccess).toBeTruthy());
});

expect(result.current[0].data).toEqual(
SystemsJSON.filter(
(system) => system.id === '65328f34a40ff5301575a4e3'
)[0]
);
expect(result.current[1].data).toEqual(
SystemsJSON.filter(
(system) => system.id === '656ef565ed0773f82e44bc6d'
)[0]
);
});
});

describe('useSystemsBreadcrumbs', () => {
it('does not send a request to fetch breadcrumbs data for a system when its id is null', async () => {
const { result } = renderHook(() => useSystemsBreadcrumbs(null), {
Expand Down
10 changes: 10 additions & 0 deletions src/api/systems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
UseMutationResult,
UseQueryResult,
useMutation,
useQueries,
useQuery,
useQueryClient,
} from '@tanstack/react-query';
Expand Down Expand Up @@ -44,6 +45,15 @@ const fetchSystems = async (parent_id?: string): Promise<System[]> => {
});
};

export const useSystemIds = (ids: string[]): UseQueryResult<System>[] => {
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
return useQueries({
queries: ids.map((id) => ({
queryKey: ['Systems', id],
queryFn: () => fetchSystem(id),
})),
});
};

export const useSystems = (
parent_id?: string
): UseQueryResult<System[], AxiosError> => {
Expand Down
Loading