Commit dcfd018a authored by Robert Knight's avatar Robert Knight

Change `mockImportedComponents` to not rely on existence of `propTypes`

Detecting a Preact/React functional component that does not use `propTypes` is
impossible to do with 100% accuracy. `function Foo() { return 'Hello' }`
is a perfectly valid component with no obvious Preact/React features.

Fortunately because of the way this helper is used, we can make do with an imperfect
check. If `mockImportedComponents` mocks something which is not really a
component, that mock will be overridden with a real mock later.
parent b17c7333
/** /**
* Return true if `value` "looks like" a React/Preact component. * Return true if an imported `value` "looks like" a Preact function component.
*
* This check can have false positives (ie. match values which are not really components).
* That's OK because typical usage in a test is to first mock all components with
* `$imports.$mock(mockImportedComponents())` and then mock other things with
* `$imports.$mock(...)`. The more specific mocks will override the generic
* component mocks.
*/ */
function isComponent(value) { function isComponent(value) {
return ( return (
typeof value === 'function' && typeof value === 'function' &&
value.hasOwnProperty('propTypes') && value.name.match(/^[A-Z]/) &&
value.name.match(/^[A-Z]/) // Check that function is not an ES class. Note this only works with real
// ES classes and may not work with ones transpiled to ES5.
!value.toString().match(/^class\b/)
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment