"use strict";

var _react = _interopRequireDefault(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _enzyme = require("enzyme");

var _index = require("../index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/

/* eslint-env browser */
jest.useFakeTimers(); // Mock React 16 portals in a way that makes them easy to test

var originalCreatePortal = _reactDom.default.createPortal; // Mock document.addEventListener

var originalDocumentAddListener = document.addEventListener;
describe('Modal', function () {
  var wrapper;
  beforeAll(function () {
    // $FlowFixMe
    _reactDom.default.createPortal = jest.fn(function (e) {
      return _react.default.createElement("div", {
        "is-portal": "true",
        key: "portal"
      }, e);
    }); // $FlowFixMe

    document.addEventListener = jest.fn();
  });
  afterEach(function () {
    _reactDom.default.createPortal.mockClear();

    document.addEventListener.mockClear();
    wrapper && wrapper.unmount();
  });
  afterAll(function () {
    // $FlowFixMe
    _reactDom.default.createPortal = originalCreatePortal; // $FlowFixMe

    document.addEventListener = originalDocumentAddListener;
  });
  test('renders nothing when closed', function () {
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: false
    }, _react.default.createElement(_index.ModalBody, null, "Hello world")));
    expect(wrapper).toBeEmptyRender();
  });
  test('renders portal when open', function () {
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: true
    }, _react.default.createElement(_index.ModalHeader, null, "Hello world"), _react.default.createElement(_index.ModalBody, null, "Modal Body"), _react.default.createElement(_index.ModalFooter, null, "Footer")));
    expect(wrapper).toMatchSnapshot('Rendered Modal');
  });
  test('close button triggers close', function () {
    var onClose = jest.fn();
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: true,
      onClose: onClose
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    wrapper.find(_index.StyledClose).simulate('click');
    expect(onClose).toHaveBeenCalledTimes(1);
    expect(onClose).toHaveBeenLastCalledWith({
      closeSource: _index.CLOSE_SOURCE.closeButton
    });
  });
  test('backdrop triggers close', function () {
    var onClose = jest.fn();
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: true,
      onClose: onClose
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    wrapper.find(_index.StyledBackdrop).simulate('click');
    expect(onClose).toHaveBeenCalledTimes(1);
    expect(onClose).toHaveBeenLastCalledWith({
      closeSource: _index.CLOSE_SOURCE.backdrop
    });
  });
  test('disable closeable', function () {
    var onClose = jest.fn();
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: true,
      closeable: false,
      onClose: onClose
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    expect(wrapper.find(_index.StyledClose)).not.toExist();
    wrapper.find(_index.StyledBackdrop).simulate('click');
    expect(onClose).not.toHaveBeenCalled();
  });
  test('prevents scroll on mount node', function () {
    var onClose = jest.fn();
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      onClose: onClose
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    var body = document.body;
    expect(body.style.overflow).toBe('');
    wrapper.setProps({
      isOpen: true
    });
    expect(body.style.overflow).toBe('hidden');
    wrapper.setProps({
      isOpen: false
    });
    expect(body.style.overflow).toBe('');
  });
  test('override components', function () {
    var mock = function mock() {
      return jest.fn().mockImplementation(function (_ref) {
        var children = _ref.children;
        return _react.default.createElement("div", null, children);
      });
    };

    var Root = mock();
    var Backdrop = mock();
    var DialogContainer = mock();
    var Dialog = mock();
    var Close = mock();
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Modal, {
      isOpen: true,
      overrides: {
        // $FlowFixMe
        Root: Root,
        // $FlowFixMe
        Backdrop: Backdrop,
        // $FlowFixMe
        DialogContainer: DialogContainer,
        // $FlowFixMe
        Dialog: Dialog,
        // $FlowFixMe
        Close: Close
      }
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    expect(wrapper.find(Root)).toExist();
    expect(wrapper.find(Backdrop)).toExist();
    expect(wrapper.find(DialogContainer)).toExist();
    expect(wrapper.find(Dialog)).toExist();
    expect(wrapper.find(Close)).toExist();
  });
  test('role', function () {
    wrapper = (0, _enzyme.mount)( // eslint-disable-next-line jsx-a11y/aria-role
    _react.default.createElement(_index.Modal, {
      role: "mycustomrole",
      isOpen: true
    }, _react.default.createElement(_index.ModalBody, null, "Modal Body")));
    expect(wrapper.find(_index.StyledDialog)).toHaveProp('role', 'mycustomrole');
  });
});