Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Feb 17, 2022
1 parent ecd6803 commit 8242568
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 131 deletions.
4 changes: 4 additions & 0 deletions test/specs/addons/Confirm/Confirm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ describe('Confirm', () => {
common.implementsShorthandProp(Confirm, {
autoGenerateKey: false,
propKey: 'header',
rendersPortal: true,
ShorthandComponent: Modal.Header,
mapValueToProps: (content) => ({ content }),
requiredProps: { open: true },
})
common.implementsShorthandProp(Confirm, {
autoGenerateKey: false,
propKey: 'content',
rendersPortal: true,
ShorthandComponent: Modal.Content,
mapValueToProps: (content) => ({ content }),
requiredProps: { open: true },
})

describe('children', () => {
Expand Down
25 changes: 14 additions & 11 deletions test/specs/commonTests/classNameHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import React, { createElement } from 'react'
import React from 'react'

import { consoleUtil } from 'test/utils'

Expand All @@ -8,24 +8,24 @@ export const classNamePropValueBeforePropName = (Component, propKey, propValues,

propValues.forEach((propVal) => {
it(`adds "${propVal} ${className}" to className`, () => {
shallow(createElement(Component, { ...requiredProps, [propKey]: propVal }), {
autoNesting: true,
}).should.have.className(`${propVal} ${className}`)
const wrapper = mount(
React.createElement(Component, { ...requiredProps, [propKey]: propVal }),
)
const elementClassName = wrapper.childAt(0).getDOMNode().className

expect(elementClassName).include(`${propVal} ${className}`)
})
})
}

export const noClassNameFromBoolProps = (Component, propKey, propValues, options = {}) => {
const { className = propKey, nestingLevel = 0, requiredProps = {} } = options
const { className = propKey, requiredProps = {} } = options

_.each([true, false], (bool) =>
it(`does not add any className when ${bool}`, () => {
consoleUtil.disableOnce()

const wrapper = shallow(createElement(Component, { ...requiredProps, [propKey]: bool }), {
autoNesting: true,
nestingLevel,
})
const wrapper = mount(React.createElement(Component, { ...requiredProps, [propKey]: bool }))

wrapper.should.not.have.className(className)
wrapper.should.not.have.className('true')
Expand All @@ -38,15 +38,18 @@ export const noClassNameFromBoolProps = (Component, propKey, propValues, options

export const noDefaultClassNameFromProp = (Component, propKey, propValues, options = {}) => {
const { defaultProps = {} } = Component
const { className = propKey, nestingLevel = 0, requiredProps = {} } = options
const { className = propKey, requiredProps = {} } = options

// required props may include a prop that creates a className
// if so, we cannot assert that it doesn't exist by default because it is required to exist
// skip assertions for required props
if (propKey in defaultProps) return
if (propKey in requiredProps) return

it('is not included in className when not defined', () => {
const wrapper = shallow(<Component {...requiredProps} />, { autoNesting: true, nestingLevel })
consoleUtil.disableOnce()
const wrapper = mount(<Component {...requiredProps} />)

wrapper.should.not.have.className(className)

// ensure that none of the prop option values are in className
Expand Down
17 changes: 7 additions & 10 deletions test/specs/commonTests/implementsClassNameProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ export const propKeyOnlyToClassName = (Component, propKey, options = {}) => {

const element = React.createElement(Component, { ...requiredProps, [propKey]: true })
const wrapper = mount(element)
const elementClassName = wrapper.childAt(0).getDOMNode().className

// ".should.have.className" with "mount" renderer does not handle properly cases when "className" contains
// multiple classes. That's why ".split()" is required.
className.split(' ').forEach((classNamePart) => {
wrapper.childAt(0).should.have.className(classNamePart)
})
// multiple classes.
expect(elementClassName).include(className)
})

it('does not add prop value to className', () => {
Expand Down Expand Up @@ -100,15 +99,13 @@ export const propKeyOrValueAndKeyToClassName = (Component, propKey, propValues,
})

it('adds only the name to className when true', () => {
shallow(React.createElement(Component, { ...requiredProps, [propKey]: true }), {
autoNesting: true,
}).should.have.className(className)
const wrapper = mount(React.createElement(Component, { ...requiredProps, [propKey]: true }))

wrapper.should.have.className(className)
})

it('adds no className when false', () => {
const wrapper = shallow(
React.createElement(Component, { ...requiredProps, [propKey]: false }),
)
const wrapper = mount(React.createElement(Component, { ...requiredProps, [propKey]: false }))

wrapper.should.not.have.className(className)
wrapper.should.not.have.className('true')
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
40 changes: 26 additions & 14 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 @@ -28,6 +29,7 @@ const shorthandComponentName = (ShorthandComponent) => {
* @param {function} options.mapValueToProps A function that maps a primitive value to the Component props.
* @param {Boolean} [options.parentIsFragment=false] A flag that shows the type of the Component to test.
* @param {Object} [options.requiredProps={}] Props required to render the component.
* @param {boolean} [options.rendersPortal=false] Does this component render a Portal powered component?
* @param {Object} [options.shorthandDefaultProps] Default props for the shorthand component.
* @param {Object} [options.shorthandOverrideProps] Override props for the shorthand component.
*/
Expand All @@ -39,13 +41,18 @@ export default (Component, options = {}) => {
mapValueToProps,
parentIsFragment = 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 @@ -60,39 +67,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) {
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
Loading

0 comments on commit 8242568

Please sign in to comment.