From b852f8274c02719b80a28c1cffad34477c443d2b Mon Sep 17 00:00:00 2001 From: Jonathan Perchoc Date: Wed, 25 Sep 2024 17:31:58 +0200 Subject: [PATCH] fix: invalidate services list query on update ref: DATATR-1497 Signed-off-by: Jonathan Perchoc --- .../hooks/api/catalog/useGetCatalog.hook.tsx | 1 + .../hooks/api/catalog/useGetCatalog.spec.tsx | 10 +-- .../service/useDeleteService.hook.tsx | 14 +++- .../service/useDeleteService.spec.tsx | 73 +------------------ .../database/service/useEditService.hook.tsx | 13 +++- .../database/service/useEditService.spec.tsx | 6 +- 6 files changed, 28 insertions(+), 89 deletions(-) diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.hook.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.hook.tsx index 1904b04c3ad5..efe4b1eabfbf 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.hook.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.hook.tsx @@ -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; diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.spec.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.spec.tsx index a5f21aaba297..56091e30566a 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.spec.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/catalog/useGetCatalog.spec.tsx @@ -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), }; }); diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.hook.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.hook.tsx index d06e595e39a2..f4bec1aca908 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.hook.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.hook.tsx @@ -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'; @@ -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 { diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.spec.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.spec.tsx index 2764f233f815..9b73f7a1c1f9 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.spec.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useDeleteService.spec.tsx @@ -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'; @@ -102,11 +35,7 @@ describe('useDeleteService', () => { expect(databaseAPI.deleteService).toHaveBeenCalledWith( deleteServiceProps, ); - expect(onSuccess).toHaveBeenCalledWith( - undefined, - deleteServiceProps, - undefined, - ); + expect(onSuccess).toHaveBeenCalledWith(); }); }); }); diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.hook.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.hook.tsx index b7d7d9bd8b5b..4b9de168c93f 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.hook.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.hook.tsx @@ -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'; @@ -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 { diff --git a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.spec.tsx b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.spec.tsx index ad465d60a0f0..fae365c80764 100644 --- a/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.spec.tsx +++ b/packages/manager/apps/pci-databases-analytics/src/hooks/api/database/service/useEditService.spec.tsx @@ -38,11 +38,7 @@ describe('useEditService', () => { await waitFor(() => { expect(databaseAPI.editService).toHaveBeenCalledWith(editServiceProps); - expect(onSuccess).toHaveBeenCalledWith( - mockedService, - editServiceProps, - undefined, - ); + expect(onSuccess).toHaveBeenCalledWith(mockedService); }); }); });