From d67d30c5a2139be5d9b2b314d3e68fb7373064e4 Mon Sep 17 00:00:00 2001 From: Lio B Date: Thu, 31 Oct 2024 17:42:48 +0100 Subject: [PATCH] feat(pci-savings-plan): avoid exceeding max quantity (#13565) ref: TAPC-1697 Signed-off-by: Lionel Bueno --- .../CreatePlanForm/CreatePlanForm.tsx | 12 +++-- .../QuantitySelector.test.tsx | 54 +++++++++++++++++++ .../QuantitySelector/QuantitySelector.tsx | 6 ++- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.test.tsx diff --git a/packages/manager/apps/pci-savings-plan/src/components/CreatePlanForm/CreatePlanForm.tsx b/packages/manager/apps/pci-savings-plan/src/components/CreatePlanForm/CreatePlanForm.tsx index 2c93a876ed6a..0f280316afe0 100644 --- a/packages/manager/apps/pci-savings-plan/src/components/CreatePlanForm/CreatePlanForm.tsx +++ b/packages/manager/apps/pci-savings-plan/src/components/CreatePlanForm/CreatePlanForm.tsx @@ -39,7 +39,9 @@ import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; import { MutationStatus, useMutationState } from '@tanstack/react-query'; -import QuantitySelector from '@/components/QuantitySelector/QuantitySelector'; +import QuantitySelector, { + MAX_QUANTITY, +} from '@/components/QuantitySelector/QuantitySelector'; import useTechnicalInfo, { usePricingInfo } from '@/hooks/useCatalogCommercial'; import { getMutationKeyCreateSavingsPlan, @@ -334,8 +336,12 @@ const CreatePlanForm: FC = ({ setQuantity(quantity - 1)} - onPlusClick={() => setQuantity(quantity + 1)} + onMinusClick={() => setQuantity((prevQ) => prevQ - 1)} + onPlusClick={() => { + if (quantity <= MAX_QUANTITY) { + setQuantity((prevQ) => prevQ + 1); + } + }} onChangeQuantity={onChangeQuantity} /> diff --git a/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.test.tsx b/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.test.tsx new file mode 100644 index 000000000000..c5d766d5f81d --- /dev/null +++ b/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.test.tsx @@ -0,0 +1,54 @@ +import React from 'react'; +import { vi, describe, it, expect } from 'vitest'; +import { screen, fireEvent } from '@testing-library/react'; +import QuantitySelector, { MAX_QUANTITY } from './QuantitySelector'; +import '@testing-library/jest-dom'; + +import { render } from '@/utils/testProvider'; + +const defaultProps = { + quantity: 1, + onMinusClick: vi.fn(), + onPlusClick: vi.fn(), + onChangeQuantity: vi.fn(), +}; + +const setupSpecTest = async (props = defaultProps) => + render(); + +describe('QuantitySelector', () => { + it('should render the quantity input with the correct value', async () => { + await setupSpecTest(); + const input = screen.getByTestId('input-quantity'); + expect(input).toHaveValue(defaultProps.quantity); + }); + + it('should call onMinusClick when minus button is clicked', async () => { + await setupSpecTest(); + const minusButton = screen.getByTestId('minus-button'); + fireEvent.click(minusButton); + expect(defaultProps.onMinusClick).toHaveBeenCalled(); + }); + + it('should call onPlusClick when plus button is clicked', async () => { + await setupSpecTest(); + const plusButton = screen.getByTestId('plus-button'); + fireEvent.click(plusButton); + expect(defaultProps.onPlusClick).toHaveBeenCalled(); + }); + + it('should call onChangeQuantity with the correct value when input changes', async () => { + await setupSpecTest(); + const input = screen.getByTestId('input-quantity'); + fireEvent.change(input, { target: { value: '5' } }); + expect(defaultProps.onChangeQuantity).toHaveBeenCalledWith(5); + }); + it('should not call onChangeQuantity with a value greater than MAX_QUANTITY', async () => { + await setupSpecTest(); + const input = screen.getByTestId('input-quantity'); + fireEvent.change(input, { target: { value: MAX_QUANTITY + 1 } }); + expect(defaultProps.onChangeQuantity).not.toHaveBeenCalledWith( + MAX_QUANTITY + 1, + ); + }); +}); diff --git a/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.tsx b/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.tsx index 29f83e1ea472..34f15da06893 100644 --- a/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.tsx +++ b/packages/manager/apps/pci-savings-plan/src/components/QuantitySelector/QuantitySelector.tsx @@ -23,6 +23,8 @@ type QuantitySelectorProps = { onChangeQuantity: (v?: number) => void; }; +export const MAX_QUANTITY = 1000; + const QuantitySelector: React.FC = ({ quantity, onMinusClick, @@ -31,6 +33,7 @@ const QuantitySelector: React.FC = ({ }) => ( = ({ = ({ value={quantity} onOdsValueChange={(e) => { const value = Number(e.detail.value); - if (value > 0) { + if (value > 0 && value <= MAX_QUANTITY) { onChangeQuantity(value); } }}