summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/asrouter/ModalOverlay.test.jsx
blob: 889d26a9d3bd0288a5de19983bbf4a8ef71254aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { ModalOverlayWrapper } from "content-src/asrouter/components/ModalOverlay/ModalOverlay";
import { mount } from "enzyme";
import React from "react";

describe("ModalOverlayWrapper", () => {
  let fakeDoc;
  let sandbox;
  let header;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    header = document.createElement("div");

    fakeDoc = {
      addEventListener: sandbox.stub(),
      removeEventListener: sandbox.stub(),
      body: { classList: { add: sandbox.stub(), remove: sandbox.stub() } },
      getElementById() {
        return header;
      },
    };
  });
  afterEach(() => {
    sandbox.restore();
  });
  it("should add eventListener and a class on mount", async () => {
    mount(<ModalOverlayWrapper document={fakeDoc} />);
    assert.calledOnce(fakeDoc.addEventListener);
    assert.calledWith(fakeDoc.body.classList.add, "modal-open");
  });

  it("should remove eventListener on unmount", async () => {
    const wrapper = mount(<ModalOverlayWrapper document={fakeDoc} />);
    wrapper.unmount();
    assert.calledOnce(fakeDoc.addEventListener);
    assert.calledOnce(fakeDoc.removeEventListener);
    assert.calledWith(fakeDoc.body.classList.remove, "modal-open");
  });

  it("should call props.onClose on an Escape key", async () => {
    const onClose = sandbox.stub();
    mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />);

    // Simulate onkeydown being called
    const [, callback] = fakeDoc.addEventListener.firstCall.args;
    callback({ key: "Escape" });

    assert.calledOnce(onClose);
  });

  it("should not call props.onClose on other keys than Escape", async () => {
    const onClose = sandbox.stub();
    mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />);

    // Simulate onkeydown being called
    const [, callback] = fakeDoc.addEventListener.firstCall.args;
    callback({ key: "Ctrl" });

    assert.notCalled(onClose);
  });

  it("should not call props.onClose when clicked outside dialog", async () => {
    const onClose = sandbox.stub();
    const wrapper = mount(
      <ModalOverlayWrapper document={fakeDoc} onClose={onClose} />
    );
    wrapper.find("div.modalOverlayOuter.active").simulate("click");
    assert.notCalled(onClose);
  });
});