Error for every fireEvent changing state in a component with a Switch
See original GitHub issueVersions
"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
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:
- Created 3 years ago
- Reactions:8
- Comments:32 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FYI: mocking with the following works (https://jestjs.io/docs/en/tutorial-react-native#mock-native-modules-using-jestmock)
Edit:
As mentioned by @kptp below, since 0.66 you need to do the following instead:
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: