Skip to content

Commit 380bec2

Browse files
committed
chore: refactor tests for shorthands to use mount() (#4342)
* chore: refactor tests for shorthands to use mount() * add comment * fix tests with unmount
1 parent 1ca76c3 commit 380bec2

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

src/elements/Flag/Flag.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,13 @@ Flag.propTypes = {
530530
name: customPropTypes.suggest(names),
531531
}
532532

533-
Flag.defaultProps = {
534-
as: 'i',
535-
}
536-
537533
// Heads up!
538534
// .create() factories should be defined on exported component to be visible as static properties
539535
const MemoFlag = React.memo(Flag)
540536

541537
MemoFlag.create = createShorthandFactory(MemoFlag, (value) => ({ name: value }))
538+
MemoFlag.defaultProps = {
539+
as: 'i',
540+
}
542541

543542
export default MemoFlag

src/elements/Icon/Icon.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ Icon.propTypes = {
148148
'aria-label': PropTypes.string,
149149
}
150150

151-
Icon.defaultProps = {
152-
as: 'i',
153-
}
154-
155151
// Heads up!
156152
// .create() factories should be defined on exported component to be visible as static properties
157153
const MemoIcon = React.memo(Icon)
158154

159155
MemoIcon.Group = IconGroup
160156
MemoIcon.create = createShorthandFactory(MemoIcon, (value) => ({ name: value }))
161157

158+
MemoIcon.defaultProps = {
159+
as: 'i',
160+
}
161+
162162
export default MemoIcon

test/specs/addons/Confirm/Confirm-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ describe('Confirm', () => {
3434
ShorthandComponent: Modal.Header,
3535
rendersPortal: true,
3636
mapValueToProps: (content) => ({ content }),
37+
requiredProps: { open: true },
3738
})
3839
common.implementsShorthandProp(Confirm, {
3940
autoGenerateKey: false,
4041
propKey: 'content',
4142
ShorthandComponent: Modal.Content,
4243
rendersPortal: true,
4344
mapValueToProps: (content) => ({ content }),
45+
requiredProps: { open: true },
4446
})
4547

4648
describe('children', () => {

test/specs/commonTests/implementsCommonProps.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export const implementsHTMLLabelProp = (Component, options = {}) => {
110110
*/
111111
export const implementsIconProp = (Component, options = {}) => {
112112
implementsShorthandProp(Component, {
113+
assertExactMatch: false,
113114
propKey: 'icon',
114115
ShorthandComponent: Icon,
115116
mapValueToProps: (val) => ({ name: val }),

test/specs/commonTests/implementsShorthandProp.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash'
2-
import React, { createElement } from 'react'
2+
import React from 'react'
3+
import ReactIs from 'react-is'
34

45
import { createShorthand } from 'src/lib'
56
import { consoleUtil, getComponentName } from 'test/utils'
@@ -41,15 +42,28 @@ export default (Component, options = {}) => {
4142
parentIsFragment = false,
4243
rendersPortal = false,
4344
propKey,
44-
ShorthandComponent,
4545
shorthandDefaultProps = {},
4646
shorthandOverrideProps = {},
4747
requiredProps = {},
4848
} = options
4949
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
5159

5260
describe(`${propKey} shorthand prop (common)`, () => {
61+
let wrapper
62+
63+
afterEach(() => {
64+
if (wrapper && wrapper.unmount) wrapper.unmount()
65+
})
66+
5367
assertRequired(Component, 'a `Component`')
5468
assertRequired(_.isPlainObject(options), 'an `options` object')
5569
assertRequired(propKey, 'a `propKey`')
@@ -62,39 +76,44 @@ export default (Component, options = {}) => {
6276
overrideProps: shorthandOverrideProps,
6377
autoGenerateKey,
6478
})
65-
const element = createElement(Component, { ...requiredProps, [propKey]: value })
66-
const wrapper = shallow(element)
79+
wrapper = mount(React.createElement(Component, { ...requiredProps, [propKey]: value }))
6780

68-
wrapper.should[assertMethod](expectedShorthandElement)
81+
const result = wrapper.find(ShorthandComponent)
82+
83+
expect(result[assertMethod](expectedShorthandElement)).to.equal(true)
6984

7085
// Enzyme's .key() method is not consistent with React for elements with
7186
// no key (`undefined` vs `null`), so use the underlying element instead
7287
// Will fail if more than one element of this type is found
7388
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")
7690
}
7791
}
7892

7993
if (alwaysPresent || (Component.defaultProps && Component.defaultProps[propKey])) {
8094
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)
8298
})
8399
} else {
84100
if (!parentIsFragment && !rendersPortal) {
85101
noDefaultClassNameFromProp(Component, propKey, [], options)
86102
}
87103

88104
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)
90108
})
91109
}
92110

93111
if (!alwaysPresent) {
94112
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)
98117
})
99118
}
100119

test/specs/modules/Dropdown/DropdownItem-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('DropdownItem', () => {
2222
common.implementsImageProp(DropdownItem, { autoGenerateKey: false })
2323

2424
common.implementsShorthandProp(DropdownItem, {
25+
assertExactMatch: false,
2526
autoGenerateKey: false,
2627
propKey: 'flag',
2728
ShorthandComponent: Flag,

0 commit comments

Comments
 (0)