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)
})