Skip to content

Commit

Permalink
Add tests expecting the deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonlenz committed Jan 26, 2021
1 parent f4e954f commit d6194d3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import React from 'react'
import { render } from '@testing-library/react'
import { renderHook, act } from '@testing-library/react-hooks'

jest.mock('../../deprecation')
import { deprecationWarning } from '../../deprecation'
import { Modal, connectModal, useModal } from './Modal'

describe('Modal component', () => {
it('renders without errors', () => {
const { queryByTestId } = render(<Modal>My Modal</Modal>)
expect(queryByTestId('modal')).toBeInTheDocument()
expect(deprecationWarning).toHaveBeenCalledTimes(1)
})
})

const TestModal = (): React.ReactElement => <div>My Modal</div>

describe('connectModal', () => {
beforeEach(() => {
jest.clearAllMocks()
})
const mockClose = jest.fn()

describe('if isOpen is false', () => {
Expand All @@ -23,6 +29,7 @@ describe('connectModal', () => {
<TestConnectedModal isOpen={false} onClose={mockClose} />
)
expect(queryByText('My Modal')).not.toBeInTheDocument()
expect(deprecationWarning).toHaveBeenCalledTimes(1)
})
})

Expand All @@ -33,13 +40,19 @@ describe('connectModal', () => {
<TestConnectedModal isOpen={true} onClose={mockClose} />
)
expect(queryByText('My Modal')).toBeInTheDocument()
expect(deprecationWarning).toHaveBeenCalledTimes(2)
})
})
})

describe('useModal', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('provides state and functions for opening/closing a modal', () => {
const { result } = renderHook(() => useModal())
expect(deprecationWarning).toHaveBeenCalledTimes(1)
expect(result.current.isOpen).toEqual(false)
expect(typeof result.current.openModal).toBe('function')
expect(typeof result.current.closeModal).toBe('function')
Expand All @@ -49,11 +62,13 @@ describe('useModal', () => {
})

expect(result.current.isOpen).toEqual(true)
expect(deprecationWarning).toHaveBeenCalledTimes(2)

act(() => {
result.current.closeModal()
})

expect(result.current.isOpen).toEqual(false)
expect(deprecationWarning).toHaveBeenCalledTimes(3)
})
})

0 comments on commit d6194d3

Please sign in to comment.