From fb58494283102a67bcecda3265f70f010764a126 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Thu, 27 Jun 2024 07:26:04 -0700 Subject: [PATCH] RN: Fix `mockComponent` for Functional Components (#45196) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45196 Currently, `mockComponent` makes a false assumption that if a component is a function, it extends `React.Component`. There's a bunch of problems with this mocking setup with requiring mock components that extend `React.Component`, but this change does not attemp to solve that. This change unblocks future refactors to make native components export functional components (that are neither class component nor `forwardRef` results). Changelog: [General][Changed] - Fixed native component mocking in Jest unit tests to support functional components Reviewed By: javache Differential Revision: D59097730 fbshipit-source-id: ca2784ac3baa9ab4ab6a503c5fd6437c60179352 --- packages/react-native/jest/mockComponent.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react-native/jest/mockComponent.js b/packages/react-native/jest/mockComponent.js index 2fa82c15eab..db79932447d 100644 --- a/packages/react-native/jest/mockComponent.js +++ b/packages/react-native/jest/mockComponent.js @@ -16,7 +16,10 @@ module.exports = (moduleName, instanceMethods, isESModule) => { const React = require('react'); const SuperClass = - typeof RealComponent === 'function' ? RealComponent : React.Component; + typeof RealComponent === 'function' && + RealComponent.prototype.constructor instanceof React.Component + ? RealComponent + : React.Component; const name = RealComponent.displayName ||