|
| 1 | +import { act, fireEvent, render } from '@testing-library/react-native'; |
| 2 | +import { TextInputOTP, TextInputOTPSlot } from '../components'; |
| 3 | +import type { TextInputOTPProps, TextInputOTPRef } from '../types'; |
| 4 | +import { createRef } from 'react'; |
| 5 | + |
| 6 | +function renderTextInputOTP({ |
| 7 | + maxLength = 6, |
| 8 | + ...rest |
| 9 | +}: Partial<TextInputOTPProps>) { |
| 10 | + return render( |
| 11 | + <TextInputOTP maxLength={maxLength} {...rest}> |
| 12 | + <TextInputOTPSlot index={0} /> |
| 13 | + <TextInputOTPSlot index={1} /> |
| 14 | + <TextInputOTPSlot index={2} /> |
| 15 | + <TextInputOTPSlot index={3} /> |
| 16 | + <TextInputOTPSlot index={4} /> |
| 17 | + <TextInputOTPSlot index={5} /> |
| 18 | + </TextInputOTP> |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +describe('TextInputOTP Component', () => { |
| 23 | + beforeAll(() => { |
| 24 | + jest.resetAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should call onFilled with the complete code when all digits are filled', () => { |
| 28 | + const CODE = '123456'; |
| 29 | + const mockedOnFilled = jest.fn(); |
| 30 | + const view = renderTextInputOTP({ onFilled: mockedOnFilled }); |
| 31 | + fireEvent.changeText(view.getByTestId('hidden-text-input'), CODE); |
| 32 | + expect(mockedOnFilled).toHaveBeenCalledWith(CODE); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should not render the animated caret when the caretHidden prop is true', () => { |
| 36 | + const view = renderTextInputOTP({ caretHidden: true }); |
| 37 | + expect(view.queryByTestId('caret')).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should render the slots only up to the number defined by the maxLength prop', () => { |
| 41 | + const MAX_LENGTH = 6; |
| 42 | + const view = renderTextInputOTP({ |
| 43 | + maxLength: MAX_LENGTH, |
| 44 | + caretHidden: true, |
| 45 | + }); |
| 46 | + const slots = view.getAllByTestId('text-input-otp-slot'); |
| 47 | + expect(slots).toHaveLength(MAX_LENGTH); |
| 48 | + }); |
| 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 | + act(() => ref.current?.setValue(CODE)); |
| 62 | + expect(mockedOnFilled).toHaveBeenCalledWith(CODE); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should call clear the input text when clear function is called programmatically', () => { |
| 66 | + const CODE = '1'; |
| 67 | + const ref = createRef<TextInputOTPRef>(); |
| 68 | + const view = render( |
| 69 | + <TextInputOTP ref={ref} maxLength={3}> |
| 70 | + <TextInputOTPSlot index={0} /> |
| 71 | + <TextInputOTPSlot index={1} /> |
| 72 | + <TextInputOTPSlot index={2} /> |
| 73 | + </TextInputOTP> |
| 74 | + ); |
| 75 | + fireEvent.changeText(view.getByTestId('hidden-text-input'), CODE); |
| 76 | + act(() => ref.current?.clear()); |
| 77 | + expect(view.queryByText(CODE)).toBeFalsy(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments