|
| 1 | +import React from "react"; |
| 2 | +import { render, screen, fireEvent, act } from "@testing-library/react-native"; |
| 3 | +import { View } from "react-native"; |
| 4 | +import { CodeInput, CodeInputText } from "../../components/CodeInput"; |
| 5 | +import { Cursor } from "react-native-confirmation-code-field"; |
| 6 | + |
| 7 | +describe("CodeInput tests", () => { |
| 8 | + test("should onInputFull be called when input is full", () => { |
| 9 | + const cellCount = 6; |
| 10 | + const text = "0".repeat(cellCount); |
| 11 | + const onInputFull = jest.fn(); |
| 12 | + |
| 13 | + const Wrapper: React.FC = () => { |
| 14 | + const [value, setValue] = React.useState(""); |
| 15 | + return ( |
| 16 | + <CodeInput |
| 17 | + value={value} |
| 18 | + onChangeText={(text) => setValue(text)} |
| 19 | + cellCount={cellCount} |
| 20 | + onInputFull={onInputFull} |
| 21 | + /> |
| 22 | + ); |
| 23 | + }; |
| 24 | + |
| 25 | + render(<Wrapper />); |
| 26 | + |
| 27 | + const textInput = screen.getByTestId("native-text-input"); |
| 28 | + act(() => fireEvent.changeText(textInput, text)); |
| 29 | + |
| 30 | + expect(onInputFull).toHaveBeenCalledTimes(1); |
| 31 | + expect(onInputFull).toHaveBeenCalledWith(text); |
| 32 | + }); |
| 33 | + |
| 34 | + test.each([2, 4, 6, 7, 8])( |
| 35 | + "should render %s custom input cells", |
| 36 | + (cellCount) => { |
| 37 | + render( |
| 38 | + <CodeInput |
| 39 | + renderItem={() => <View testID="test-input-cell" />} |
| 40 | + cellCount={cellCount} |
| 41 | + /> |
| 42 | + ); |
| 43 | + |
| 44 | + const cells = screen.queryAllByTestId("test-input-cell"); |
| 45 | + expect(cells).toHaveLength(cellCount); |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + test.each([2, 4, 6, 7, 8])( |
| 50 | + "should render %s default input cells when renderItem not provided", |
| 51 | + (cellCount) => { |
| 52 | + render(<CodeInput cellCount={cellCount} />); |
| 53 | + |
| 54 | + const cells = screen.queryAllByTestId("default-code-input-cell"); |
| 55 | + expect(cells).toHaveLength(cellCount); |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + describe("CodeInputText tests", () => { |
| 60 | + test("should render cursor when focused and does not have a value", () => { |
| 61 | + render(<CodeInputText isFocused />); |
| 62 | + |
| 63 | + const cursor = screen.UNSAFE_queryByType(Cursor); |
| 64 | + expect(cursor).toBeTruthy(); |
| 65 | + }); |
| 66 | + |
| 67 | + test("should render text value when focused and has a value", () => { |
| 68 | + const text = "sample text"; |
| 69 | + render(<CodeInputText isFocused>{text}</CodeInputText>); |
| 70 | + |
| 71 | + const cursor = screen.UNSAFE_queryByType(Cursor); |
| 72 | + const componentWithText = screen.queryByText(text); |
| 73 | + expect(componentWithText).toBeTruthy(); |
| 74 | + expect(cursor).toBeFalsy(); |
| 75 | + }); |
| 76 | + |
| 77 | + test("should render text value when not focused and has a value", () => { |
| 78 | + const text = "sample text"; |
| 79 | + render(<CodeInputText isFocused={false}>{text}</CodeInputText>); |
| 80 | + |
| 81 | + const cursor = screen.UNSAFE_queryByType(Cursor); |
| 82 | + const componentWithText = screen.queryByText(text); |
| 83 | + expect(componentWithText).toBeTruthy(); |
| 84 | + expect(cursor).toBeFalsy(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments