-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Presence of has-body when (un)mounting several modals (#1262)
fix: Presence of has-body when (un)mounting several modals
- Loading branch information
Showing
3 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react' | ||
import { act } from 'react-dom/test-utils' | ||
import { useState } from 'react' | ||
import { mount } from 'enzyme' | ||
import Modal, { BODY_CLASS } from './index' | ||
|
||
describe('Modal', () => { | ||
const Example = () => { | ||
const [showModal, setShowModal] = useState(false) | ||
const [showModal2, setShowModal2] = useState(false) | ||
return ( | ||
<div> | ||
{showModal ? <Modal title="example 1">1</Modal> : null} | ||
{showModal2 ? <Modal title="example 2">2</Modal> : null} | ||
<button onClick={() => setShowModal(true)} /> | ||
<button onClick={() => setShowModal2(true)} /> | ||
<button onClick={() => setShowModal2(false)} /> | ||
<button onClick={() => setShowModal(false)} /> | ||
</div> | ||
) | ||
} | ||
|
||
const clickNthButton = (root, n) => | ||
root | ||
.find('button') | ||
.at(n) | ||
.props() | ||
.onClick() | ||
|
||
const mountFirstModal = root => clickNthButton(root, 0) | ||
const mountSecondModal = root => clickNthButton(root, 1) | ||
const unmountSecondModal = root => clickNthButton(root, 2) | ||
const unmountFirstModal = root => clickNthButton(root, 3) | ||
|
||
const hasModalBodyClass = () => document.body.classList.contains(BODY_CLASS) | ||
|
||
afterEach(() => { | ||
document.body.classList.remove(BODY_CLASS) | ||
}) | ||
|
||
it('should apply a class to the body when mounted', () => { | ||
const root = mount(<Example />) | ||
expect(hasModalBodyClass()).toBe(false) | ||
act(() => { | ||
mountFirstModal(root) | ||
}) | ||
expect(hasModalBodyClass()).toBe(true) | ||
act(() => { | ||
unmountFirstModal(root) | ||
}) | ||
expect(hasModalBodyClass()).toBe(false) | ||
}) | ||
|
||
it('should not remove the class if two modals were mounted and the second one is removed', () => { | ||
const root = mount(<Example />) | ||
expect(hasModalBodyClass()).toBe(false) | ||
act(() => { | ||
mountFirstModal(root) | ||
}) | ||
expect(hasModalBodyClass()).toBe(true) | ||
act(() => { | ||
mountSecondModal(root) | ||
}) | ||
act(() => { | ||
unmountSecondModal(root) | ||
}) | ||
expect(hasModalBodyClass()).toBe(true) | ||
act(() => { | ||
unmountFirstModal(root) | ||
}) | ||
expect(hasModalBodyClass()).toBe(false) | ||
}) | ||
}) |