1
1
import _ from 'lodash'
2
- import React , { createElement } from 'react'
2
+ import React from 'react'
3
+ import ReactIs from 'react-is'
3
4
4
5
import { createShorthand } from 'src/lib'
5
6
import { consoleUtil , getComponentName } from 'test/utils'
@@ -41,15 +42,28 @@ export default (Component, options = {}) => {
41
42
parentIsFragment = false ,
42
43
rendersPortal = false ,
43
44
propKey,
44
- ShorthandComponent,
45
45
shorthandDefaultProps = { } ,
46
46
shorthandOverrideProps = { } ,
47
47
requiredProps = { } ,
48
48
} = options
49
49
const { assertRequired } = helpers ( 'implementsShorthandProp' , Component )
50
- const assertMethod = assertExactMatch ? 'contain' : 'containMatchingElement'
50
+
51
+ const assertMethod = assertExactMatch ? 'equals' : 'matchesElement'
52
+ // Heads up!
53
+ // Enzyme does handle properly React.memo() in find and always returns inner component
54
+ // That's why we should unwrap it, otherwise "wrapper.find(Component)" is not equal to "Component" 💥
55
+ const ShorthandComponent =
56
+ options . ShorthandComponent . $$typeof === ReactIs . Memo
57
+ ? options . ShorthandComponent . type
58
+ : options . ShorthandComponent
51
59
52
60
describe ( `${ propKey } shorthand prop (common)` , ( ) => {
61
+ let wrapper
62
+
63
+ afterEach ( ( ) => {
64
+ if ( wrapper && wrapper . unmount ) wrapper . unmount ( )
65
+ } )
66
+
53
67
assertRequired ( Component , 'a `Component`' )
54
68
assertRequired ( _ . isPlainObject ( options ) , 'an `options` object' )
55
69
assertRequired ( propKey , 'a `propKey`' )
@@ -62,39 +76,44 @@ export default (Component, options = {}) => {
62
76
overrideProps : shorthandOverrideProps ,
63
77
autoGenerateKey,
64
78
} )
65
- const element = createElement ( Component , { ...requiredProps , [ propKey ] : value } )
66
- const wrapper = shallow ( element )
79
+ wrapper = mount ( React . createElement ( Component , { ...requiredProps , [ propKey ] : value } ) )
67
80
68
- wrapper . should [ assertMethod ] ( expectedShorthandElement )
81
+ const result = wrapper . find ( ShorthandComponent )
82
+
83
+ expect ( result [ assertMethod ] ( expectedShorthandElement ) ) . to . equal ( true )
69
84
70
85
// Enzyme's .key() method is not consistent with React for elements with
71
86
// no key (`undefined` vs `null`), so use the underlying element instead
72
87
// Will fail if more than one element of this type is found
73
88
if ( autoGenerateKey ) {
74
- const shorthandElement = wrapper . find ( ShorthandComponent ) . getElement ( )
75
- expect ( shorthandElement . key ) . to . equal ( expectedShorthandElement . key , "key doesn't match" )
89
+ expect ( result . getElement ( ) . key ) . to . equal ( expectedShorthandElement . key , "key doesn't match" )
76
90
}
77
91
}
78
92
79
93
if ( alwaysPresent || ( Component . defaultProps && Component . defaultProps [ propKey ] ) ) {
80
94
it ( `has default ${ name } when not defined` , ( ) => {
81
- shallow ( < Component { ...requiredProps } /> ) . should . have . descendants ( ShorthandComponent )
95
+ wrapper = mount ( React . createElement ( Component , requiredProps ) )
96
+
97
+ wrapper . should . have . descendants ( ShorthandComponent )
82
98
} )
83
99
} else {
84
100
if ( ! parentIsFragment && ! rendersPortal ) {
85
101
noDefaultClassNameFromProp ( Component , propKey , [ ] , options )
86
102
}
87
103
88
104
it ( `has no ${ name } when not defined` , ( ) => {
89
- shallow ( < Component { ...requiredProps } /> ) . should . not . have . descendants ( ShorthandComponent )
105
+ wrapper = mount ( React . createElement ( Component , requiredProps ) )
106
+
107
+ wrapper . should . not . have . descendants ( ShorthandComponent )
90
108
} )
91
109
}
92
110
93
111
if ( ! alwaysPresent ) {
94
112
it ( `has no ${ name } when null` , ( ) => {
95
- shallow (
96
- createElement ( Component , { ...requiredProps , [ propKey ] : null } ) ,
97
- ) . should . not . have . descendants ( ShorthandComponent )
113
+ const element = React . createElement ( Component , { ...requiredProps , [ propKey ] : null } )
114
+ wrapper = mount ( element )
115
+
116
+ wrapper . should . not . have . descendants ( ShorthandComponent )
98
117
} )
99
118
}
100
119
0 commit comments