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.

No handler function found with fireEvent.press on disabled TouchableOpacity

See original GitHub issue

Describe the bug

When running fireEvent.press on a disabled TouchableOpacity, the following error appears: No handler function found for event: "press"

Expected behavior

Should not raise an error

Steps to Reproduce

Run the following

import { TouchableOpacity } from 'react-native'

describe.only('Test TouchableOpacity', () => {
    const Button = ({ onPress }) => {
        return <TouchableOpacity testID='testBtn' disabled={true} onPress={onPress}></TouchableOpacity>
    }

    const onPressSpy = sinon.spy()

    it('onPress does not trigger', () => {
        const { getByTestId } = render( <Button onPress={onPressSpy}/>)
        fireEvent.press(getByTestId('testBtn'))
        expect(props.onPress.callCount).toBe(0)
    })
})

Versions

  npmPackages:
    @testing-library/react-native: ^7.0.0 => 7.0.0 
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.0 => 0.63.0 
    react-test-renderer: ^16.13.0 => 16.13.1 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
mdjastrzebskicommented, Jul 30, 2020

@Andarius I think your original complain is a valid point. So fireEvent throws error when it does not find any matching event handler, which should suggested to the programmer that there is a problem with assertions or components under test.

However the case when there is event handler but is disabled should probably be handled without throwing error.

2reactions
thymikeecommented, Jul 31, 2020

We’ve just released 7.0.1 which now doesn’t throw. So please adjust your tests to something like this:

fireEvent.press(getByTestId('testBtn'));
expect(handlePress).not.toHaveBeenCalled();
Read more comments on GitHub >

github_iconTop Results From Across the Web

react native - No handler function found for event: "changeText"
The error occurs because there's no onChangeText callback in your TextInput so there's no handler to call when triggering fireEvent.
Read more >
react testing library disabled button - You.com | The Search Engine ...
When running fireEvent.press on a disabled TouchableOpacity, the following error appears: No handler function found for event: "press" ...
Read more >
API | React Native Testing Library - Open Source
Useful function to help testing components that use hooks API. By default any render , update , fireEvent , and waitFor calls are...
Read more >
How to use the react-testing-library.fireEvent.change function ...
To help you get started, we've selected a few react-testing-library.fireEvent.change examples, based on popular ways it is used in public projects.
Read more >
React Native touchable vs. pressable components
Later, the framework introduced the Pressable API for handling ... Adjust the pressed opacity level with the activeOpacity prop; Disable the ...
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