Test react-native switch
See original GitHub issueI’m using the switch component from react-native and I’m trying to test the toggle change. I’m using fireEvent.press
but is not working. It returns No handler function found for event: "press"
Could anyone help me?
Versions:
@testing-library/react-native: 7.0.1
react: 16.11.0
react-native: 0.62.2
react-test-renderer: 16.11.0
This is my code:
<Switch
testID="switch"
onValueChange={onToggleSwitch}
value={value}
thumbColor={value ? AppColors.Orange : AppColors.Gray2}
/>
describe('when the switch is pressed', () => {
beforeEach(() => {
const { getByTestId } = getComponent();
const switchComponent = getByTestId('switch');
fireEvent.press(switchComponent);
});
it('should fire `props.onToggleSwitch`', () => {
expect(props.onToggleSwitch).toHaveBeenCalledTimes(1);
});
});
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 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 >Switch
Switch. Renders a boolean input. This is a controlled component that requires an onValueChange callback that updates the value prop in order ...
Read more >Using Jest and Testing Library with React Native Part VII
In the last part of our series on testing React Native with Jest and Testing Library, we'll cover a few handy, random features...
Read more >React Native Tutorial #32 (2021) - Testing with Jest ... - YouTube
In this session, we will get acquainted with the testing in React Native using Jest and we will do this in the form...
Read more >Testing React Native Apps
To understand UI testing for a React Native app, we have developed a ... Tests toggle behavior */ it('should show switch and toggle...
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
I solved this with fresh eyes this morning and wanted to share in case it helps somebody else.
The main problem was my mistake - I was getting the wrong component from the render
For clarity on using the generic form, my working code looks like this:
There’s no “onPress” on Switch. There’s “onValueChange” so please use that as a value for
fireEvent()
fn