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.

Test react-native switch

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
NicholasIoanJonescommented, Jul 2, 2021

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:

        const switchComponent = component.getByTestId(`form-switch-${switchProps.basicProps.itemDef.id}`);
        fireEvent(switchComponent, 'valueChange', true);

3reactions
thymikeecommented, Aug 24, 2020

There’s no “onPress” on Switch. There’s “onValueChange” so please use that as a value for fireEvent() fn

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 >
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 >

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