question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error for every fireEvent changing state in a component with a Switch

See original GitHub issue

Versions

"react": "16.11.0",
"react-native": "0.62.0"
"react-native-testing-library": "1.14.0",
"react-test-renderer": "16.11.0"

Description

I’m getting a console error for every fire event that changes the component state in a component with a Switch.

console.error node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js:608
  Warning: dispatchCommand was called with a ref that isn't a native component. Use React.forwardRef to get access to the underlying native component

Screenshot 2020-05-18 at 18 30 48

Reproducible Demo

https://github.com/Pardiez/rn-testing-library-329

// src/Dummy/__ tests __/Dummy.test.js

import React from 'react';
import { render, fireEvent } from 'react-native-testing-library';
import Dummy from '../';

describe('Dummy component', () => {
  test('renders correctly', () => {
    const { queryByTestId } = render(<Dummy />);

    fireEvent.changeText(queryByTestId('text-input'), 'stage');
    fireEvent.press(queryByTestId('touchable-opacity'));
    expect(queryByTestId('text-input')).toBeTruthy(); // dummy expect

  });
});
// src/Dummy/index.js

import React, { Component } from 'react';
import { View, TextInput, Switch, TouchableOpacity } from 'react-native';

class Dummy extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text:'',
    };
  }

  render() {
    return (
      <View>
        <TextInput
          onChangeText={(text) => {this.setState({text})}}
          testID="text-input" />

        <TouchableOpacity
          onPress={() => {this.setState({text: ''})}}
          testID="touchable-opacity" />
          
        <Switch />
      </View>
    );
  }
}

export default Dummy;

Without setState on the fired event or without the Switch, there is no error. This error occurs only with React Native 0.62+

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:32 (10 by maintainers)

github_iconTop GitHub Comments

104reactions
Andariuscommented, Dec 1, 2021

FYI: mocking with the following works (https://jestjs.io/docs/en/tutorial-react-native#mock-native-modules-using-jestmock)

jest.mock('react-native/Libraries/Components/Switch/Switch', () => {
    const mockComponent = require('react-native/jest/mockComponent')
    return mockComponent('react-native/Libraries/Components/Switch/Switch')
})

Edit:

As mentioned by @kptp below, since 0.66 you need to do the following instead:

jest.mock('react-native/Libraries/Components/Switch/Switch', () => {
  const mockComponent = require('react-native/jest/mockComponent')
  return {
    default: mockComponent('react-native/Libraries/Components/Switch/Switch')
  }
})
10reactions
kptpcommented, Nov 26, 2021

This issue popped up for me again after updating react-native to version 0.66.2. The fix was to change the mock to return a default export like so:

jest.mock('react-native/Libraries/Components/Switch/Switch', () => {
  const mockComponent = require('react-native/jest/mockComponent');
  return {
    default: mockComponent('react-native/Libraries/Components/Switch/Switch'),
  };
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed Testing Switch React Native with Testing Library
I solved this problem by changing onChange to onValueChange <Switch value={isActive} onValueChange={handleChangeSwitch} ...
Read more >
React Testing Library and the “not wrapped in act” Errors
This normally happens in components that have loading state, e.g. components fetching data using GraphQL or REST. const MyComponent = () => {...
Read more >
Firing Events - Testing Library
Convenience methods for firing DOM events. Check out src/event-map.js for a full list as well as default eventProperties .
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
Not using @testing-library/user-event​​ In the example above, fireEvent. change will simply trigger a single change event on the input. However ...
Read more >
Rich Events and Testing - WebStorm Guide - JetBrains
Add event handling to a stateful class component by first writing ... on every change, to see if your change works without breaking...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found