|
| 1 | +import React from 'react'; |
| 2 | +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; |
| 3 | + |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | + |
| 6 | +import { GlobalModal } from '../GlobalModal'; |
| 7 | +import { ModalDialogManagerProvider } from '../../../context'; |
| 8 | + |
| 9 | +const renderComponent = ({ props } = {}) => |
| 10 | + render( |
| 11 | + <ModalDialogManagerProvider> |
| 12 | + <GlobalModal {...props} /> |
| 13 | + </ModalDialogManagerProvider>, |
| 14 | + ); |
| 15 | + |
| 16 | +describe('GlobalModal', () => { |
| 17 | + const textContent = 'some text'; |
| 18 | + afterEach(cleanup); |
| 19 | + |
| 20 | + it('should be closed (null) if the `open` prop is set to false', () => { |
| 21 | + const { container } = renderComponent({ props: { open: false } }); |
| 22 | + |
| 23 | + expect(container).toBeEmptyDOMElement(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should be open if the `open` prop is set to true', () => { |
| 27 | + const { container } = renderComponent({ props: { open: true } }); |
| 28 | + const dialogOverlay = container.firstChild; |
| 29 | + |
| 30 | + expect(dialogOverlay.firstChild).toHaveClass('str-chat__modal--open'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should render what is passed as props.children when opened', () => { |
| 34 | + const { queryByText } = renderComponent({ |
| 35 | + props: { children: textContent, open: true }, |
| 36 | + }); |
| 37 | + |
| 38 | + expect(queryByText(textContent)).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call the onClose prop function if the escape key is pressed', () => { |
| 42 | + const onClose = jest.fn(); |
| 43 | + renderComponent({ |
| 44 | + props: { onClose, open: true }, |
| 45 | + }); |
| 46 | + |
| 47 | + fireEvent( |
| 48 | + document, |
| 49 | + new KeyboardEvent('keydown', { |
| 50 | + key: 'Escape', |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should remove the escape keydown event handler on unmount', () => { |
| 58 | + const onClose = jest.fn(); |
| 59 | + const { unmount } = renderComponent({ |
| 60 | + props: { onClose, open: true }, |
| 61 | + }); |
| 62 | + |
| 63 | + unmount(); |
| 64 | + fireEvent( |
| 65 | + document, |
| 66 | + new KeyboardEvent('keydown', { |
| 67 | + key: 'Escape', |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + expect(onClose).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not call onClose if the inside of the modal was clicked', () => { |
| 75 | + const onClose = jest.fn(); |
| 76 | + renderComponent({ |
| 77 | + props: { children: textContent, onClose, open: true }, |
| 78 | + }); |
| 79 | + const textContainer = screen.queryByText(textContent); |
| 80 | + |
| 81 | + fireEvent.click(textContainer); |
| 82 | + |
| 83 | + expect(onClose).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should call onClose if the close button is clicked', () => { |
| 87 | + const onClose = jest.fn(); |
| 88 | + renderComponent({ |
| 89 | + props: { children: textContent, onClose, open: true }, |
| 90 | + }); |
| 91 | + const closeButton = screen.getByTitle('Close'); |
| 92 | + fireEvent.click(closeButton); |
| 93 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call onClose if the modal overlay is clicked', () => { |
| 97 | + const onClose = jest.fn(); |
| 98 | + const { container, debug } = renderComponent({ |
| 99 | + props: { children: textContent, onClose, open: true }, |
| 100 | + }); |
| 101 | + console.log(debug(container)); |
| 102 | + const dialogOverlay = container.querySelector('.str-chat__modal'); |
| 103 | + |
| 104 | + fireEvent.click(dialogOverlay); |
| 105 | + |
| 106 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 107 | + }); |
| 108 | +}); |
0 commit comments