ModalOverlay.test.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {ModalOverlayWrapper} from "content-src/asrouter/components/ModalOverlay/ModalOverlay";
  2. import {mount} from "enzyme";
  3. import React from "react";
  4. describe("ModalOverlayWrapper", () => {
  5. let fakeDoc;
  6. let sandbox;
  7. beforeEach(() => {
  8. sandbox = sinon.createSandbox();
  9. fakeDoc = {
  10. addEventListener: sandbox.stub(),
  11. removeEventListener: sandbox.stub(),
  12. body: {classList: {add: sandbox.stub(), remove: sandbox.stub()}},
  13. };
  14. });
  15. afterEach(() => {
  16. sandbox.restore();
  17. });
  18. it("should add eventListener and a class on mount", async () => {
  19. mount(<ModalOverlayWrapper document={fakeDoc} />);
  20. assert.calledOnce(fakeDoc.addEventListener);
  21. assert.calledWith(fakeDoc.body.classList.add, "modal-open");
  22. });
  23. it("should remove eventListener on unmount", async () => {
  24. const wrapper = mount(<ModalOverlayWrapper document={fakeDoc} />);
  25. wrapper.unmount();
  26. assert.calledOnce(fakeDoc.addEventListener);
  27. assert.calledOnce(fakeDoc.removeEventListener);
  28. assert.calledWith(fakeDoc.body.classList.remove, "modal-open");
  29. });
  30. it("should call props.onClose on an Escape key", async () => {
  31. const onClose = sandbox.stub();
  32. mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />);
  33. // Simulate onkeydown being called
  34. const [, callback] = fakeDoc.addEventListener.firstCall.args;
  35. callback({key: "Escape"});
  36. assert.calledOnce(onClose);
  37. });
  38. it("should not call props.onClose on other keys than Escape", async () => {
  39. const onClose = sandbox.stub();
  40. mount(<ModalOverlayWrapper document={fakeDoc} onClose={onClose} />);
  41. // Simulate onkeydown being called
  42. const [, callback] = fakeDoc.addEventListener.firstCall.args;
  43. callback({key: "Ctrl"});
  44. assert.notCalled(onClose);
  45. });
  46. });