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.

"Cannot read property 'target' of undefined" with fireEvent.press()

See original GitHub issue

Ask 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjastrzebskicommented, Jun 10, 2020

I’ve done a sample test how does onPress work.

In running RN app the onPress handler receives event with event.target being a number.

In RNTL test onPress handler receives undefined 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.

0reactions
mdjastrzebskicommented, Jun 12, 2020

Just to clarify by what I mean by passing button identifier in closure:

onPress={() => { // <-- here: could remove unused 'e' parameter
          Linking.openURL(data.config.subscription_url);
          onSubscribeClick(
            widget,
           "button 1", // <-- here: used string identifier instead of 'e.target':
            data.config.subscription_url
          );
        }}
Read more comments on GitHub >

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

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