Skip to content

Commit 371889e

Browse files
committed
test(text-input-otp): add test for setValue method and update existing tests
- add test case for setValue method - update renderTextInputOTP function - import act and createRef - add onFilled callback in setValue method
1 parent 67c72f2 commit 371889e

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/__tests__/text-input-otp.test.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { fireEvent, render as TLRender } from '@testing-library/react-native';
1+
import { act, fireEvent, render } from '@testing-library/react-native';
22
import { TextInputOTP, TextInputOTPSlot } from '../components';
3-
import type { TextInputOTPProps } from '../types';
3+
import type { TextInputOTPProps, TextInputOTPRef } from '../types';
4+
import { createRef } from 'react';
45

5-
function render({ maxLength = 6, ...rest }: Partial<TextInputOTPProps>) {
6-
return TLRender(
6+
function renderTextInputOTP({
7+
maxLength = 6,
8+
...rest
9+
}: Partial<TextInputOTPProps>) {
10+
return render(
711
<TextInputOTP maxLength={maxLength} {...rest}>
812
<TextInputOTPSlot index={0} />
913
<TextInputOTPSlot index={1} />
@@ -23,20 +27,40 @@ describe('TextInputOTP Component', () => {
2327
it('should call onFilled with the complete code when all digits are filled', () => {
2428
const CODE = '123456';
2529
const mockedOnFilled = jest.fn();
26-
const view = render({ onFilled: mockedOnFilled });
30+
const view = renderTextInputOTP({ onFilled: mockedOnFilled });
2731
fireEvent.changeText(view.getByTestId('hidden-text-input'), CODE);
2832
expect(mockedOnFilled).toHaveBeenCalledWith(CODE);
2933
});
3034

3135
it('should not render the animated caret when the caretHidden prop is true', () => {
32-
const view = render({ caretHidden: true });
36+
const view = renderTextInputOTP({ caretHidden: true });
3337
expect(view.queryByTestId('caret')).toBeNull();
3438
});
3539

3640
it('should render the slots only up to the number defined by the maxLength prop', () => {
3741
const MAX_LENGTH = 6;
38-
const view = render({ maxLength: MAX_LENGTH, caretHidden: true });
42+
const view = renderTextInputOTP({
43+
maxLength: MAX_LENGTH,
44+
caretHidden: true,
45+
});
3946
const slots = view.getAllByTestId('text-input-otp-slot');
4047
expect(slots).toHaveLength(MAX_LENGTH);
4148
});
49+
50+
it('should call onFilled with the complete code when setValue is called programmatically', () => {
51+
const CODE = '123';
52+
const mockedOnFilled = jest.fn();
53+
const ref = createRef<TextInputOTPRef>();
54+
render(
55+
<TextInputOTP ref={ref} maxLength={3} onFilled={mockedOnFilled}>
56+
<TextInputOTPSlot index={0} />
57+
<TextInputOTPSlot index={1} />
58+
<TextInputOTPSlot index={2} />
59+
</TextInputOTP>
60+
);
61+
62+
act(() => ref.current?.setValue(CODE));
63+
64+
expect(mockedOnFilled).toHaveBeenCalledWith(CODE);
65+
});
4266
});

src/hooks/use-text-input-otp.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function TextInputOTPProvider({
7070
text.length > maxLength ? text.slice(0, maxLength) : text;
7171
setCode(filledCode);
7272
setCurrentIndex(maxLength - 1);
73+
onFilled?.(filledCode);
7374
},
7475
[maxLength]
7576
);

0 commit comments

Comments
 (0)