|
| 1 | +import { ModalManager } from '@/components/Modal/modalManager'; |
| 2 | +import { createRef } from 'react'; |
| 3 | + |
| 4 | +describe('Test modalManager', function () { |
| 5 | + it('should add backdrop if modal needs it', () => { |
| 6 | + const manager = new ModalManager(); |
| 7 | + manager.addModal({ |
| 8 | + ref: createRef(), |
| 9 | + backdrop: true |
| 10 | + }) |
| 11 | + |
| 12 | + expect(document.body.querySelector('.modal-backdrop')).not.toBeNull(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should add and remove class from body', () => { |
| 16 | + const ref = createRef<HTMLDivElement>(); |
| 17 | + const manager = new ModalManager(); |
| 18 | + manager.addModal({ |
| 19 | + ref: ref |
| 20 | + }) |
| 21 | + |
| 22 | + expect(document.body.classList.contains('has-modal')).toBeTruthy(); |
| 23 | + |
| 24 | + manager.removeModal(ref); |
| 25 | + |
| 26 | + expect(document.body.classList.contains('has-modal')).toBeFalsy(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should do nothing when ref not found', () => { |
| 30 | + const ref = createRef<HTMLDivElement>(); |
| 31 | + const otherRef = createRef<HTMLDivElement>(); |
| 32 | + const manager = new ModalManager(); |
| 33 | + manager.addModal({ |
| 34 | + ref: ref |
| 35 | + }) |
| 36 | + |
| 37 | + manager.removeModal(otherRef); |
| 38 | + |
| 39 | + expect(manager.modals.length).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should do nothing when the last modal with no backdrop', () => { |
| 43 | + const manager = new ModalManager(); |
| 44 | + manager.addModal({ |
| 45 | + ref: createRef<HTMLDivElement>(), |
| 46 | + backdrop: false |
| 47 | + }); |
| 48 | + manager.removeBackdrop(); |
| 49 | + |
| 50 | + expect(document.body.querySelector('.modal-backdrop')).toBeNull(); |
| 51 | + }) |
| 52 | +}); |
0 commit comments