Skip to content

Commit

Permalink
feat(pci-object-storage): add test on hook
Browse files Browse the repository at this point in the history
Signed-off-by: Yoann Fievez <[email protected]>
  • Loading branch information
kqesar committed Jan 23, 2025
1 parent 84637f5 commit 4c136a4
Show file tree
Hide file tree
Showing 8 changed files with 734 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/manager/apps/pci-object-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"start": "lerna exec --stream --scope='@ovh-ux/manager-pci-object-storage-app' --include-dependencies -- npm run build --if-present",
"start:dev": "lerna exec --stream --scope='@ovh-ux/manager-pci-object-storage-app' --include-dependencies -- npm run dev --if-present",
"start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/manager-pci-object-storage-app' --include-dependencies -- npm run dev:watch --if-present",
"test": "vitest run"
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@ovh-ux/manager-config": "^8.0.0",
Expand Down
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,
});
});
});
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);
});
});
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();
});
});
});
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);
});
});
Loading

0 comments on commit 4c136a4

Please sign in to comment.