Skip to content

Commit 1a4913b

Browse files
authored
chore: refactor Modal tests (#4339)
1 parent abd59d0 commit 1a4913b

File tree

2 files changed

+45
-116
lines changed

2 files changed

+45
-116
lines changed

test/specs/modules/Modal/Modal-test.js

Lines changed: 32 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Portal from 'src/addons/Portal/Portal'
1111

1212
import {
1313
assertNodeContains,
14-
assertBodyClasses,
1514
assertBodyContains,
1615
assertWithTimeout,
1716
domEvent,
@@ -30,16 +29,6 @@ let wrapper
3029
const wrapperMount = (...args) => (wrapper = mount(...args))
3130
const wrapperShallow = (...args) => (wrapper = shallow(...args))
3231

33-
// cleanup in `useEffect()` is executed async, so we need to perform a proper cleanup first to avoid
34-
// collisions with other tests
35-
function waitForClassesCleanup(done, customAssertions = () => {}) {
36-
wrapper.unmount()
37-
assertWithTimeout(() => {
38-
assertBodyClasses('dimmed', false)
39-
customAssertions()
40-
}, done)
41-
}
42-
4332
describe('Modal', () => {
4433
beforeEach(() => {
4534
if (wrapper && wrapper.unmount) {
@@ -72,12 +61,16 @@ describe('Modal', () => {
7261
propKey: 'header',
7362
ShorthandComponent: ModalHeader,
7463
mapValueToProps: (content) => ({ content }),
64+
rendersPortal: true,
65+
requiredProps: { open: true },
7566
})
7667
common.implementsShorthandProp(Modal, {
7768
autoGenerateKey: false,
7869
propKey: 'content',
7970
ShorthandComponent: ModalContent,
8071
mapValueToProps: (content) => ({ content }),
72+
rendersPortal: true,
73+
requiredProps: { open: true },
8174
})
8275

8376
// Heads up!
@@ -246,67 +239,24 @@ describe('Modal', () => {
246239
})
247240

248241
describe('dimmer', () => {
249-
it('adds a "dimmer" className to the body', (done) => {
242+
it('renders ModalDimmer by default', () => {
250243
wrapperMount(<Modal open />)
251-
252-
assertBodyContains('.ui.page.modals.dimmer.transition.visible.active')
253-
waitForClassesCleanup(done)
244+
wrapper.should.have.descendants('ModalDimmer')
254245
})
255246

256-
describe('can be "true"', () => {
257-
it('adds/removes body classes "dimmable dimmed" on mount/unmount', (done) => {
258-
assertBodyClasses('dimmable dimmed', false)
259-
260-
wrapperMount(<Modal open dimmer />)
261-
assertBodyClasses('dimmable dimmed')
262-
263-
wrapper.setProps({ open: false })
264-
assertBodyClasses('dimmable dimmed', false)
265-
266-
waitForClassesCleanup(done)
267-
})
247+
it('renders ModalDimmer when is "true"', () => {
248+
wrapperMount(<Modal open dimmer />)
249+
wrapper.should.have.descendants('ModalDimmer')
268250
})
269251

270-
describe('blurring', () => {
271-
it('adds/removes body classes "dimmable dimmed blurring" on mount/unmount', (done) => {
272-
assertBodyClasses('dimmable dimmed blurring', false)
273-
274-
wrapperMount(<Modal open dimmer='blurring' />)
275-
assertBodyClasses('dimmable dimmed blurring')
276-
277-
wrapper.setProps({ open: false })
278-
assertBodyClasses('dimmable dimmed blurring', false)
279-
280-
waitForClassesCleanup(done)
281-
})
282-
283-
it('adds a dimmer to the body', (done) => {
284-
wrapperMount(<Modal open dimmer='blurring' />)
285-
286-
assertBodyContains('.ui.page.modals.dimmer.transition.visible.active')
287-
waitForClassesCleanup(done)
288-
})
252+
it('passes "blurring" to ModalDimmer', () => {
253+
wrapperMount(<Modal open dimmer='blurring' />)
254+
wrapper.find('ModalDimmer').should.have.prop('blurring', true)
289255
})
290256

291-
describe('inverted', () => {
292-
it('adds/removes body classes "dimmable dimmed" on mount/unmount', (done) => {
293-
assertBodyClasses('dimmable dimmed', false)
294-
295-
wrapperMount(<Modal open dimmer />)
296-
assertBodyClasses('dimmable dimmed')
297-
298-
wrapper.setProps({ open: false })
299-
assertBodyClasses('dimmable dimmed', false)
300-
301-
waitForClassesCleanup(done)
302-
})
303-
304-
it('adds an inverted dimmer to the body', (done) => {
305-
wrapperMount(<Modal open dimmer='inverted' />)
306-
307-
assertBodyContains('.ui.inverted.page.modals.dimmer.transition.visible.active')
308-
waitForClassesCleanup(done)
309-
})
257+
it('passes "inverted" to ModalDimmer', () => {
258+
wrapperMount(<Modal open dimmer='inverted' />)
259+
wrapper.find('ModalDimmer').should.have.prop('inverted', true)
310260
})
311261

312262
describe('object', () => {
@@ -550,41 +500,42 @@ describe('Modal', () => {
550500
window.innerHeight = innerHeight
551501
})
552502

553-
it('does not add the scrolling class to the body by default', (done) => {
503+
it('does not pass "scrolling" by default', () => {
554504
wrapperMount(<Modal open />)
555-
556-
assertBodyClasses('scrolling', false)
557-
waitForClassesCleanup(done)
505+
wrapper.find('ModalDimmer').should.have.prop('scrolling', false)
558506
})
559507

560-
it('does not add the scrolling class to the body when equal to the window height', (done) => {
508+
it('does not pass "scrolling" when equal to the window height', (done) => {
561509
/* 101 is `padding * 2 + 1, see Modal/utils */
562510
const height = window.innerHeight - 101
511+
563512
wrapperMount(
564513
<Modal open style={{ height }}>
565514
foo
566515
</Modal>,
567516
)
568517

569518
requestAnimationFrame(() => {
570-
assertBodyClasses('scrolling', false)
519+
wrapper.update()
520+
wrapper.find('ModalDimmer').should.have.prop('scrolling', false)
521+
571522
done()
572523
})
573524
})
574525

575-
it('adds the scrolling class to the body when taller than the window', (done) => {
526+
it('passes "scrolling" when taller than the window', (done) => {
576527
window.innerHeight = 10
577528
wrapperMount(<Modal open>foo</Modal>)
578529

579530
requestAnimationFrame(() => {
580-
assertBodyClasses('scrolling')
531+
wrapper.update()
532+
wrapper.find('ModalDimmer').should.have.prop('scrolling', true)
533+
581534
done()
582535
})
583536
})
584537

585-
it('adds/removes the scrolling class to the body when the window grows/shrinks', (done) => {
586-
assertBodyClasses('scrolling', false)
587-
538+
it('passes "scrolling" when the window grows/shrinks', (done) => {
588539
wrapperMount(
589540
<Modal open>
590541
<span />
@@ -594,51 +545,18 @@ describe('Modal', () => {
594545

595546
assertWithTimeout(
596547
() => {
597-
assertBodyClasses('scrolling')
548+
wrapper.update()
549+
wrapper.find('ModalDimmer').should.have.prop('scrolling', true)
550+
598551
window.innerHeight = 10000
599552
},
600553
() =>
601554
assertWithTimeout(() => {
602-
assertBodyClasses('scrolling', false)
555+
wrapper.update()
556+
wrapper.find('ModalDimmer').should.have.prop('scrolling', false)
603557
}, done),
604558
)
605559
})
606-
607-
it('adds the scrolling class to the body after re-open', (done) => {
608-
assertBodyClasses('scrolling', false)
609-
610-
window.innerHeight = 10
611-
wrapperMount(<Modal defaultOpen>foo</Modal>)
612-
613-
assertWithTimeout(
614-
() => {
615-
assertBodyClasses('scrolling')
616-
domEvent.click('.ui.dimmer')
617-
},
618-
() =>
619-
assertWithTimeout(
620-
() => {
621-
assertBodyClasses('scrolling', false)
622-
wrapper.setProps({ open: true })
623-
},
624-
() =>
625-
assertWithTimeout(() => {
626-
assertBodyClasses('scrolling')
627-
}, done),
628-
),
629-
)
630-
})
631-
632-
it('removes the scrolling class from the body on unmount', (done) => {
633-
assertBodyClasses('scrolling', false)
634-
635-
window.innerHeight = 10
636-
wrapperMount(<Modal open>foo</Modal>)
637-
638-
waitForClassesCleanup(done, () => {
639-
assertBodyClasses('scrolling', false)
640-
})
641-
})
642560
})
643561

644562
describe('server-side', () => {

test/specs/modules/Modal/ModalDimmer-test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ describe('ModalDimmer', () => {
1111

1212
common.propKeyOnlyToClassName(ModalDimmer, 'inverted')
1313

14+
it('has required classes', () => {
15+
const wrapper = mount(<ModalDimmer mountNode={null} />)
16+
17+
expect(wrapper).to.have.className('page')
18+
expect(wrapper).to.have.className('modals')
19+
expect(wrapper).to.have.className('dimmer')
20+
expect(wrapper).to.have.className('transition')
21+
expect(wrapper).to.have.className('visible')
22+
expect(wrapper).to.have.className('active')
23+
})
24+
1425
describe('children', () => {
15-
it('adds classes to "MountNode"', () => {
26+
it('adds classes to "mountNode"', () => {
1627
const element = document.createElement('div')
1728
mount(<ModalDimmer mountNode={element} />)
1829

@@ -22,7 +33,7 @@ describe('ModalDimmer', () => {
2233
})
2334

2435
describe('blurring', () => {
25-
it('adds nothing "MountNode" by default', () => {
36+
it('adds nothing "mountNode" by default', () => {
2637
const element = document.createElement('div')
2738
mount(<ModalDimmer mountNode={element} />)
2839

0 commit comments

Comments
 (0)