"Cannot read property 'target' of undefined" with fireEvent.press()
See original GitHub issueAsk your question
During a test that I’ve made over one of my component, I wanted to ‘click’ on a Button . This button worked perfectly out of the test. But wen the button is trigger by fireEvent.press()
it seems to not recognise the event and so the target of it.
I’m a beginner in testing and moreover in react-native tests so it’s quite possible that this issue is due to me. But if someone got an idea it would be very appreciable 👍
The Button
<Button
testID="mainButton"
title="Découvrir les offres"
style={defaultStyles.p3_actions}
color="#000A24"
onPress={(e) => {
Linking.openURL(data.config.subscription_url);
onSubscribeClick(
widget,
e.target,
data.config.subscription_url
);
}}
/>
The Test
it('should render without issues', () => {
const { getByTestId } = render(
<PaywallContext onSubscribeClick={(a, b, c) => {}} >
<SubscriptionWidget { ... widgetProps }/>
</PaywallContext>
);
const element = getByTestId('widgetView');
const mainButton = getByTestId('mainButton');
fireEvent.press(mainButton);
expect(element).toBeDefined();
});
The Issue
<SubscriptionWidget /> › should render without issues
TypeError: Cannot read property 'target' of undefined
37 | onSubscribeClick(
38 | widget,
> 39 | e.target,
| ^
40 | data.config.subscription_url
41 | );
42 | }}
at onPress (src/paywall/components/SubscriptionWidget.js:39:15)
at node_modules/react-native-testing-library/build/fireEvent.js:40:26
at batchedUpdates (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12463:12)
at act (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15367:14)
at invokeEvent (node_modules/react-native-testing-library/build/fireEvent.js:39:3)
at Function.press (node_modules/react-native-testing-library/build/fireEvent.js:47:33)
at Object.<anonymous> (tests/widgets/subscriptionWidget.test.js:38:15)
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Cannot read property 'target' of undefined when testing useState
Here, you seem to test that setBio gets correctly called when handleBio is called, which is just testing that React binds props correctly...
Read more >Cannot read property 'fireEvent' of undefined... - ServiceNow
So, I have a UI Script that I'm pulling onto a form. When I pull in this UI Script, I can't do anything...
Read more >API | React Native Testing Library - Open Source
Follow this great guide on how to set this up. The render method returns a RenderResult object having properties described below.
Read more >React Testing Library and the “not wrapped in act” Errors
Case 1: Asynchronous Updates. Test code somehow triggered a testing component to update in an asynchronous way. Here is an example: const MyComponent...
Read more >jest cannot read properties of null (reading 'usestate') - You.com
TypeError: Cannot read properties of undefined (reading 'current'). Here is my test file code : import React from "react"; import renderer from ...
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’ve done a sample test how does
onPress
work.In running RN app the
onPress
handler receivesevent
withevent.target
being a number.In RNTL test
onPress
handler receivesundefined
event. Which is technically correct according to flow types for RN Button component.@defless How do you use this value
event.target
? If for identifying button, then maybe you could use closure to capture some button identifier in event handler.Just to clarify by what I mean by passing button identifier in closure: