Skip to content

Commit

Permalink
fix: invalidate services list query on update
Browse files Browse the repository at this point in the history
ref: DATATR-1497
Signed-off-by: Jonathan Perchoc <[email protected]>
  • Loading branch information
jperchoc committed Sep 27, 2024
1 parent f3e5279 commit b852f82
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function useGetCatalog(
const queryKey = ['order/catalog/public/cloud', subsidiary, product];
return useQueryImmediateRefetch({
queryKey,
enabled: !!user?.ovhSubsidiary,
queryFn: () => catalogApi.getCatalog(subsidiary, product),
...options,
}) as UseQueryResult<order.publicOrder.Catalog, Error>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ vi.mock('@/data/api/catalog/catalog.api', () => ({
},
}));

vi.mock('@ovh-ux/manager-react-shell-client', () => {
vi.mock('@/hooks/useUser', () => {
return {
useShell: vi.fn(() => ({
environment: {
getEnvironment: vi.fn(() => ({
getUser: vi.fn(() => mockedUser),
})),
},
})),
useUser: vi.fn(() => mockedUser),
};
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import * as database from '@/types/cloud/project/database';
import { deleteService } from '@/data/api/database/service.api';
import { CdbError, ServiceData } from '@/data/api/database';

Expand All @@ -8,12 +10,20 @@ interface UseDeleteService {
}

export function useDeleteService({ onError, onSuccess }: UseDeleteService) {
const queryClient = useQueryClient();
const { projectId } = useParams();
const mutation = useMutation({
mutationFn: (serviceInfo: ServiceData) => {
return deleteService(serviceInfo);
},
onError,
onSuccess,
onSuccess: () => {
onSuccess();
// Invalidate service list query to get the latest data
queryClient.invalidateQueries({
queryKey: [projectId, 'database/service'],
});
},
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,14 @@
import { renderHook, waitFor } from '@testing-library/react';
import { vi } from 'vitest';
import { useDeleteService } from './useDeleteService.hook';
import { useEditService } from './useEditService.hook';
import { useAddService } from './useAddService.hook';
import * as databaseAPI from '@/data/api/database/service.api';
import * as database from '@/types/cloud/project/database';
import { QueryClientWrapper } from '@/__tests__/helpers/wrappers/QueryClientWrapper';
import {
mockedService,
mockedServiceCreation,
mockedServiceCreationWithEngine,
} from '@/__tests__/helpers/mocks/services';

vi.mock('@/data/api/database/service.api', () => ({
getServices: vi.fn(),
getService: vi.fn(),
addService: vi.fn(),
editService: vi.fn(),
deleteService: vi.fn(),
}));

describe('useAddService', () => {
it('should call useAddService on mutation with data', async () => {
const onSuccess = vi.fn();
const onError = vi.fn();

vi.mocked(databaseAPI.addService).mockResolvedValue(mockedService);
const { result } = renderHook(() => useAddService({ onError, onSuccess }), {
wrapper: QueryClientWrapper,
});

result.current.addService(mockedServiceCreationWithEngine);

await waitFor(() => {
expect(databaseAPI.addService).toHaveBeenCalledWith({
projectId: undefined,
engine: database.EngineEnum.mongodb,
serviceInfo: mockedServiceCreation,
});
expect(onSuccess).toHaveBeenCalledWith(mockedService);
});
});
});

describe('useEditService', () => {
it('should call useEditService on mutation with data', async () => {
const projectId = 'projectId';
const engine = database.EngineEnum.mysql;
const serviceId = 'serviceId';
const onSuccess = vi.fn();
const onError = vi.fn();

vi.mocked(databaseAPI.editService).mockResolvedValue(mockedService);
const { result } = renderHook(
() => useEditService({ onError, onSuccess }),
{ wrapper: QueryClientWrapper },
);

const editServiceProps: databaseAPI.EditService = {
projectId,
engine,
serviceId,
data: mockedService,
};
result.current.editService(editServiceProps);

await waitFor(() => {
expect(databaseAPI.editService).toHaveBeenCalledWith(editServiceProps);
expect(onSuccess).toHaveBeenCalledWith(
mockedService,
editServiceProps,
undefined,
);
});
});
});

describe('useDeleteService', () => {
it('should call useDeleteService on mutation with data', async () => {
const projectId = 'projectId';
Expand All @@ -102,11 +35,7 @@ describe('useDeleteService', () => {
expect(databaseAPI.deleteService).toHaveBeenCalledWith(
deleteServiceProps,
);
expect(onSuccess).toHaveBeenCalledWith(
undefined,
deleteServiceProps,
undefined,
);
expect(onSuccess).toHaveBeenCalledWith();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import * as database from '@/types/cloud/project/database';
import { EditService, editService } from '@/data/api/database/service.api';
import { CdbError } from '@/data/api/database';
Expand All @@ -8,10 +9,18 @@ interface UseEditService {
onSuccess: (service: database.Service) => void;
}
export function useEditService({ onError, onSuccess }: UseEditService) {
const queryClient = useQueryClient();
const { projectId } = useParams();
const mutation = useMutation({
mutationFn: (serviceUpdate: EditService) => editService(serviceUpdate),
onError,
onSuccess,
onSuccess: (data: database.Service) => {
onSuccess(data);
// Invalidate service list query to get the latest data
queryClient.invalidateQueries({
queryKey: [projectId, 'database/service'],
});
},
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ describe('useEditService', () => {

await waitFor(() => {
expect(databaseAPI.editService).toHaveBeenCalledWith(editServiceProps);
expect(onSuccess).toHaveBeenCalledWith(
mockedService,
editServiceProps,
undefined,
);
expect(onSuccess).toHaveBeenCalledWith(mockedService);
});
});
});

0 comments on commit b852f82

Please sign in to comment.