Skip to content

Commit 491ac1a

Browse files
ElenaBitireyjose
authored andcommitted
refactor: refactor Input tests
1 parent 270f729 commit 491ac1a

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src/ui/input.test.tsx

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,47 @@ afterEach(cleanup);
99

1010
describe('Input component ', () => {
1111
it('renders correctly ', () => {
12-
render(<Input testID="text-input" />);
13-
const input = screen.getByTestId('text-input');
14-
expect(input).toBeOnTheScreen();
15-
expect(screen.getByTestId('text-input')).toBeOnTheScreen();
12+
render(<Input testID="input" />);
13+
expect(screen.getByTestId('input')).toBeOnTheScreen();
1614
});
1715

1816
it('should render the placeholder correctly ', () => {
19-
render(<Input testID="text-input" placeholder="Enter your username" />);
20-
expect(screen.getByTestId('text-input')).toBeOnTheScreen();
17+
render(<Input testID="input" placeholder="Enter your username" />);
18+
expect(screen.getByTestId('input')).toBeOnTheScreen();
2119
expect(
2220
screen.getByPlaceholderText('Enter your username')
2321
).toBeOnTheScreen();
2422
});
2523

2624
it('should render the label correctly ', () => {
27-
render(<Input testID="text-input" label="Username" />);
28-
expect(screen.getByTestId('text-input')).toBeOnTheScreen();
29-
expect(screen.getByTestId('text-input-label')).toBeOnTheScreen();
30-
expect(screen.getByTestId('text-input-label')?.props.children).toBe(
31-
'Username'
32-
);
25+
render(<Input testID="input" label="Username" />);
26+
expect(screen.getByTestId('input')).toBeOnTheScreen();
27+
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
28+
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');
3329
});
3430

3531
it('should render the error message correctly ', () => {
36-
render(<Input testID="text-input" error="This is an error message" />);
37-
expect(screen.getByTestId('text-input')).toBeOnTheScreen();
38-
expect(screen.getByTestId('text-input-error')).toBeOnTheScreen();
39-
expect(screen.getByTestId('text-input-error')?.props.children).toBe(
32+
render(<Input testID="input" error="This is an error message" />);
33+
expect(screen.getByTestId('input')).toBeOnTheScreen();
34+
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
35+
expect(screen.getByTestId('input-error')?.props.children).toBe(
4036
'This is an error message'
4137
);
4238
});
4339
it('should render the label, error message & placeholder correctly ', () => {
4440
render(
4541
<Input
46-
testID="text-input"
42+
testID="input"
4743
label="Username"
4844
placeholder="Enter your username"
4945
error="This is an error message"
5046
/>
5147
);
52-
expect(screen.getByTestId('text-input')).toBeOnTheScreen();
53-
expect(screen.getByTestId('text-input-label')).toBeOnTheScreen();
54-
expect(screen.getByTestId('text-input-label')?.props.children).toBe(
55-
'Username'
56-
);
57-
expect(screen.getByTestId('text-input-error')).toBeOnTheScreen();
58-
expect(screen.getByTestId('text-input-error')?.props.children).toBe(
48+
expect(screen.getByTestId('input')).toBeOnTheScreen();
49+
expect(screen.getByTestId('input-label')).toBeOnTheScreen();
50+
expect(screen.getByTestId('input-label')?.props.children).toBe('Username');
51+
expect(screen.getByTestId('input-error')).toBeOnTheScreen();
52+
expect(screen.getByTestId('input-error')?.props.children).toBe(
5953
'This is an error message'
6054
);
6155
expect(
@@ -65,7 +59,7 @@ describe('Input component ', () => {
6559

6660
it('should trigger onFocus event correctly ', () => {
6761
const onFocus = jest.fn();
68-
render(<Input onFocus={onFocus} />);
62+
render(<Input testID="input" onFocus={onFocus} />);
6963

7064
const input = screen.getByTestId('input');
7165
fireEvent(input, 'focus');
@@ -74,25 +68,25 @@ describe('Input component ', () => {
7468

7569
it('should trigger onBlur event correctly ', () => {
7670
const onBlur = jest.fn();
77-
render(<Input onBlur={onBlur} />);
71+
render(<Input testID="input" onBlur={onBlur} />);
7872

7973
const input = screen.getByTestId('input');
8074
fireEvent(input, 'blur');
8175
expect(onBlur).toHaveBeenCalledTimes(1);
8276
});
8377
it('should trigger onChangeText event correctly', () => {
8478
const onChangeText = jest.fn();
85-
render(<Input onChangeText={onChangeText} />);
79+
render(<Input testID="input" onChangeText={onChangeText} />);
8680

8781
const input = screen.getByTestId('input');
8882
fireEvent.changeText(input, 'test text');
8983
expect(onChangeText).toHaveBeenCalledTimes(1);
9084
expect(onChangeText).toHaveBeenCalledWith('test text');
9185
});
9286
it('should be disabled when disabled prop is true', () => {
93-
render(<Input testID="text-input" disabled={true} />);
87+
render(<Input testID="input" disabled={true} />);
9488

95-
const input = screen.getByTestId('text-input');
89+
const input = screen.getByTestId('input');
9690
expect(input.props.disabled).toBe(true);
9791
});
9892
});

src/ui/input.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ interface ControlledInputProps<T extends FieldValues>
7070
InputControllerType<T> {}
7171

7272
export const Input = React.forwardRef<TextInput, NInputProps>((props, ref) => {
73-
const { label, error, testID = 'input', ...inputProps } = props;
73+
const { label, error, testID, ...inputProps } = props;
7474
const [isFocussed, setIsFocussed] = React.useState(false);
7575
const onBlur = React.useCallback(() => setIsFocussed(false), []);
7676
const onFocus = React.useCallback(() => setIsFocussed(true), []);
@@ -88,7 +88,10 @@ export const Input = React.forwardRef<TextInput, NInputProps>((props, ref) => {
8888
return (
8989
<View className={styles.container()}>
9090
{label && (
91-
<Text testID={`${testID}-label`} className={styles.label()}>
91+
<Text
92+
testID={testID ? `${testID}-label` : undefined}
93+
className={styles.label()}
94+
>
9295
{label}
9396
</Text>
9497
)}
@@ -107,7 +110,7 @@ export const Input = React.forwardRef<TextInput, NInputProps>((props, ref) => {
107110
/>
108111
{error && (
109112
<Text
110-
testID={`${testID}-error`}
113+
testID={testID ? `${testID}-error` : undefined}
111114
className="text-sm text-danger-400 dark:text-danger-600"
112115
>
113116
{error}

0 commit comments

Comments
 (0)