Skip to content

Commit

Permalink
test(pci-load-balancer): add unit tests
Browse files Browse the repository at this point in the history
ref: DTCORE-2770
Signed-off-by: Yoann Fievez <[email protected]>
  • Loading branch information
kqesar committed Oct 25, 2024
1 parent f8ee4dd commit cfd2713
Show file tree
Hide file tree
Showing 51 changed files with 3,946 additions and 285 deletions.

This file was deleted.

5 changes: 4 additions & 1 deletion packages/manager/apps/pci-load-balancer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"start": "lerna exec --stream --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run build --if-present",
"start:dev": "lerna exec --stream --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run dev --if-present",
"start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run dev:watch --if-present",
"test": "vitest run"
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:watch": "vitest",
"test:watch:coverage": "vitest --coverage"
},
"dependencies": {
"@ovh-ux/manager-config": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { TAddon } from '@/pages/create/store';
import { getFlavor } from './flavors';
import { TFlavor } from '@/api/data/load-balancer';

describe('getFlavor', () => {
const projectId = 'test-project';
const regionName = 'test-region';
const addon = ({
technicalName: 'test-flavor',
invoiceName: 'invoiceName',
} as unknown) as TAddon;

it('should return the correct flavor', async () => {
const mockFlavors: TFlavor[] = [
{ name: 'test-flavor', region: 'region1', id: 'id1' },
{ name: 'another-flavor', region: 'region2', id: 'id2' },
];

vi.mocked(v6.get).mockResolvedValue({ data: mockFlavors });

const result = await getFlavor(projectId, regionName, addon);

expect(result).toEqual(mockFlavors[0]);
});

it('should return undefined if the flavor is not found', async () => {
const mockFlavors: TFlavor[] = [
{ name: 'another-flavor', region: 'value2', id: 'id2' },
];

vi.mocked(v6.get).mockResolvedValue({ data: mockFlavors });

const result = await getFlavor(projectId, regionName, addon);

expect(result).toBeUndefined();
});

it('should handle API errors gracefully', async () => {
vi.mocked(v6.get).mockRejectedValue(new Error('API Error'));

await expect(getFlavor(projectId, regionName, addon)).rejects.toThrow(
'API Error',
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { getFloatingIps, TFloatingIp } from './floating-ips';

describe('getFloatingIps', () => {
const projectId = 'test-project-id';
const region = 'test-region';
const mockFloatingIps: TFloatingIp[] = [
{
associatedEntity: 'entity1',
id: 'id1',
ip: '192.168.1.1',
networkId: 'network1',
status: 'active',
type: 'public',
},
{
associatedEntity: 'entity2',
id: 'id2',
ip: '192.168.1.2',
networkId: 'network2',
status: 'inactive',
type: 'private',
},
];

it('should fetch floating IPs for a given project and region', async () => {
vi.mocked(v6.get).mockResolvedValue({ data: mockFloatingIps });

const result = await getFloatingIps(projectId, region);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/floatingip`,
);
expect(result).toEqual(mockFloatingIps);
});

it('should handle errors when fetching floating IPs', async () => {
const errorMessage = 'Network Error';
vi.mocked(v6.get).mockRejectedValue(new Error(errorMessage));

await expect(getFloatingIps(projectId, region)).rejects.toThrow(
errorMessage,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { getSubnetGateways, TSubnetGateway } from './gateways';

describe('getSubnetGateways', () => {
const projectId = 'test-project';
const regionName = 'test-region';
const subnetId = 'test-subnet';

const mockGateways: TSubnetGateway[] = [
{
externalInformation: {
ips: [{ ip: '192.168.1.1', subnetId: 'subnet-1' }],
networkId: 'network-1',
},
id: 'gateway-1',
interfaces: [
{
id: 'interface-1',
ip: '192.168.1.2',
networkId: 'network-1',
subnetId: 'subnet-1',
},
],
model: 'model-1',
name: 'gateway-name-1',
region: 'region-1',
status: 'ACTIVE',
},
];

it('should fetch subnet gateways and return data', async () => {
vi.mocked(v6.get).mockResolvedValueOnce({ data: mockGateways });

const result = await getSubnetGateways(projectId, regionName, subnetId);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${regionName}/gateway?subnetId=${subnetId}`,
);
expect(result).toEqual(mockGateways);
});

it('should handle empty response', async () => {
vi.mocked(v6.get).mockResolvedValueOnce({ data: [] });

const result = await getSubnetGateways(projectId, regionName, subnetId);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${regionName}/gateway?subnetId=${subnetId}`,
);
expect(result).toEqual([]);
});

it('should handle API errors', async () => {
const errorMessage = 'API Error';
vi.mocked(v6.get).mockRejectedValueOnce(new Error(errorMessage));

await expect(
getSubnetGateways(projectId, regionName, subnetId),
).rejects.toThrow(errorMessage);
});
});
Loading

0 comments on commit cfd2713

Please sign in to comment.