|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { userEvent } from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import { BaseModal, Modal, ModalHeader } from './Modal'; |
| 6 | + |
| 7 | +describe('Modal components', () => { |
| 8 | + describe('BaseModal', () => { |
| 9 | + test('renders children into a portal with default classes', () => { |
| 10 | + render( |
| 11 | + <BaseModal> |
| 12 | + <div className="inner-content">hello</div> |
| 13 | + </BaseModal> |
| 14 | + ); |
| 15 | + |
| 16 | + const wrapper = document.querySelector('.modal-wrapper'); |
| 17 | + expect(wrapper).not.toBeNull(); |
| 18 | + expect(document.querySelector('.modal-backdrop')).not.toBeNull(); |
| 19 | + |
| 20 | + const dialog = document.querySelector('.modal-dialog'); |
| 21 | + expect(dialog).not.toBeNull(); |
| 22 | + expect(dialog.classList.contains('modal-sm')).toBe(false); |
| 23 | + expect(dialog.classList.contains('modal-lg')).toBe(false); |
| 24 | + |
| 25 | + expect(document.querySelector('.modal-content .inner-content').textContent).toEqual('hello'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('applies bsSize="sm" class', () => { |
| 29 | + render(<BaseModal bsSize="sm">child</BaseModal>); |
| 30 | + const dialog = document.querySelector('.modal-dialog'); |
| 31 | + expect(dialog.classList.contains('modal-sm')).toBe(true); |
| 32 | + expect(dialog.classList.contains('modal-lg')).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + test('applies bsSize="lg" class', () => { |
| 36 | + render(<BaseModal bsSize="lg">child</BaseModal>); |
| 37 | + const dialog = document.querySelector('.modal-dialog'); |
| 38 | + expect(dialog.classList.contains('modal-lg')).toBe(true); |
| 39 | + expect(dialog.classList.contains('modal-sm')).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + test('applies custom className', () => { |
| 43 | + render(<BaseModal className="custom-class">child</BaseModal>); |
| 44 | + const dialog = document.querySelector('.modal-dialog'); |
| 45 | + expect(dialog.classList.contains('custom-class')).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + test('toggles "no-scroll" on document.body while mounted', () => { |
| 49 | + expect(document.body.classList.contains('no-scroll')).toBe(false); |
| 50 | + const { unmount } = render(<BaseModal>child</BaseModal>); |
| 51 | + expect(document.body.classList.contains('no-scroll')).toBe(true); |
| 52 | + unmount(); |
| 53 | + expect(document.body.classList.contains('no-scroll')).toBe(false); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('ModalHeader', () => { |
| 58 | + test('renders title and no close button by default', () => { |
| 59 | + render(<ModalHeader title="My Title" />); |
| 60 | + const header = document.querySelector('.modal-header'); |
| 61 | + expect(header).not.toBeNull(); |
| 62 | + expect(header.querySelector('.modal-title').textContent).toEqual('My Title'); |
| 63 | + expect(header.querySelector('button.close')).toBeNull(); |
| 64 | + }); |
| 65 | + |
| 66 | + test('renders close button when onCancel is provided and invokes it on click', async () => { |
| 67 | + const onCancel = jest.fn(); |
| 68 | + render(<ModalHeader onCancel={onCancel} title="t" />); |
| 69 | + const closeBtn = document.querySelector('button.close'); |
| 70 | + expect(closeBtn).not.toBeNull(); |
| 71 | + expect(closeBtn.querySelector('.sr-only').textContent).toEqual('Close'); |
| 72 | + await userEvent.click(closeBtn); |
| 73 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + test('does not render the title element when title is falsy', () => { |
| 77 | + render(<ModalHeader onCancel={jest.fn()} title={null} />); |
| 78 | + expect(document.querySelector('.modal-title')).toBeNull(); |
| 79 | + // Close button should still be present |
| 80 | + expect(document.querySelector('button.close')).not.toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('renders children inside the header', () => { |
| 84 | + render( |
| 85 | + <ModalHeader title="t"> |
| 86 | + <span className="extra-child">extra</span> |
| 87 | + </ModalHeader> |
| 88 | + ); |
| 89 | + const header = document.querySelector('.modal-header'); |
| 90 | + expect(header.querySelector('.extra-child').textContent).toEqual('extra'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('Modal', () => { |
| 95 | + test('renders children inside modal-body', () => { |
| 96 | + render( |
| 97 | + <Modal onCancel={jest.fn()}> |
| 98 | + <div className="body-child">body content</div> |
| 99 | + </Modal> |
| 100 | + ); |
| 101 | + const body = document.querySelector('.modal-body'); |
| 102 | + expect(body).not.toBeNull(); |
| 103 | + expect(body.querySelector('.body-child').textContent).toEqual('body content'); |
| 104 | + }); |
| 105 | + |
| 106 | + test('renders default ModalHeader when title or onCancel is provided and no custom header', () => { |
| 107 | + render(<Modal onCancel={jest.fn()} title="Hello" />); |
| 108 | + const header = document.querySelector('.modal-header'); |
| 109 | + expect(header).not.toBeNull(); |
| 110 | + expect(header.querySelector('.modal-title').textContent).toEqual('Hello'); |
| 111 | + expect(header.querySelector('button.close')).not.toBeNull(); |
| 112 | + }); |
| 113 | + |
| 114 | + test('does not render header when no title, onCancel, or custom header is given', () => { |
| 115 | + render(<Modal onConfirm={jest.fn()}>body</Modal>); |
| 116 | + expect(document.querySelector('.modal-header')).toBeNull(); |
| 117 | + }); |
| 118 | + |
| 119 | + test('renders custom header instead of default ModalHeader', () => { |
| 120 | + render( |
| 121 | + <Modal header={<div className="custom-header">custom</div>} onCancel={jest.fn()} title="ignored"> |
| 122 | + body |
| 123 | + </Modal> |
| 124 | + ); |
| 125 | + // Default ModalHeader should not render when a custom header is supplied |
| 126 | + expect(document.querySelector('.modal-header')).toBeNull(); |
| 127 | + expect(document.querySelector('.custom-header').textContent).toEqual('custom'); |
| 128 | + }); |
| 129 | + |
| 130 | + test('renders custom footer when provided and skips ModalButtons', () => { |
| 131 | + render( |
| 132 | + <Modal footer={<span className="custom-footer">f</span>} onCancel={jest.fn()} onConfirm={jest.fn()}> |
| 133 | + body |
| 134 | + </Modal> |
| 135 | + ); |
| 136 | + const footer = document.querySelector('.modal-footer'); |
| 137 | + expect(footer).not.toBeNull(); |
| 138 | + expect(footer.querySelector('.custom-footer').textContent).toEqual('f'); |
| 139 | + // ModalButtons applies the 'modal-buttons' class — should not be present |
| 140 | + expect(document.querySelector('.modal-buttons')).toBeNull(); |
| 141 | + }); |
| 142 | + |
| 143 | + test('renders ModalButtons when no footer is provided', async () => { |
| 144 | + const onCancel = jest.fn(); |
| 145 | + const onConfirm = jest.fn(); |
| 146 | + render( |
| 147 | + <Modal cancelText="Nope" confirmText="Go" onCancel={onCancel} onConfirm={onConfirm}> |
| 148 | + body |
| 149 | + </Modal> |
| 150 | + ); |
| 151 | + |
| 152 | + const buttons = document.querySelector('.modal-footer.modal-buttons'); |
| 153 | + expect(buttons).not.toBeNull(); |
| 154 | + |
| 155 | + const buttonEls = buttons.querySelectorAll('button'); |
| 156 | + // First button is cancel, last is confirm |
| 157 | + const cancelBtn = Array.from(buttonEls).find(b => b.textContent === 'Nope'); |
| 158 | + const confirmBtn = Array.from(buttonEls).find(b => b.textContent === 'Go'); |
| 159 | + expect(cancelBtn).toBeDefined(); |
| 160 | + expect(confirmBtn).toBeDefined(); |
| 161 | + |
| 162 | + await userEvent.click(cancelBtn); |
| 163 | + await userEvent.click(confirmBtn); |
| 164 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 165 | + expect(onConfirm).toHaveBeenCalledTimes(1); |
| 166 | + }); |
| 167 | + |
| 168 | + test('renders footerContent inside default ModalButtons', () => { |
| 169 | + render( |
| 170 | + <Modal footerContent={<span className="fc">fc</span>} onCancel={jest.fn()} onConfirm={jest.fn()}> |
| 171 | + body |
| 172 | + </Modal> |
| 173 | + ); |
| 174 | + const buttons = document.querySelector('.modal-footer.modal-buttons'); |
| 175 | + expect(buttons).not.toBeNull(); |
| 176 | + expect(buttons.querySelector('.fc').textContent).toEqual('fc'); |
| 177 | + }); |
| 178 | + |
| 179 | + test('passes bsSize and className down to BaseModal', () => { |
| 180 | + render( |
| 181 | + <Modal bsSize="lg" className="my-modal" onCancel={jest.fn()}> |
| 182 | + body |
| 183 | + </Modal> |
| 184 | + ); |
| 185 | + const dialog = document.querySelector('.modal-dialog'); |
| 186 | + expect(dialog.classList.contains('modal-lg')).toBe(true); |
| 187 | + expect(dialog.classList.contains('my-modal')).toBe(true); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments