Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test fails with error on function which uses TextInput.clear() #2421

Closed
art-reddy opened this issue Dec 22, 2016 · 3 comments
Closed

Test fails with error on function which uses TextInput.clear() #2421

art-reddy opened this issue Dec 22, 2016 · 3 comments

Comments

@art-reddy
Copy link

In React Native application I have a component with "Clear" button which suppose to clear TextInput. Application works fine and clears the field without any exceptions but test fails with error:

 FAIL  js/components/searchInput/SearchInput.tests.js
  ● SearchInput › should clear input value

    Invariant Violation: TextInput "viewConfig" is not defined.

      at invariant (node_modules/fbjs/lib/invariant.js:38:15)
      at Component.setNativeProps (node_modules/react-native/Libraries/Renderer/src/renderers/native/NativeMethodsMixin.js:145:1)
      at Component.clear (node_modules/react-native/Libraries/Components/TextInput/TextInput.js:576:6)
      at SearchInput.clear (js/components/searchInput/SearchInput.js:17:12)
      at Object.<anonymous> (js/components/searchInput/SearchInput.tests.js:20:24)

Component itself:

export default class SearchInput extends Component {
  constructor(props) {
    super(props);

    this.clear = this.clear.bind(this);
  }

  clear() {
    this.input.clear();
  }

  render() {
    return (
      <View>
        <TextInput
          ref={(input) => { this.input = input; }}
          defaultValue={this.props.value}
        />
        <TouchableOpacity onPress={this.clear}>
          <Text>Clear</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

Test:

describe('SearchInput', () => {
  beforeAll(() => {
    jest.mock('TouchableOpacity', () => 'TouchableOpacity');
  });

  it('should clear input value', () => {
    const component = renderer.create(
      <SearchInput defaultValue="Test" />,
    );

    let tree = component.toJSON();
    expect(tree).toMatchSnapshot();

    // Simulate click on Clear button
    tree.children[1].props.onPress();
    tree = component.toJSON();
    expect(tree).toMatchSnapshot();
  });
});

Is 'react-test-renderer' initializes components differently than react-native or is it related to 'ref' property? Maybe there is a recommended way to test such case without causing an error?

@jwbay
Copy link
Contributor

jwbay commented Dec 22, 2016

See facebook/react#7740

@cpojer
Copy link
Member

cpojer commented Dec 26, 2016

@jwbay thanks for helping out on Jest's issue tracker.

@cpojer cpojer closed this as completed Dec 26, 2016
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants