Skip to content

Commit e4c9e45

Browse files
committed
test(text-input-otp): add test for maxLength prop and improve existing tests
- refactor render function to destructure props with default maxLength - add test to verify slots rendered match maxLength prop - add testID to TextInputOTPSlot component for easier testing - improve existing test descriptions for clarity
1 parent dbbf7ca commit e4c9e45

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { fireEvent, render as TLRender } from '@testing-library/react-native';
22
import { TextInputOTP, TextInputOTPSlot } from '../components';
33
import type { TextInputOTPProps } from '../types';
44

5-
function render(props?: Partial<TextInputOTPProps>) {
5+
function render({ maxLength = 6, ...rest }: Partial<TextInputOTPProps>) {
66
return TLRender(
7-
<TextInputOTP maxLength={6} {...props}>
7+
<TextInputOTP maxLength={maxLength} {...rest}>
88
<TextInputOTPSlot index={0} />
99
<TextInputOTPSlot index={1} />
1010
<TextInputOTPSlot index={2} />
@@ -28,8 +28,15 @@ describe('TextInputOTP Component', () => {
2828
expect(mockedOnFilled).toHaveBeenCalledWith(CODE);
2929
});
3030

31-
it('should not render the animated caret when caretHidden prop is true', () => {
31+
it('should not render the animated caret when the caretHidden prop is true', () => {
3232
const view = render({ caretHidden: true });
3333
expect(view.queryByTestId('caret')).toBeNull();
3434
});
35+
36+
it('should render the slots only up to the number defined by the maxLength prop', async () => {
37+
const MAX_LENGTH = 6;
38+
const view = render({ maxLength: MAX_LENGTH, caretHidden: true });
39+
const slots = view.getAllByTestId('text-input-otp-slot');
40+
expect(slots).toHaveLength(MAX_LENGTH);
41+
});
3542
});

src/components/text-input-otp-slot.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function TextInputOTPSlotComponent({
2626

2727
return (
2828
<Pressable
29+
testID="text-input-otp-slot"
2930
onPress={handlePress}
3031
style={StyleSheet.flatten([
3132
styles.slot,

0 commit comments

Comments
 (0)