ModalOverlay.test.jsx (2246B)
1 import { ModalOverlayWrapper } from "content-src/components/ModalOverlay/ModalOverlay"; 2 import { mount } from "enzyme"; 3 import React from "react"; 4 5 describe("ModalOverlayWrapper", () => { 6 let fakeDoc; 7 let sandbox; 8 let header; 9 beforeEach(() => { 10 sandbox = sinon.createSandbox(); 11 header = document.createElement("div"); 12 13 fakeDoc = { 14 addEventListener: sandbox.stub(), 15 removeEventListener: sandbox.stub(), 16 body: { classList: { add: sandbox.stub(), remove: sandbox.stub() } }, 17 getElementById() { 18 return header; 19 }, 20 }; 21 }); 22 afterEach(() => { 23 sandbox.restore(); 24 }); 25 it("should add eventListener and a class on mount", async () => { 26 mount(<ModalOverlayWrapper document={fakeDoc} />); 27 assert.calledOnce(fakeDoc.addEventListener); 28 assert.calledWith(fakeDoc.body.classList.add, "modal-open"); 29 }); 30 31 it("should remove eventListener on unmount", async () => { 32 const wrapper = mount(<ModalOverlayWrapper document={fakeDoc} />); 33 wrapper.unmount(); 34 assert.calledOnce(fakeDoc.addEventListener); 35 assert.calledOnce(fakeDoc.removeEventListener); 36 assert.calledWith(fakeDoc.body.classList.remove, "modal-open"); 37 }); 38 39 it("should call props.onClose on an Escape key", async () => { 40 const onClose = sandbox.stub(); 41 mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />); 42 43 // Simulate onkeydown being called 44 const [, callback] = fakeDoc.addEventListener.firstCall.args; 45 callback({ key: "Escape" }); 46 47 assert.calledOnce(onClose); 48 }); 49 50 it("should not call props.onClose on other keys than Escape", async () => { 51 const onClose = sandbox.stub(); 52 mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />); 53 54 // Simulate onkeydown being called 55 const [, callback] = fakeDoc.addEventListener.firstCall.args; 56 callback({ key: "Ctrl" }); 57 58 assert.notCalled(onClose); 59 }); 60 61 it("should not call props.onClose when clicked outside dialog", async () => { 62 const onClose = sandbox.stub(); 63 const wrapper = mount( 64 <ModalOverlayWrapper document={fakeDoc} onClose={onClose} /> 65 ); 66 wrapper.find("div.modalOverlayOuter.active").simulate("click"); 67 assert.notCalled(onClose); 68 }); 69 });