Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor tests for shorthands to use mount() #4342

Merged
merged 3 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/elements/Flag/Flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,13 @@ Flag.propTypes = {
name: customPropTypes.suggest(names),
}

Flag.defaultProps = {
as: 'i',
}

// Heads up!
// .create() factories should be defined on exported component to be visible as static properties
const MemoFlag = React.memo(Flag)

MemoFlag.create = createShorthandFactory(MemoFlag, (value) => ({ name: value }))
MemoFlag.defaultProps = {
as: 'i',
}
Comment on lines +538 to +540
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really strange change that is required for "how Enzyme works". It does not affect functionality, so it's okay.


export default MemoFlag
8 changes: 4 additions & 4 deletions src/elements/Icon/Icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ Icon.propTypes = {
'aria-label': PropTypes.string,
}

Icon.defaultProps = {
as: 'i',
}

// Heads up!
// .create() factories should be defined on exported component to be visible as static properties
const MemoIcon = React.memo(Icon)

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

MemoIcon.defaultProps = {
as: 'i',
}

export default MemoIcon
2 changes: 2 additions & 0 deletions test/specs/addons/Confirm/Confirm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ describe('Confirm', () => {
ShorthandComponent: Modal.Header,
rendersPortal: true,
mapValueToProps: (content) => ({ content }),
requiredProps: { open: true },
})
common.implementsShorthandProp(Confirm, {
autoGenerateKey: false,
propKey: 'content',
ShorthandComponent: Modal.Content,
rendersPortal: true,
mapValueToProps: (content) => ({ content }),
requiredProps: { open: true },
})

describe('children', () => {
Expand Down
1 change: 1 addition & 0 deletions test/specs/commonTests/implementsCommonProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const implementsHTMLLabelProp = (Component, options = {}) => {
*/
export const implementsIconProp = (Component, options = {}) => {
implementsShorthandProp(Component, {
assertExactMatch: false,
propKey: 'icon',
ShorthandComponent: Icon,
mapValueToProps: (val) => ({ name: val }),
Expand Down
37 changes: 24 additions & 13 deletions test/specs/commonTests/implementsShorthandProp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import React, { createElement } from 'react'
import React from 'react'
import ReactIs from 'react-is'

import { createShorthand } from 'src/lib'
import { consoleUtil, getComponentName } from 'test/utils'
Expand Down Expand Up @@ -41,13 +42,18 @@ export default (Component, options = {}) => {
parentIsFragment = false,
rendersPortal = false,
propKey,
ShorthandComponent,
shorthandDefaultProps = {},
shorthandOverrideProps = {},
rendersPortal = false,
requiredProps = {},
} = options
const { assertRequired } = helpers('implementsShorthandProp', Component)
const assertMethod = assertExactMatch ? 'contain' : 'containMatchingElement'

const assertMethod = assertExactMatch ? 'equals' : 'matchesElement'
const ShorthandComponent =
options.ShorthandComponent.$$typeof === ReactIs.Memo
? options.ShorthandComponent.type
: options.ShorthandComponent

describe(`${propKey} shorthand prop (common)`, () => {
assertRequired(Component, 'a `Component`')
Expand All @@ -62,39 +68,44 @@ export default (Component, options = {}) => {
overrideProps: shorthandOverrideProps,
autoGenerateKey,
})
const element = createElement(Component, { ...requiredProps, [propKey]: value })
const wrapper = shallow(element)
const wrapper = mount(React.createElement(Component, { ...requiredProps, [propKey]: value }))

const result = wrapper.find(ShorthandComponent)

wrapper.should[assertMethod](expectedShorthandElement)
expect(result[assertMethod](expectedShorthandElement)).to.equal(true)

// Enzyme's .key() method is not consistent with React for elements with
// no key (`undefined` vs `null`), so use the underlying element instead
// Will fail if more than one element of this type is found
if (autoGenerateKey) {
const shorthandElement = wrapper.find(ShorthandComponent).getElement()
expect(shorthandElement.key).to.equal(expectedShorthandElement.key, "key doesn't match")
expect(result.getElement().key).to.equal(expectedShorthandElement.key, "key doesn't match")
}
}

if (alwaysPresent || (Component.defaultProps && Component.defaultProps[propKey])) {
it(`has default ${name} when not defined`, () => {
shallow(<Component {...requiredProps} />).should.have.descendants(ShorthandComponent)
const wrapper = mount(React.createElement(Component, requiredProps))

wrapper.should.have.descendants(ShorthandComponent)
})
} else {
if (!parentIsFragment && !rendersPortal) {
noDefaultClassNameFromProp(Component, propKey, [], options)
}

it(`has no ${name} when not defined`, () => {
shallow(<Component {...requiredProps} />).should.not.have.descendants(ShorthandComponent)
const wrapper = mount(React.createElement(Component, requiredProps))

wrapper.should.not.have.descendants(ShorthandComponent)
})
}

if (!alwaysPresent) {
it(`has no ${name} when null`, () => {
shallow(
createElement(Component, { ...requiredProps, [propKey]: null }),
).should.not.have.descendants(ShorthandComponent)
const element = React.createElement(Component, { ...requiredProps, [propKey]: null })
const wrapper = mount(element)

wrapper.should.not.have.descendants(ShorthandComponent)
})
}

Expand Down
1 change: 1 addition & 0 deletions test/specs/modules/Dropdown/DropdownItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('DropdownItem', () => {
common.implementsImageProp(DropdownItem, { autoGenerateKey: false })

common.implementsShorthandProp(DropdownItem, {
assertExactMatch: false,
autoGenerateKey: false,
propKey: 'flag',
ShorthandComponent: Flag,
Expand Down