Skip to content

Commit 37c33af

Browse files
committed
chore: remove .state() assertions in tests (#4232)
* Ember: remove .state() usage in Enzyme * AccordionAccordion: remove .state() usage in Enzyme * Checkbox: remove .state() usage in Enzyme * TransitionablePortal: remove .state() usage in Enzyme * Transition: remove .state() usage in Enzyme * Dropdown: remove .state() usage in Enzyme
1 parent 49f0056 commit 37c33af

File tree

8 files changed

+283
-308
lines changed

8 files changed

+283
-308
lines changed

src/modules/Embed/Embed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cx from 'clsx'
2+
import _ from 'lodash'
23
import PropTypes from 'prop-types'
34
import React from 'react'
45

@@ -57,10 +58,9 @@ export default class Embed extends Component {
5758
}
5859

5960
handleClick = (e) => {
60-
const { onClick } = this.props
6161
const { active } = this.state
6262

63-
if (onClick) onClick(e, { ...this.props, active: true })
63+
_.invoke(this.props, 'onClick', e, { ...this.props, active: true })
6464
if (!active) this.setState({ active: true })
6565
}
6666

src/modules/Transition/Transition.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ export default class Transition extends Component {
8787
const durationType = TRANSITION_CALLBACK_TYPE[nextStatus]
8888
const durationValue = normalizeTransitionDuration(duration, durationType)
8989

90-
this.timeoutId = setTimeout(() => this.setState({ status: nextStatus }), durationValue)
90+
if (durationValue === 0) {
91+
this.setState({ status: nextStatus })
92+
} else {
93+
this.timeoutId = setTimeout(() => this.setState({ status: nextStatus }), durationValue)
94+
}
9195
}
9296

9397
updateStatus = (prevState) => {
@@ -161,7 +165,7 @@ export default class Transition extends Component {
161165
debug('render(): state', this.state)
162166

163167
const { children } = this.props
164-
const { status } = this.state
168+
const { nextStatus, status } = this.state
165169

166170
if (status === TRANSITION_STATUS_UNMOUNTED) {
167171
return null
@@ -170,6 +174,10 @@ export default class Transition extends Component {
170174
return cloneElement(children, {
171175
className: this.computeClasses(),
172176
style: this.computeStyle(),
177+
...(process.env.NODE_ENV !== 'production' && {
178+
'data-test-status': status,
179+
'data-test-next-status': nextStatus,
180+
}),
173181
})
174182
}
175183
}

test/specs/addons/TransitionablePortal/TransitionablePortal-test.js

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,26 @@ import TransitionablePortal from 'src/addons/TransitionablePortal/Transitionable
44
import * as common from 'test/specs/commonTests'
55
import { domEvent, sandbox, assertWithTimeout } from 'test/utils'
66

7-
// ----------------------------------------
8-
// Wrapper
9-
// ----------------------------------------
10-
let wrapper
11-
12-
// we need to unmount the modal after every test to remove it from the document
13-
// wrap the render methods to update a global wrapper that is unmounted after each test
14-
const wrapperMount = (...args) => (wrapper = mount(...args))
15-
const wrapperShallow = (...args) => (wrapper = shallow(...args))
16-
177
const quickTransition = { duration: 0 }
188
const requiredProps = {
19-
children: <div />,
9+
children: <div id='children' />,
2010
}
2111

2212
describe('TransitionablePortal', () => {
23-
beforeEach(() => {
24-
wrapper = undefined
25-
document.body.innerHTML = ''
26-
})
27-
28-
afterEach(() => {
29-
if (wrapper && wrapper.unmount) wrapper.unmount()
30-
})
31-
3213
common.isConformant(TransitionablePortal, { requiredProps })
3314

3415
describe('children', () => {
35-
it('renders a Portal', () => {
36-
wrapperShallow(<TransitionablePortal {...requiredProps} />).should.have.descendants('Portal')
37-
})
38-
3916
it('renders a Transition', () => {
40-
wrapperShallow(<TransitionablePortal {...requiredProps} />).should.have.descendants(
41-
'Transition',
42-
)
43-
})
44-
})
45-
46-
describe('getDerivedStateFromProps', () => {
47-
it('passes `open` prop to `portalOpen` when defined', () => {
48-
wrapperMount(<TransitionablePortal {...requiredProps} />)
17+
const wrapper = mount(<TransitionablePortal {...requiredProps} open />)
4918

50-
wrapper.setProps({ open: true })
51-
wrapper.should.have.state('portalOpen', true)
52-
wrapper.setProps({ open: false })
53-
wrapper.should.have.state('portalOpen', false)
54-
})
55-
56-
it('does not pass `open` prop to `portalOpen` when not defined', () => {
57-
wrapperMount(<TransitionablePortal {...requiredProps} />)
58-
59-
wrapper.setProps({ transition: {} })
60-
wrapper.should.have.not.state('portalOpen')
19+
wrapper.should.have.descendants('.transition')
6120
})
6221
})
6322

6423
describe('onClose', () => {
65-
it('is called with (null, data) when Portal closes', (done) => {
24+
it('is called with (null, data) on a click outside', (done) => {
6625
const onClose = sandbox.spy()
67-
68-
wrapperMount(
26+
const wrapper = mount(
6927
<TransitionablePortal
7028
{...requiredProps}
7129
onClose={onClose}
@@ -80,36 +38,33 @@ describe('TransitionablePortal', () => {
8038
assertWithTimeout(() => {
8139
onClose.should.have.been.calledOnce()
8240
onClose.should.have.been.calledWithMatch(null, { portalOpen: false })
41+
42+
wrapper.unmount()
8343
}, done)
8444
})
8545

86-
it('changes `portalOpen` to false', () => {
87-
wrapperMount(
88-
<TransitionablePortal
89-
{...requiredProps}
90-
transition={quickTransition}
91-
trigger={<button />}
92-
/>,
93-
)
46+
it('hides contents on a click outside', () => {
47+
const wrapper = mount(<TransitionablePortal {...requiredProps} trigger={<button />} />)
9448

9549
wrapper.find('button').simulate('click')
96-
domEvent.click(document.body)
50+
wrapper.should.have.descendants('.in#children')
9751

98-
wrapper.should.have.state('portalOpen', false)
52+
domEvent.click(document.body)
53+
wrapper.update()
54+
wrapper.should.have.descendants('.out#children')
9955
})
10056
})
10157

10258
describe('onHide', () => {
10359
it('is called with (null, data) when exiting transition finished', (done) => {
10460
const onHide = sandbox.spy()
105-
const trigger = <button />
106-
wrapperMount(
61+
const wrapper = mount(
10762
<TransitionablePortal
10863
{...requiredProps}
10964
onHide={onHide}
11065
open
11166
transition={quickTransition}
112-
trigger={trigger}
67+
trigger={<button />}
11368
/>,
11469
)
11570

@@ -121,37 +76,58 @@ describe('TransitionablePortal', () => {
12176
portalOpen: false,
12277
transitionVisible: false,
12378
})
79+
80+
wrapper.unmount()
12481
}, done)
12582
})
12683
})
12784

12885
describe('onOpen', () => {
129-
it('is called with (null, data) when Portal opens', () => {
86+
it('is called with (null, data) when opens', () => {
13087
const onOpen = sandbox.spy()
88+
const wrapper = mount(
89+
<TransitionablePortal {...requiredProps} onOpen={onOpen} trigger={<button />} />,
90+
)
13191

132-
wrapperMount(<TransitionablePortal {...requiredProps} onOpen={onOpen} trigger={<button />} />)
13392
wrapper.find('button').simulate('click')
134-
13593
onOpen.should.have.been.calledOnce()
13694
onOpen.should.have.been.calledWithMatch(null, { portalOpen: true })
13795
})
13896

139-
it('changes `portalOpen` to true', () => {
140-
wrapperMount(<TransitionablePortal {...requiredProps} trigger={<button />} />)
97+
it('renders contents', () => {
98+
const wrapper = mount(<TransitionablePortal {...requiredProps} trigger={<button />} />)
14199

142100
wrapper.find('button').simulate('click')
143-
wrapper.should.have.state('portalOpen', true)
101+
wrapper.should.have.descendants('.in#children')
144102
})
145103
})
146104

147105
describe('open', () => {
148-
it('does not block update of state on Portal close', () => {
149-
wrapperMount(<TransitionablePortal {...requiredProps} open />)
150-
wrapper.should.have.state('portalOpen', true)
151-
wrapper.update()
106+
it('does not block update of state on a portal close', () => {
107+
const wrapper = mount(<TransitionablePortal {...requiredProps} open />)
108+
wrapper.should.have.descendants('.in#children')
152109

153110
domEvent.click(document.body)
154-
wrapper.should.have.state('portalOpen', false)
111+
wrapper.update()
112+
wrapper.should.have.descendants('.out#children')
113+
})
114+
115+
it('passes `open` prop to Transition when defined', () => {
116+
const wrapper = mount(<TransitionablePortal {...requiredProps} />)
117+
118+
wrapper.setProps({ open: true })
119+
wrapper.should.have.descendants('.in#children')
120+
121+
wrapper.setProps({ open: false })
122+
wrapper.should.have.descendants('.out#children')
123+
})
124+
125+
it('does not pass `open` prop to Transition when not defined', () => {
126+
const wrapper = mount(<TransitionablePortal {...requiredProps} />)
127+
wrapper.should.have.not.descendants('#children')
128+
129+
wrapper.setProps({ transition: {} })
130+
wrapper.should.have.not.descendants('#children')
155131
})
156132
})
157133
})

0 commit comments

Comments
 (0)