Skip to content

Commit

Permalink
add tests for valid case after resolving bug with JS floating number …
Browse files Browse the repository at this point in the history
…issue
  • Loading branch information
JeanMarcMilletScality committed Dec 16, 2024
1 parent 79b68b1 commit 35b1a61
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
11 changes: 11 additions & 0 deletions src/react/ui-elements/Veeam/VeeamCapacityModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { bucketName } from '../../../js/mock/S3Client';
import { NewWrapper, TEST_API_BASE_URL } from '../../utils/testUtil';
import { VeeamCapacityModal } from './VeeamCapacityModal';
import { VEEAM_XML_PREFIX } from './VeeamConstants';
import userEvent from '@testing-library/user-event';

describe('VeeamCapacityModal', () => {
const mockMutate = jest.fn();
Expand Down Expand Up @@ -52,6 +53,16 @@ describe('VeeamCapacityModal', () => {
expect(selectors.modalTitle()).toBeInTheDocument();
});

it('should enabled edit button when value is valid', async () => {
await userEvent.click(selectors.editBtn());

await userEvent.clear(selectors.capacityInput());
expect(selectors.editModalBtn()).toBeDisabled();
await userEvent.type(selectors.capacityInput(), '2.2');

expect(selectors.editModalBtn()).toBeEnabled();
});

it('should call mutate function when edit button is clicked', async () => {
fireEvent.click(selectors.editBtn());
fireEvent.change(selectors.capacityInput(), { target: { value: '200' } });
Expand Down
12 changes: 2 additions & 10 deletions src/react/ui-elements/Veeam/VeeamCapacityModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ import {
VEEAM_XML_PREFIX,
} from './VeeamConstants';
import { getCapacityBytes, useCapacityUnit } from './useCapacityUnit';
import { checkDecimals } from './VeeamConfiguration';

const schema = Joi.object({
capacity: Joi.number()
.required()
.min(1)
.max(1024)
.custom((value, helpers) => {
const stringValue = value.toString();
if (stringValue.includes('.')) {
const decimals = stringValue.split('.')[1];
if (decimals.length > 2) {
return helpers.message({
custom: '"capacity" must have at most 2 decimals',
});
}
}
return value;
return checkDecimals(value, helpers);
}),
capacityUnit: Joi.string().required(),
});
Expand Down
13 changes: 13 additions & 0 deletions src/react/ui-elements/Veeam/VeeamConfiguration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ describe('Veeam Configuration UI', () => {
expect(selectors.accountNameInput()).toHaveValue('Veeam');
});
});
it('should enabled continnue button if capacity value is valid', async () => {
//S
mockUseAccountsImplementation();
renderVeeamConfigurationForm();
await userEvent.type(selectors.accountNameInput(), 'Veeam');
await userEvent.type(selectors.repositoryInput(), 'veeam-bucket');
//E
await userEvent.clear(selectors.maxCapacityInput());
expect(selectors.continueButton()).toBeDisabled();
await userEvent.type(selectors.maxCapacityInput(), '2.2');
//V
expect(selectors.continueButton()).toBeEnabled();
});
it('should show validation error if the max capacity is less than 1', async () => {
//S
mockUseAccountsImplementation();
Expand Down
26 changes: 14 additions & 12 deletions src/react/ui-elements/Veeam/VeeamConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ import { useAccountsLocationsAndEndpoints } from '../../../react/next-architectu
import { useAccountsLocationsEndpointsAdapter } from '../../..//react/next-architecture/ui/AccountsLocationsEndpointsAdapterProvider';
import { VeeamLogo } from './VeeamLogo';

export const checkDecimals = (value: number, helpers: Joi.CustomHelpers) => {
const stringValue = value.toString();
if (stringValue.includes('.')) {
const decimals = stringValue.split('.')[1];
if (decimals.length > 2) {
return helpers.message({
custom: '"capacity" must have at most 2 decimals',
});
}
}
return value;
};

const schema = Joi.object({
accountName: accountNameValidationSchema,
bucketName: bucketNameValidationSchema,
Expand All @@ -50,18 +63,7 @@ const schema = Joi.object({
.required()
.min(1)
.max(1024)
.custom((value, helpers) => {
const stringValue = value.toString();
if (stringValue.includes('.')) {
const decimals = stringValue.split('.')[1];
if (decimals.length > 2) {
return helpers.message({
custom: '"capacity" must have at most 2 decimals',
});
}
}
return value;
}),
.custom((value, helpers) => checkDecimals(value, helpers)),
otherwise: Joi.valid(),
}),
capacityUnit: Joi.when('application', {
Expand Down

0 comments on commit 35b1a61

Please sign in to comment.