Skip to content

Commit

Permalink
test: Add tests for Modal has-body class
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Nov 22, 2019
1 parent 9230b45 commit ea627cf
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions react/Modal/index.spec.jsx
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)
})
})

0 comments on commit ea627cf

Please sign in to comment.