From ff085d84fc33bc1de77b07864c24c519a274c14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Dombya?= <135591453+hervedombya@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:07:04 +0100 Subject: [PATCH] Update input buttons and input list component --- src/lib/components/inputlist/InputButtons.tsx | 4 +- .../inputlist/InputList.component.tsx | 51 ++++----- .../components/inputlist/InputList.test.tsx | 102 ++++++++++++++++++ 3 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 src/lib/components/inputlist/InputList.test.tsx diff --git a/src/lib/components/inputlist/InputButtons.tsx b/src/lib/components/inputlist/InputButtons.tsx index 50e8a49ef9..6179c773ad 100644 --- a/src/lib/components/inputlist/InputButtons.tsx +++ b/src/lib/components/inputlist/InputButtons.tsx @@ -62,7 +62,7 @@ export const AddButton = ({ name={`addbtn${index}`} id={`addbtn${index}`} onClick={onClickFn} - aria-label="Add" + aria-label={`Add${index}`} tooltip={{ overlay: 'Add', placement: 'top', @@ -97,7 +97,7 @@ export const SubButton = ({ variant="danger" type="button" disabled={isDisabled} - aria-label="Remove" + aria-label={`Remove${index}`} name={`delbtn${index}`} id={`delbtn${index}`} onClick={() => deleteEntry(index)} diff --git a/src/lib/components/inputlist/InputList.component.tsx b/src/lib/components/inputlist/InputList.component.tsx index 21b08e1567..a453fb9e08 100644 --- a/src/lib/components/inputlist/InputList.component.tsx +++ b/src/lib/components/inputlist/InputList.component.tsx @@ -19,22 +19,24 @@ export type InputListProps = HTMLProps & { function InternalInputList< T extends string | number | readonly string[] | undefined, ->({ - onChange, - onBlur, - ref, - min, - max, - maxLength, - minLength, - pattern, - required, - disabled, - maxItems, - id, - value, - ...rest -}: InputListProps) { +>( + { + onChange, + onBlur, + min, + max, + maxLength, + minLength, + pattern, + required, + disabled, + maxItems, + value, + name, + ...rest + }: InputListProps, + _, +) { const isMaxItemsReached = maxItems !== undefined && maxItems !== null && value.length === maxItems; @@ -47,13 +49,11 @@ function InternalInputList< }; const deleteEntry = (entryIndex: number) => { - let tempValues = [...value]; - tempValues.splice(entryIndex, 1); - if (tempValues.length === 0) { - tempValues = [''] as T[]; - } + const newValues = value.filter((_, index) => index !== entryIndex); + const updatedValues = newValues.length === 0 ? ([''] as T[]) : newValues; + onChange?.({ - target: { value: tempValues }, + target: { value: updatedValues }, } as unknown as React.ChangeEvent); }; @@ -62,7 +62,8 @@ function InternalInputList< {value.map((val, index) => ( { @@ -76,14 +77,14 @@ function InternalInputList< /> { + const onChangeMock = jest.fn(); + + const renderInputList = (props: InputListProps) => { + render( + + + + + , + ); + }; + + beforeEach(() => { + onChangeMock.mockClear(); + }); + + it('should render an empty input list', () => { + renderInputList({ + value: [''], + }); + + expect(screen.getByLabelText('inputListTest0')).toHaveValue(''); + }); + + it('should render an input list with initial values', () => { + const initialValues = ['Value 1', 'Value 2', 'Value 3']; + + renderInputList({ + value: initialValues, + }); + + const inputElements = screen.getAllByRole('textbox'); + + expect(inputElements).toHaveLength(initialValues.length); + + inputElements.forEach((input, index) => { + expect(input).toHaveValue(initialValues[index]); + }); + }); + + it('should add a new input when clicking the add button', () => { + const initialValues = ['Value 1', 'Value 2']; + + renderInputList({ + value: initialValues, + }); + + const addButton = screen.getByLabelText('Add1'); + + fireEvent.click(addButton); + + expect(onChangeMock).toHaveBeenCalledWith({ + target: { value: [...initialValues, ''] }, + }); + }); + + it('should delete an input when clicking the delete button', () => { + const initialValues = ['Value 1', 'Value 2', 'Value 3']; + + renderInputList({ + value: initialValues, + }); + + const deleteButton = screen.getByLabelText('Remove1'); + + fireEvent.click(deleteButton); + + expect(onChangeMock).toHaveBeenCalledWith({ + target: { value: ['Value 1', 'Value 3'] }, + }); + }); + + it('should update the value of an input', () => { + const initialValues = ['Value 1', 'Value 2', 'Value 3']; + + renderInputList({ + value: initialValues, + }); + + const inputElements = screen.getAllByRole('textbox'); + + const newValue = 'New Value'; + + fireEvent.change(inputElements[1], { target: { value: newValue } }); + + expect(onChangeMock).toHaveBeenCalledWith({ + target: { value: ['Value 1', newValue, 'Value 3'] }, + }); + }); +});