-
Notifications
You must be signed in to change notification settings - Fork 271
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
Error for every fireEvent changing state in a component with a Switch #329
Comments
Can you create a repro we can download? |
Updated with repro link. Just a |
Thanks! I think it's something specific to React Native then, and not to tests, but definitely worth to check and understand what's going on. |
Maybe, but the error is logged when I run the test. I don't have any error/warning firing the same events on an emulator 🤔 |
Remember you're running your test in different environment (Node) than your emulator (JSC/Hermes). There may be slight differences, not sure if that's the case though. Let's wait for somebody to triage and investigate this. cc @mdjastrzebski @cross19xx |
@Pardiez I was able to reproduce your issue using your code. Everything is a you diagnosed it, removing So I've made a little experiment and swapped This makes me believe that warning is caused by re-render of I've also found relevant commit in @thymikee maybe you can see using the linked PR if we can and/or should somehow mitigate it in RNTL. |
@mdjastrzebski is the link correct? It points to changes in |
@Pardiez I got the same error:
Effectively it seems to be link with the Switch component. When I removed it, there is no more error. To pass through this warning, I mock the Switch component.
In jest I can fire the event with:
|
I'm facing the same issue with the // test.js
...
const switchBtn = await findByA11yRole('switch');
fireEvent(switchBtn, 'onValueChange');
... |
This comment has been minimized.
This comment has been minimized.
@TheSavior ideas about why this warning triggers for a native component in test environment? I've also tried this with 0.63, and latest nightly, the warning stays the same (minus component stack). Hope you have some additional context so we don't have to dig too far :) |
Probably something about the mock that is being used for Switch not forwardRef'ing to the underlying component created with That warning fires when dispatchCommand is called with a ref to a component that was created in JS like a JS function or class, vs a ref to a component created like this: |
This comment has been minimized.
This comment has been minimized.
I was able to solve this by using |
@hsavit1 would you mind clarifying how you're firing the event? I want make sure I was trying what you were suggesting. I am trying like this,
But still getting the warning.
I wonder if it is an issue we are on an older version? |
It's not related to this library, rather the |
Thanks for the info. We can ignore for now for sure. It's a ~20-line bright red warning though so it is fairly eye-catching every time running tests. |
I tried this and it works like a charm, thank you @Merlier however, instead of creating "jest": {
"setupFiles": [
"<rootDir>/jest-setup.js"
]
} here is the glimpse of my const React = require('react')
const ReactNative = require('react-native')
const Switch = function(props) {
const [value, setValue] = React.useState(props.value)
return (
<ReactNative.TouchableOpacity
onPress={() => {
props.onValueChange(!value)
setValue(!value)
}}
testID={props.testID}>
<ReactNative.Text>{value.toString()}</ReactNative.Text>
</ReactNative.TouchableOpacity>
)
}
Object.defineProperty(ReactNative, 'Switch', {
get: function() {
return Switch
}
}) |
@thymikee what kind of action do you need for this? I am happy to offer my help |
Based on #329 (comment) I'd say it's an upstream issue. You can investigate the source of the problem in the RN mocks, if it's not resolved on master branch already. |
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')
}
}) |
@Andarius it works like a charm! |
@Andarius thank you! This worked amazing, and is really simple and follows the pattern for mocking other modules. |
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:
|
We have tests that use const Switch = function (props) {
const [value, setValue] = React.useState(props.value)
return (
<ReactNative.TouchableOpacity
onPress={() => {
props.onValueChange(!value)
setValue(!value)
}}
testID={props.testID}
accessibilityRole="switch"
accessibilityLabel={props.accessibilityLabel}
>
<ReactNative.Text>{value.toString()}</ReactNative.Text>
</ReactNative.TouchableOpacity>
)
}
Object.defineProperty(ReactNative, 'Switch', {
get: function () {
return Switch
},
}) This did prevent us from asserting on the |
No solution in this thread worked for me. Is there an update on this? Do I need to update React Native version or React-Test-Renderer? |
Have you tried this? |
Yes. I resolved it now. I had to update |
I resolved this issue by upgrading react-native from 0.66.4 to 0.68.2. So it seems it was fixed upstream. |
Closing as resolved upstream. @Pardiez if you find that the problem is still present in the current version of RNTL (v11) in recommended setup as in basic example app, please create a new GH issue for it. If so, please also provide a repro repository. |
Only a default jest.mock('react-native/Libraries/Components/Switch/Switch', () => ({
default: jest.fn(),
})); |
Versions
Description
I'm getting a console error for every fire event that changes the component state in a component with a Switch.
Reproducible Demo
https://github.com/Pardiez/rn-testing-library-329
Without
setState
on the fired event or without theSwitch
, there is no error.This error occurs only with React Native 0.62+
The text was updated successfully, but these errors were encountered: