-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pci-object-storage): add test on hook
Signed-off-by: Yoann Fievez <[email protected]>
- Loading branch information
Showing
8 changed files
with
734 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/manager/apps/pci-object-storage/src/api/hooks/useContainer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
import { applyFilters } from '@ovh-ux/manager-core-api'; | ||
import { getServerContainer, TServerContainer } from '@/api/data/container'; | ||
import { usePaginatedObjects, useServerContainer } from './useContainer'; | ||
|
||
import { wrapper } from '@/wrapperRenders'; | ||
import { paginateResults, sortResults } from '@/helpers'; | ||
|
||
vi.mock('@/api/data/container'); | ||
|
||
const mockProjectId = 'project-123'; | ||
const mockRegion = 'GRA'; | ||
const mockName = 'container-1'; | ||
const mockId = 'id-123'; | ||
describe('useContainer hooks', () => { | ||
it('fetches container data successfully', async () => { | ||
vi.mocked(getServerContainer).mockResolvedValueOnce({ | ||
id: mockId, | ||
name: mockName, | ||
} as TServerContainer); | ||
|
||
const { result } = renderHook( | ||
() => useServerContainer(mockProjectId, mockRegion, mockName, mockId), | ||
{ wrapper }, | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data).toEqual({ id: mockId, name: mockName }); | ||
expect(getServerContainer).toHaveBeenCalledWith( | ||
mockProjectId, | ||
mockRegion, | ||
mockName, | ||
mockId, | ||
); | ||
}); | ||
}); | ||
|
||
describe('usePaginatedObjects', () => { | ||
it('should return paginated and sorted objects of container', async () => { | ||
const mockData = { | ||
id: '1', | ||
name: 'listener1', | ||
objects: [], | ||
} as TServerContainer; | ||
vi.mocked(getServerContainer).mockResolvedValue(mockData); | ||
vi.mocked(applyFilters).mockReturnValue(mockData.objects); | ||
vi.mocked(sortResults).mockReturnValue(mockData.objects); | ||
vi.mocked(paginateResults).mockReturnValue({ | ||
rows: mockData.objects, | ||
pageCount: 1, | ||
totalRows: 1, | ||
}); | ||
|
||
const pagination = { pageIndex: 0, pageSize: 10 }; | ||
const sorting = { id: 'name', desc: false }; | ||
const filters = []; | ||
|
||
const { result } = renderHook( | ||
() => | ||
usePaginatedObjects( | ||
mockProjectId, | ||
mockRegion, | ||
mockName, | ||
mockId, | ||
pagination, | ||
sorting, | ||
filters, | ||
), | ||
{ wrapper }, | ||
); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toBe(false)); | ||
|
||
expect(result.current.paginatedObjects).toEqual({ | ||
rows: mockData.objects, | ||
pageCount: 1, | ||
totalRows: 1, | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/manager/apps/pci-object-storage/src/api/hooks/useGetEncriptionAvailability.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
useFeatureAvailability, | ||
UseFeatureAvailabilityResult, | ||
} from '@ovh-ux/manager-react-components'; | ||
import { vi, describe, it, expect } from 'vitest'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { useGetEncriptionAvailability } from './useGetEncriptionAvailability'; | ||
|
||
vi.mock('@ovh-ux/manager-react-components', () => ({ | ||
useFeatureAvailability: vi.fn(), | ||
})); | ||
|
||
describe('useGetEncriptionAvailability', () => { | ||
it('should return available as true when feature is available', () => { | ||
vi.mocked(useFeatureAvailability).mockReturnValue({ | ||
data: { 'public-cloud:object-storage:encryption': true }, | ||
} as UseFeatureAvailabilityResult); | ||
|
||
const { result } = renderHook(() => useGetEncriptionAvailability()); | ||
|
||
expect(result.current.available).toBe(true); | ||
}); | ||
|
||
it('should return available as false when feature is not available', () => { | ||
vi.mocked(useFeatureAvailability).mockReturnValue({ | ||
data: { 'public-cloud:object-storage:encryption': false }, | ||
} as UseFeatureAvailabilityResult); | ||
|
||
const { result } = renderHook(() => useGetEncriptionAvailability()); | ||
|
||
expect(result.current.available).toBe(false); | ||
}); | ||
|
||
it('should return available as false when feature data is undefined', () => { | ||
vi.mocked(useFeatureAvailability).mockReturnValue({ | ||
data: undefined, | ||
} as UseFeatureAvailabilityResult); | ||
|
||
const { result } = renderHook(() => useGetEncriptionAvailability()); | ||
|
||
expect(result.current.available).toBe(false); | ||
}); | ||
}); |
194 changes: 194 additions & 0 deletions
194
packages/manager/apps/pci-object-storage/src/api/hooks/useObject.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { vi } from 'vitest'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { getStorageAccess, TStorage, TStorageAccess } from '../data/storages'; | ||
import { | ||
addHighPerfObjects, | ||
addUser, | ||
deleteS3Object, | ||
deleteSwiftObject, | ||
} from '../data/objects'; | ||
|
||
import { wrapper } from '@/wrapperRenders'; | ||
import { useAddObjects, useAddUser, useDeleteObject } from './useObject'; | ||
|
||
vi.mock('@/api/data/objects'); | ||
vi.mock('../data/storages', () => ({ | ||
getStorageAccess: vi.fn(), | ||
})); | ||
|
||
describe('useObject hooks', () => { | ||
describe('useDeleteObject', () => { | ||
it('should call deleteS3Object', async () => { | ||
const onSuccess = vi.fn(); | ||
const onError = vi.fn(); | ||
const storage = { | ||
name: 'test', | ||
s3StorageType: 'STANDARD', | ||
id: '1', | ||
region: 'us-east-1', | ||
} as TStorage; | ||
const projectId = 'project1'; | ||
const objectName = 'object1'; | ||
const region = 'us-east-1'; | ||
|
||
vi.mocked(deleteS3Object).mockResolvedValueOnce({ | ||
data: {}, | ||
} as never); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useDeleteObject({ | ||
projectId, | ||
storage, | ||
objectName, | ||
region, | ||
onError, | ||
onSuccess, | ||
}), | ||
{ wrapper }, | ||
); | ||
|
||
await act(async () => { | ||
result.current.deleteObject(); | ||
}); | ||
|
||
expect(deleteS3Object).toHaveBeenCalledWith({ | ||
projectId, | ||
containerId: storage.name, | ||
objectName, | ||
containerRegion: region, | ||
s3StorageType: storage.s3StorageType, | ||
}); | ||
expect(onSuccess).toHaveBeenCalled(); | ||
expect(onError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call deleteSwiftObject', async () => { | ||
const onSuccess = vi.fn(); | ||
const onError = vi.fn(); | ||
const storage = { | ||
name: 'test', | ||
id: '1', | ||
region: 'us-east-1', | ||
} as TStorage; | ||
const projectId = 'project1'; | ||
const objectName = 'object1'; | ||
const region = 'us-east-1'; | ||
|
||
vi.mocked(getStorageAccess).mockResolvedValue({ | ||
token: 'token', | ||
} as TStorageAccess); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useDeleteObject({ | ||
projectId, | ||
storage, | ||
objectName, | ||
region, | ||
onError, | ||
onSuccess, | ||
}), | ||
{ wrapper }, | ||
); | ||
|
||
await act(async () => { | ||
await result.current.deleteObject(); | ||
}); | ||
|
||
expect(deleteSwiftObject).toHaveBeenCalledWith({ | ||
projectId, | ||
storageName: storage.name, | ||
objectName, | ||
token: 'token', | ||
region, | ||
}); | ||
expect(onSuccess).toHaveBeenCalled(); | ||
expect(onError).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('useAddUser', () => { | ||
it('should call addUser', async () => { | ||
const onSuccess = vi.fn(); | ||
const onError = vi.fn(); | ||
const projectId = 'project1'; | ||
const storageId = 'storage1'; | ||
const objectName = 'object1'; | ||
const region = 'us-east-1'; | ||
const userId = 1; | ||
const role = 'admin'; | ||
|
||
const { result } = renderHook( | ||
() => | ||
useAddUser({ | ||
projectId, | ||
storageId, | ||
objectName, | ||
region, | ||
userId, | ||
role, | ||
onError, | ||
onSuccess, | ||
}), | ||
{ wrapper }, | ||
); | ||
|
||
await act(async () => { | ||
await result.current.addUser(); | ||
}); | ||
|
||
expect(addUser).toHaveBeenCalledWith({ | ||
projectId, | ||
region, | ||
objectName, | ||
storageId, | ||
userId, | ||
role, | ||
}); | ||
expect(onSuccess).toHaveBeenCalled(); | ||
expect(onError).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('useAddObjects', () => { | ||
it('should call addHighPerfObjects', async () => { | ||
const onSuccess = vi.fn(); | ||
const onError = vi.fn(); | ||
const projectId = 'project1'; | ||
const container = { | ||
name: 'test', | ||
id: '1', | ||
s3StorageType: 'STANDARD', | ||
region: 'region', | ||
} as TStorage; | ||
const files = [{ name: 'object1' }] as File[]; | ||
const { result } = renderHook( | ||
() => | ||
useAddObjects({ | ||
projectId, | ||
container, | ||
prefix: '/prefix', | ||
files, | ||
storageClass: 'storageClass', | ||
onError, | ||
onSuccess, | ||
}), | ||
{ wrapper }, | ||
); | ||
await act(async () => { | ||
result.current.addObjects(); | ||
}); | ||
expect(addHighPerfObjects).toHaveBeenCalledWith( | ||
projectId, | ||
container.region, | ||
container.name, | ||
'/prefix', | ||
files, | ||
container.s3StorageType, | ||
'storageClass', | ||
); | ||
expect(onSuccess).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/manager/apps/pci-object-storage/src/api/hooks/useRegion.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { vi } from 'vitest'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { useGetRegion } from './useRegion'; | ||
import { getRegion, TRegion } from '@/api/data/region'; | ||
import { wrapper } from '@/wrapperRenders'; | ||
|
||
vi.mock('@/api/data/region'); | ||
|
||
describe('useGetRegion', () => { | ||
it('should fetch region data successfully', async () => { | ||
const projectId = 'test-project'; | ||
const region = 'test-region'; | ||
const mockRegionData = ({ | ||
id: region, | ||
name: 'Test Region', | ||
status: 'UP', | ||
services: [], | ||
} as unknown) as TRegion; | ||
|
||
vi.mocked(getRegion).mockResolvedValueOnce(mockRegionData); | ||
|
||
const { result } = renderHook(() => useGetRegion(projectId, region), { | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data).toEqual(mockRegionData); | ||
expect(getRegion).toHaveBeenCalledWith(projectId, region); | ||
}); | ||
|
||
it('should handle error while fetching region data', async () => { | ||
const projectId = 'test-project'; | ||
const region = 'test-region'; | ||
const mockError = new Error('Failed to fetch region data'); | ||
|
||
vi.mocked(getRegion).mockRejectedValueOnce(mockError); | ||
|
||
const { result } = renderHook(() => useGetRegion(projectId, region), { | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isError).toBe(true)); | ||
|
||
expect(result.current.error).toEqual(mockError); | ||
expect(getRegion).toHaveBeenCalledWith(projectId, region); | ||
}); | ||
}); |
Oops, something went wrong.