Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 773 Bytes

example-react-modal.mdx

File metadata and controls

42 lines (35 loc) · 773 Bytes
id title
example-react-modal
Modals
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

const Modal = ({ onClose, children }) => {
  return (
    <div onClick={onClose}>
      <div onClick={e => e.stopPropagation()}>
        {children}
        <hr />
        <button onClick={onClose}>Close</button>
      </div>
    </div>
  )
}

test('modal shows the children and a close button', () => {
  // Arrange
  const handleClose = jest.fn()

  // Act
  const { getByText } = render(
    <Modal onClose={handleClose}>
      <div>test</div>
    </Modal>
  )
  // Assert
  expect(getByText('test')).toBeTruthy()

  // Act
  fireEvent.click(getByText(/close/i))

  // Assert
  expect(handleClose).toHaveBeenCalledTimes(1)
})