|
1 | | -import { render } from '@testing-library/react'; |
2 | | -import { createRef } from 'react'; |
| 1 | +import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; |
| 2 | +import { createRef, useState } from 'react'; |
3 | 3 | import { testA11y } from '@interlay/test-utils'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
4 | 5 |
|
5 | 6 | import { Modal, ModalBody, ModalDivider, ModalFooter, ModalHeader } from '..'; |
6 | 7 |
|
@@ -43,4 +44,106 @@ describe('Modal', () => { |
43 | 44 | </Modal> |
44 | 45 | ); |
45 | 46 | }); |
| 47 | + |
| 48 | + it('should control open state', async () => { |
| 49 | + const Component = () => { |
| 50 | + const [isOpen, setOpen] = useState(false); |
| 51 | + |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <button onClick={() => setOpen(true)}>trigger</button> |
| 55 | + <Modal isOpen={isOpen} onClose={() => setOpen(false)}> |
| 56 | + <ModalHeader>title</ModalHeader> |
| 57 | + <ModalDivider /> |
| 58 | + <ModalBody>body</ModalBody> |
| 59 | + <ModalFooter>footer</ModalFooter> |
| 60 | + </Modal> |
| 61 | + </> |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + render(<Component />); |
| 66 | + |
| 67 | + userEvent.click(screen.getByRole('button', { name: /trigger/i })); |
| 68 | + |
| 69 | + await waitFor(() => { |
| 70 | + expect(screen.getByRole('dialog', { name: /title/i })); |
| 71 | + }); |
| 72 | + |
| 73 | + userEvent.click(screen.getByRole('button', { name: /dismiss/i })); |
| 74 | + |
| 75 | + await waitForElementToBeRemoved(screen.getByRole('dialog', { name: /title/i })); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should modal sections', async () => { |
| 79 | + render( |
| 80 | + <Modal isOpen onClose={jest.fn}> |
| 81 | + <ModalHeader>title</ModalHeader> |
| 82 | + <ModalDivider /> |
| 83 | + <ModalBody>body</ModalBody> |
| 84 | + <ModalFooter>footer</ModalFooter> |
| 85 | + </Modal> |
| 86 | + ); |
| 87 | + |
| 88 | + expect(screen.getByRole('heading', { level: 3, name: /title/i })).toBeInTheDocument(); |
| 89 | + expect(screen.getByText(/body/i)).toBeInTheDocument(); |
| 90 | + expect(screen.getByText(/footer/i)).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should emit onClose using close btn', async () => { |
| 94 | + const handleClose = jest.fn(); |
| 95 | + |
| 96 | + render( |
| 97 | + <Modal isOpen onClose={handleClose}> |
| 98 | + <ModalHeader>title</ModalHeader> |
| 99 | + <ModalDivider /> |
| 100 | + <ModalBody>body</ModalBody> |
| 101 | + <ModalFooter>footer</ModalFooter> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | + |
| 105 | + userEvent.click(screen.getByRole('button', { name: /dismiss/i })); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should emit onClose using ESC key', async () => { |
| 113 | + const handleClose = jest.fn(); |
| 114 | + |
| 115 | + render( |
| 116 | + <Modal isOpen onClose={handleClose}> |
| 117 | + <ModalHeader>title</ModalHeader> |
| 118 | + <ModalDivider /> |
| 119 | + <ModalBody>body</ModalBody> |
| 120 | + <ModalFooter>footer</ModalFooter> |
| 121 | + </Modal> |
| 122 | + ); |
| 123 | + |
| 124 | + userEvent.keyboard('{Escape}'); |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should emit onClose by clicking modal wrapper', async () => { |
| 132 | + const handleClose = jest.fn(); |
| 133 | + |
| 134 | + render( |
| 135 | + <Modal isOpen onClose={handleClose}> |
| 136 | + <ModalHeader>title</ModalHeader> |
| 137 | + <ModalDivider /> |
| 138 | + <ModalBody>body</ModalBody> |
| 139 | + <ModalFooter>footer</ModalFooter> |
| 140 | + </Modal> |
| 141 | + ); |
| 142 | + |
| 143 | + userEvent.click(screen.getByRole('dialog', { name: /title/i }).parentElement?.parentElement as any); |
| 144 | + |
| 145 | + await waitFor(() => { |
| 146 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 147 | + }); |
| 148 | + }); |
46 | 149 | }); |
0 commit comments