Skip to content

Commit fdd2bce

Browse files
committed
Add additional tests for Foldable Body
1 parent 0d25d13 commit fdd2bce

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

src/components/Foldable/Body.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ class FoldableBody extends Component {
1111
}
1212
}
1313

14-
componentDidUpdate() {
14+
componentDidUpdate(prevProps) {
1515
if (this.props.transition) {
16-
this.executeTransitions()
16+
if (prevProps.transition !== this.props.transition) {
17+
return this.setInitialStyles()
18+
}
19+
return this.executeTransitions()
20+
}
21+
22+
if (prevProps.transition !== this.props.transition) {
23+
return this.resetStyles()
1724
}
25+
26+
return undefined
1827
}
1928

2029
setStyles = (styles) => {
@@ -36,6 +45,13 @@ class FoldableBody extends Component {
3645
this.setStyles({ height: `${height}px` })
3746
}, 0)
3847

48+
resetStyles = () => {
49+
this.node.style.removeProperty('transition')
50+
this.node.style.removeProperty('height')
51+
this.node.style.removeProperty('display')
52+
this.node.style.removeProperty('overflow')
53+
}
54+
3955
closing = false
4056

4157
transitionOpen = () => {
@@ -70,14 +86,20 @@ class FoldableBody extends Component {
7086
handleTransitionOpenEnd = () => {
7187
setTimeout(() => {
7288
if (this.context.isFoldableOpen) {
73-
this.setStyles({ overflow: 'visible' })
89+
this.setStyles({ overflow: 'visible', height: 'auto' })
7490
}
7591
}, this.props.transitionDuration)
7692
}
7793

7894
transitionClose = () => {
7995
this.closing = true
80-
this.setStyles({ overflow: 'hidden', height: 0 })
96+
const height = getHeight(this.node)
97+
this.setStyles({ height: `${height}px` })
98+
99+
setTimeout(() => {
100+
this.setStyles({ overflow: 'hidden', height: 0 })
101+
}, 0)
102+
81103
setTimeout(() => {
82104
if (!this.context.isFoldableOpen) {
83105
this.setStyles({ display: 'none' })

src/components/Foldable/__tests__/Body.jsx

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ describe('<Foldable.Body />', () => {
166166
})
167167
expect($.html()).toMatch(/style="display: block; overflow: hidden; height: 0px;"/)
168168

169+
jest.runTimersToTime(1)
170+
expect($.html()).toMatch(/style="display: block; overflow: hidden; height: 100px;"/)
171+
169172
jest.runAllTimers()
170-
expect($.html()).toMatch(/style="display: block; overflow: visible; height: 100px;"/)
173+
expect($.html()).toMatch(/style="display: block; overflow: visible; height: auto;"/)
171174
})
172175

173176
it('can transition to closed from initial open position', () => {
@@ -194,13 +197,16 @@ describe('<Foldable.Body />', () => {
194197
...defaultContext,
195198
isFoldableOpen: false
196199
})
200+
expect($.html()).toMatch(/style="height: 100px;"/)
201+
202+
jest.runTimersToTime(1)
197203
expect($.html()).toMatch(/style="height: 0px; overflow: hidden;"/)
198204

199205
jest.runAllTimers()
200206
expect($.html()).toMatch(/style="height: 0px; overflow: hidden; display: none;"/)
201207
})
202208

203-
it('can take a set transiion duration and handles opening before close duration has ended', () => {
209+
it('can take a set transition duration and handles opening before close duration has ended', () => {
204210
jest.useFakeTimers()
205211
getHeight.mockImplementation(() => '100')
206212

@@ -224,6 +230,9 @@ describe('<Foldable.Body />', () => {
224230
...defaultContext,
225231
isFoldableOpen: false
226232
})
233+
expect($.html()).toMatch(/style="height: 100px;"/)
234+
235+
jest.runTimersToTime(1)
227236
expect($.html()).toMatch(/style="height: 0px; overflow: hidden;"/)
228237

229238
jest.runTimersToTime(500)
@@ -233,6 +242,62 @@ describe('<Foldable.Body />', () => {
233242
})
234243

235244
expect($.html()).toMatch(/style="height: 100px; overflow: hidden; display: block;"/)
245+
246+
jest.runAllTimers()
247+
expect($.html()).toMatch(/style="height: auto; overflow: visible; display: block;"/)
248+
})
249+
250+
it('removes styles related to transition if transition prop is removed', () => {
251+
jest.useFakeTimers()
252+
getHeight.mockImplementation(() => '100')
253+
254+
const context = {
255+
...defaultContext,
256+
isFoldableOpen: true
257+
}
258+
259+
const $ = mount(
260+
<Foldable.Body transition>
261+
_
262+
</Foldable.Body>
263+
, { context }
264+
)
265+
266+
expect($.html()).toMatch(/style="height: auto;"/)
267+
268+
jest.runAllTimers()
269+
expect($.html()).toMatch(/style="height: 100px;"/)
270+
271+
$.setProps({
272+
transition: false
273+
})
274+
275+
expect($.html()).toMatch(/style=""/)
276+
})
277+
278+
it('sets initial transition styles if transition prop is added', () => {
279+
jest.useFakeTimers()
280+
getHeight.mockImplementation(() => '100')
281+
282+
const context = {
283+
...defaultContext,
284+
isFoldableOpen: true
285+
}
286+
287+
const $ = mount(
288+
<Foldable.Body>
289+
_
290+
</Foldable.Body>
291+
, { context }
292+
)
293+
294+
expect($.html()).not.toMatch(/style="height/)
295+
296+
$.setProps({
297+
transition: true
298+
})
299+
300+
expect($.html()).toMatch(/style="height: auto;/)
236301
})
237302
})
238303
})

0 commit comments

Comments
 (0)