Search code examples
reactjsreact-nativeenzymereact-native-textinputreact-native-state

TextInput onChangeText set useState value is not working in React Native Testing with Enzyme


I want to test that user should not navigate to any other screen if TextInput equals to "".

I have tried to put console logs to understand more correctly but the problem is now that, after input.simulate("changeText", "[email protected]"); happened correctly, console log says that incoming value from test is "[email protected]" but after setValue(text); happened console.log says that useState value is "".

Why state is not changing? Do I need to update the wrapper with .update() ?

Here is my React Native code:

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

export const LoginContainer = (props) => {
  const [value, setValue] = useState("");

  const handleClick = () => {
    if (value !== "") {
      props.navigation.navigate("any other screen");
    }
  }

  return (
    <View>
      <TextInput
        test-id="login-input"
        onChangeText={(text) => {
          console.log("INPUT_TEXT:", text);
          setValue(text);
          console.log("STATE_TEXT:", value);
        }}
        value={value}
      />
      <Button test-id="login-button" onPress={() => handleClick()} />
    </View>
  );
}

Console log:

  console.log
    INPUT_TEXT: [email protected]

      at onChangeText (....)

  console.log
    STATE_TEXT: 

      at onChangeText (....)

  expect(jest.fn()).toHaveBeenCalledTimes(expected)

  Expected number of calls: 1
  Received number of calls: 0

Test code:

import "react-native";
import React from "react";
import { shallow } from 'enzyme';
import { LoginContainer } from "...";
import { findByTestAttr } from '...';

const navigation = {
  navigate: jest.fn()
}

describe('correct login action', () => {
    const wrapper = shallow(<LoginContainer navigation={navigation} />);
    let input = findByTestAttr(wrapper, "login-input");
    let button = findByTestAttr(wrapper, "login-button");

    test('should not navigate to login mail screen if email adress is not entered', () => {
      input.simulate("changeText", "[email protected]");
      button.simulate("press");

      expect(navigation.navigate).toHaveBeenCalledTimes(1);

      //input.simulate("changeText", "");
      //button.simulate("press");
      //expect(navigation.navigate).toHaveBeenCalledTimes(0);
    });
});

Solution

  • I solved this ussie with the solution in here.

    test('should not navigate to login mail screen if email adress is not entered', () => {
        const wrapper = shallow(<LoginContainer navigation={navigation} />);
        input = findByTestAttr(wrapper, "login-input");
    
        input.simulate("changeText", "[email protected]");
        wrapper.update(); // update after changing the state
       
        // After performing update, should find button element
        button = findByTestAttr(wrapper, "login-button");
        button.simulate("press");
        expect(navigation.navigate).toHaveBeenCalledTimes(1);
    });