FeatureHighlight.test.jsx (2413B)
1 import React from "react"; 2 import { mount } from "enzyme"; 3 4 import { FeatureHighlight } from "content-src/components/DiscoveryStreamComponents/FeatureHighlight/FeatureHighlight"; 5 6 describe("<FeatureHighlight>", () => { 7 let wrapper; 8 let fakeWindow; 9 10 beforeEach(() => { 11 wrapper = mount(<FeatureHighlight />); 12 }); 13 14 it("should render", () => { 15 assert.ok(wrapper.exists()); 16 assert.ok(wrapper.find(".feature-highlight").exists()); 17 }); 18 19 it("should render a title", () => { 20 wrapper.setProps({ message: "foo" }); 21 assert.ok(wrapper.find(".feature-highlight-modal p").exists()); 22 assert.equal(wrapper.find(".feature-highlight-modal p").text(), "foo"); 23 }); 24 25 it("should open a modal", () => { 26 assert.ok(wrapper.find(".feature-highlight-modal.closed").exists()); 27 wrapper.find(".toggle-button").simulate("click"); 28 assert.ok(wrapper.find(".feature-highlight-modal.opened").exists()); 29 wrapper.find("moz-button").simulate("click"); 30 assert.ok(wrapper.find(".feature-highlight-modal.closed").exists()); 31 }); 32 33 it("should close a modal if clicking outside", () => { 34 fakeWindow = { 35 document: { 36 addEventListener: (event, handler) => { 37 fakeWindow.document.handleOutsideClick = handler; 38 }, 39 removeEventListener: () => {}, 40 }, 41 }; 42 wrapper.setProps({ windowObj: fakeWindow }); 43 44 wrapper.find(".toggle-button").simulate("click"); 45 fakeWindow.document.handleOutsideClick({ target: null }); 46 }); 47 48 it("should call outsideClickCallback on Escape key press", () => { 49 const outsideClickCallback = sinon.spy(); 50 51 fakeWindow = { 52 document: { 53 addEventListener: (event, handler) => { 54 if (event === "keydown") { 55 fakeWindow.document.keydownHandler = handler; 56 } 57 }, 58 removeEventListener: () => {}, 59 }, 60 }; 61 62 wrapper = mount( 63 <FeatureHighlight 64 windowObj={fakeWindow} 65 outsideClickCallback={outsideClickCallback} 66 /> 67 ); 68 69 // Open the modal so we can test closing it with Escape 70 wrapper.find(".toggle-button").simulate("click"); 71 assert(wrapper.find(".feature-highlight-modal.opened").exists()); 72 73 // Simulate Escape key press 74 fakeWindow.document.keydownHandler({ key: "Escape" }); 75 76 assert.calledOnce(outsideClickCallback); 77 assert(wrapper.find(".feature-highlight-modal.closed").exists); 78 }); 79 });