Skip to content

Unable to test isFocused on TextInput #276

@Eldjotnar

Description

@Eldjotnar

Ask your Question

I'm attempting to test the native methods of TextInput, but am unable to test isFocused or clear, but focus and blur work fine.

My test file:

// Sample.test.js
import React, { createRef } from 'react';
import { render } from 'react-native-testing-library';
import { TextInput } from 'react-native';

describe('@input: native methods', () => {
  it('* blur', () => {
    const componentRef = createRef();
    render(<TextInput ref={componentRef} />);

    expect(componentRef.current?.blur).toBeTruthy();
  });

  it('* isFocused', () => {
    const componentRef = createRef();
    render(<TextInput ref={componentRef} />);

    expect(componentRef.current?.isFocused).toBeTruthy();
  });
});

* blur passes, but * isFocused fails:

expect(received).toBeTruthy()

    Received: undefined

      15 |     render(<TextInput ref={componentRef} />);
      16 |
    > 17 |     expect(componentRef.current?.isFocused).toBeTruthy();
         |                                             ^
      18 |   });
      19 | });
      20 |

package.json has:

"dependencies": {
    "@react-native-community/masked-view": "^0.1.7",
    "react": "16.11.0",
    "react-dom": "16.11.0",
    "react-is": "^16.8",
    "react-native": "0.62.1",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "^1.7.1",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-safe-area-view": "^1.0.0",
    "react-native-screens": "^2.4.0",
    "styled-components": "^5.0.1",
    "styled-system": "^5.1.5"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@commitlint/cli": "^8.3.5",
    "@commitlint/config-conventional": "^8.3.4",
    "@react-native-community/eslint-config": "^1.0.0",
    "@types/jest": "^24.0.24",
    "@types/react-native": "^0.62.0",
    "@types/react-test-renderer": "16.9.2",
    "@types/styled-components": "^5.0.1",
    "@types/styled-system": "^5.1.9",
    "@typescript-eslint/eslint-plugin": "^2.25.0",
    "@typescript-eslint/parser": "^2.25.0",
    "babel-jest": "^24.9.0",
    "babel-plugin-styled-components": "^1.10.7",
    "concurrently": "^5.1.0",
    "eslint": "^6.5.1",
    "eslint-import-resolver-typescript": "^2.0.0",
    "eslint-plugin-import": "^2.20.2",
    "husky": "^4.2.3",
    "jest": "^24.9.0",
    "lint-staged": "^10.1.2",
    "metro-react-native-babel-preset": "^0.58.0",
    "prettier": "^2.0.2",
    "react-native-testing-library": "^1.13.0",
    "react-test-renderer": "^16.13.1",
    "typescript": "^3.8.3"
  }

Any ideas what's going on?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions