1- import { fireEvent , render as TLRender } from '@testing-library/react-native' ;
1+ import { act , fireEvent , render } from '@testing-library/react-native' ;
22import { 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} ) ;
0 commit comments