Knowledge Base (Text Input Component)

Please review knowledge base, if you cannot find the answer you are looking for? Reach out to our support team by opening forum thread.

1. React Native TextInput Sample Code

some basic test cases for a React Native Text Input Component using Jest

 

// TextInputComponent.js

import React, { useState } from 'react';
import { TextInput, View, Button, Text } from 'react-native';

const MyTextInput = () => {
  const [text, setText] = useState('');

  const handleInputChange = (inputText) => {
    setText(inputText);
  };

  const handleButtonPress = () => {
    // Perform some action with the input text
    console.log(`Input Text: ${text}`);
  };

  return (
    <View>
      <TextInput
        placeholder="Type something..."
        value={text}
        onChangeText={handleInputChange}
      />
      <Button title="Submit" onPress={handleButtonPress} />
    </View>
  );
};

export default MyTextInput;
2. Handles input change
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('handles input change', () => {
    // Render the MyTextInput component
    const { getByPlaceholderText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');

    // Simulate entering text into the input
    fireEvent.changeText(input, 'Hello, World!');

    // Assert that the input value reflects the entered text
    expect(input.props.value).toBe('Hello, World!');
  });
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText function from the returned object.
  2. Get the Input Element:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
  3. Simulate Entering Text:

    • fireEvent.changeText(input, 'Hello, World!');: Simulating entering the text 'Hello, World!' into the input.
  4. Assertion:

    • expect(input.props.value).toBe('Hello, World!');: Asserting that the value prop of the input reflects the entered text. This ensures that the MyTextInput component correctly handles and retains the entered input.
3. Executes button press
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('executes button press', () => {
    // Render the MyTextInput component
    const { getByPlaceholderText, getByText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');
    const button = getByText('Submit');

    // Simulate entering text into the input
    fireEvent.changeText(input, 'Hello, World!');

    // Simulate pressing the submit button
    fireEvent.press(button);

  });
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText, getByText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText and getByText functions from the returned object.
  2. Get Elements:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
    • const button = getByText('Submit');: Using getByText to get the button element by its text content.
  3. Simulate Entering Text:

    • fireEvent.changeText(input, 'Hello, World!');: Simulating entering the text 'Hello, World!' into the input.
  4. Simulate Button Press:

    • fireEvent.press(button);: Simulating pressing the submit button
    •  
4. Renders with default empty value
// TextInputComponent.test.js
import React from 'react';
import { render } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('renders with default empty value', () => {
    // Render the MyTextInput component
    const { getByPlaceholderText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');

    // Assert that the input value is an empty string by default
    expect(input.props.value).toBe('');
  });
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText function from the returned object.
  2. Get the Input Element:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
  3. Assertion:

    • expect(input.props.value).toBe('');: Asserting that the value prop of the input is an empty string by default. This ensures that the component is initially rendered with an empty input value.
5. Handles empty input correctly
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('handles empty input correctly', () => {
    // Render the MyTextInput component
    const { getByPlaceholderText, getByText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');
    const button = getByText('Submit');

    // Simulate a button press without entering any text
    fireEvent.press(button);

    // Assertions to check how the component handles empty input
    // Example assertions (customize based on your component's behavior):
    // 1. Check if an error message is displayed
    //    const errorMessage = getByText('Please enter something.');
    //    expect(errorMessage).toBeTruthy();

    // 2. Check if a specific callback function is not called
    //    const mockCallback = jest.fn();
    //    expect(mockCallback).not.toHaveBeenCalled();
  });
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText, getByText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText and getByText functions from the returned object.
  2. Get Elements:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
    • const button = getByText('Submit');: Using getByText to get the button element by its text content.
  3. Simulate Button Press:

    • fireEvent.press(button);: Simulating a button press without entering any text into the input.
  4. Assertions:

    • Add your specific assertions based on the behavior you expect when handling empty input. In the example, I've included two scenarios:
      • Checking if an error message is displayed when no text is entered.
      • Checking if a specific callback function is not called.

 

 

6. Handles input with special characters
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('handles input with special characters', () => {
    const { getByPlaceholderText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');
    fireEvent.changeText(input, 'Special!@#$%^&*()_+-={}[]|:;"<>,.?/~`');
    expect(input.props.value).toBe('Special!@#$%^&*()_+-={}[]|:;"<>,.?/~`');
  });
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText function from the returned object.
  2. Get the Input Element:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
  3. Simulate Entering Special Characters:

    • fireEvent.changeText(input, 'Special!@#$%^&*()_+-={}[]|:;"<>,.?/~');: Simulating entering a string with various special characters into the input using fireEvent.changeText`.
  4. Assertion:

    • expect(input.props.value).toBe('Special!@#$%^&*()_+-={}[]|:;"<>,.?/~');: Asserting that the valueprop of the input reflects the entered string with special characters. This checks whether theMyTextInput` component correctly handles and retains input with special characters.
7. Renders without crashing
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
 it('renders without crashing', () => {
  // Render the component
  const { getByPlaceholderText, getByText } = render(<MyTextInput />);

  // Get the input element by its placeholder text
  const input = getByPlaceholderText('Type something...');

  // Get the button element by its text content
  const button = getByText('Submit');

  // Assertions to check if the component renders without crashing
  expect(input).toBeInTheDocument();
  expect(button).toBeInTheDocument();
  expect(input).toBeVisible();
  expect(button).toBeVisible();
});
});

 

Explanation:

  1. Render the Component:

    • const { getByPlaceholderText, getByText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText and getByText functions from the returned object.
  2. Get Elements:

    • const input = getByPlaceholderText('Type something...');: Using getByPlaceholderText to get the input element by its placeholder text.
    • const button = getByText('Submit');: Using getByText to get the button element by its text content.
  3. Assertions:

    • expect(input).toBeInTheDocument();: Asserting that the input element is in the document.
    • expect(button).toBeInTheDocument();: Asserting that the button element is in the document.
    • expect(input).toBeVisible();: Asserting that the input element is visible.
    • expect(button).toBeVisible();: Asserting that the button element is visible.
8. Displays an error message for invalid input
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
  it('displays an error message for invalid input', () => {
    const { getByPlaceholderText, getByText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');
    const button = getByText('Submit');
    fireEvent.changeText(input, 'Invalid Input');
    fireEvent.press(button);
  });
});

 

Test Logic:

  • const errorMessage = getByText('Invalid input. Please try again.');: Using the getByText function to get the element that should contain the error message. Adjust the text content based on what your actual error message is.

  • expect(errorMessage).toBeTruthy();: Asserting that the errorMessage element exists and is truthy. If the error message element is found, the test will pass; otherwise, it will fail.

9. Renders the submit button
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
 it('renders the submit button', () => {
    const { getByText } = render(<MyTextInput />);
    const button = getByText('Submit');
    expect(button).toBeTruthy();
  });
});

 

Test Logic:

  • const { getByText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByText function from the returned object. This function allows you to select elements based on their text content.

  • const button = getByText('Submit');: Using the getByText function to get the button element with the text content 'Submit'.

  • expect(button).toBeTruthy();: Asserting that the button element exists and is truthy. If the button element is found, the test will pass; otherwise, it will fail.

10. Handles input with numbers
// TextInputComponent.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MyTextInput from './TextInputComponent';

describe('MyTextInput component', () => {
 it('handles input with numbers', () => {
    const { getByPlaceholderText } = render(<MyTextInput />);
    const input = getByPlaceholderText('Type something...');
    fireEvent.changeText(input, '12345');
    expect(input.props.value).toBe('12345');
  });
});

 

Test Logic:

  • const { getByPlaceholderText } = render(<MyTextInput />);: Rendering the MyTextInput component and destructuring the getByPlaceholderText function from the returned object. This function allows you to select elements based on their placeholder text.

  • const input = getByPlaceholderText('Type something...');: Using the getByPlaceholderText function to get the input element with the placeholder text 'Type something...'.

  • fireEvent.changeText(input, '12345');: Simulating a change in the input text by firing the changeText event with a string that includes numbers.

  • expect(input.props.value).toBe('12345');: Asserting that the value prop of the input reflects the text with numbers. This checks whether the MyTextInput component correctly handles and retains the input with numbers.