Skip to content

Commit

Permalink
Update input tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cgero-eth committed Dec 11, 2023
1 parent e4d9aff commit dba5eec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/components/input/inputContainer/inputContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ describe('<InputContainer /> component', () => {
expect(screen.getByText(`[${inputLength}/${maxLength}]`)).toBeInTheDocument();
});

it('adds a shake animation when the input length is equal to the max length property', () => {
const maxLength = 10;
const inputLength = 10;
render(createTestComponent({ maxLength, inputLength }));
expect(screen.getByText(`[${inputLength}/${maxLength}]`).className).toContain('shake');
});

it('renders the input alert when defined', () => {
const alert = {
message: 'input-alert-message',
Expand Down
10 changes: 5 additions & 5 deletions src/components/input/inputContainer/inputContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const InputContainer: React.FC<IInputContainerProps> = (props) => {
variantToClassNames[processedVariant],
);

const counterClasses = classNames('text-sm font-normal leading-tight text-neutral-600', {
'animate-shake': inputLength === maxLength,
});

return (
<div className={classNames('flex grow flex-col gap-2 md:gap-3', className)}>
{(label != null || helpText != null) && (
Expand All @@ -71,11 +75,7 @@ export const InputContainer: React.FC<IInputContainerProps> = (props) => {
)}
<div className={containerClasses}>{children}</div>
{maxLength != null && (
<p
className={classNames('text-sm font-normal leading-tight text-neutral-600', {
'animate-shake': inputLength === maxLength,
})}
>
<p className={counterClasses}>
[{inputLength}/{maxLength}]
</p>
)}
Expand Down
24 changes: 21 additions & 3 deletions src/components/input/useInputProps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderHook } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react';
import type { ChangeEvent } from 'react';
import { useInputProps } from './useInputProps';

describe('useInputProps hook', () => {
Expand All @@ -8,14 +9,15 @@ describe('useInputProps hook', () => {
variant: 'warning' as const,
helpText: 'help-text',
isOptional: true,
infoText: 'info-text',
alert: { message: 'alert-message', variant: 'critical' as const },
className: 'container-classname',
maxLength: 10,
};

const inputProps = {
placeholder: 'input-placeholder',
value: 'input-value',
maxLength: 10,
};

const { result } = renderHook(() => useInputProps({ ...containerProps, ...inputProps }));
Expand All @@ -36,12 +38,28 @@ describe('useInputProps hook', () => {
const props = { isDisabled };
const { result } = renderHook(() => useInputProps(props));
expect(result.current.inputProps.disabled).toBeTruthy();
expect(result.current.inputProps.className).toContain('cursor-not-allowed');
});

it('sets a random id to the input property when the id prop is not set', () => {
const { result } = renderHook(() => useInputProps({}));
expect(result.current.containerProps.id).toBeDefined();
expect(result.current.inputProps.id).toBeDefined();
});

it('tracks the current input length for the container props', () => {
const newValue = 'newValue';
const changeEvent = { target: { value: newValue } } as ChangeEvent<HTMLInputElement>;
const { result } = renderHook(() => useInputProps({}));
expect(result.current.containerProps.inputLength).toEqual(0);
act(() => result.current.inputProps.onChange?.(changeEvent));
expect(result.current.containerProps.inputLength).toEqual(newValue.length);
});

it('calls the onChange property on input value change', () => {
const onChange = jest.fn();
const { result } = renderHook(() => useInputProps({ onChange }));
const changeEvent = { target: { value: '' } } as ChangeEvent<HTMLInputElement>;
act(() => result.current.inputProps.onChange?.(changeEvent));
expect(onChange).toHaveBeenCalledWith(changeEvent);
});
});

0 comments on commit dba5eec

Please sign in to comment.