function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React, { useState } from 'react';
import '@testing-library/jest-dom/extend-expect';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { Button } from '../Button';
import { Modal } from './Modal';
var primaryButton = {
  onClick: jest.fn(),
  children: 'Primary'
};
var secondaryButton = {
  onClick: jest.fn(),
  children: 'Secondary'
};
var tertiaryButton = {
  onClick: jest.fn(),
  children: 'Tertiary'
};
describe('Modal', function () {
  var wrapper;
  var title;
  var onClose;
  describe('when the component is provided no title or buttons', function () {
    describe('and it is set to be initially open', function () {
      beforeEach(function () {
        wrapper = render( /*#__PURE__*/React.createElement(Modal, {
          "aria-label": "Accessible name",
          isOpen: true
        }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
      });
      it('does not set the `aria-labelledby` attribute', function () {
        expect(wrapper.getByTestId('modal-wrapper')).not.toHaveAttribute('aria-labelledby');
      });
      it('sets the `aria-label` attribute', function () {
        expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('aria-label', 'Accessible name');
      });
      it('should apply the `role` attribute', function () {
        expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('role', 'dialog');
      });
      it('should apply the `aria-modal` attribute', function () {
        expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('aria-modal', 'true');
      });
      it('should set the `aria-describedby` attribute to the ID of the content', function () {
        var descriptionId = wrapper.getByTestId('modal-body').getAttribute('id');
        expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('aria-describedby', descriptionId);
      });
      it('should open the modal', function () {
        expect(wrapper.getByTestId('modal-wrapper')).toHaveStyleRule('display', 'block');
      });
      it('should scroll on lower resolution screens', function () {
        expect(wrapper.getByTestId('modal-main')).toHaveStyleRule('max-height', '95vh');
        expect(wrapper.getByTestId('modal-main')).toHaveStyleRule('overflow-y', 'auto');
      });
      it('should not render the Header component', function () {
        expect(wrapper.queryByTestId('modal-header')).toBeNull();
      });
      it('should not render the Footer component', function () {
        expect(wrapper.queryByTestId('modal-footer')).toBeNull();
      });
    });
  });
  describe('when the component is provided a title', function () {
    beforeEach(function () {
      title = 'Modal Header';
    });
    describe('and the onClose callback is provided', function () {
      beforeEach(function () {
        onClose = jest.fn();
        wrapper = render( /*#__PURE__*/React.createElement(Modal, {
          isOpen: true,
          title: title,
          onClose: onClose
        }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
      });
      it('should set the `aria-labelledby` attribute to the ID of the title', function () {
        var titleId = wrapper.getByTestId('modal-header-text').getAttribute('id');
        expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('aria-labelledby', titleId);
      });
      it('should render the Header component', function () {
        expect(wrapper.queryByTestId('modal-header')).toBeInTheDocument();
      });
      it('should render the child JSX', function () {
        expect(wrapper.queryByTestId('modal-body')).toHaveTextContent('Example child JSX');
      });
      describe('and the close button is clicked', function () {
        beforeEach(function () {
          fireEvent(wrapper.getByTestId('modal-close'), new MouseEvent('click', {
            bubbles: true,
            cancelable: true
          }));
        });
        it('should invoke the onClose callback', function () {
          expect(onClose).toHaveBeenCalled();
        });
        it('should close the modal', function () {
          expect(wrapper.getByTestId('modal-wrapper')).toHaveStyleRule('display', 'none');
        });
      });
      describe('and buttons are provided', function () {
        beforeEach(function () {
          wrapper = render( /*#__PURE__*/React.createElement(Modal, {
            isOpen: true,
            title: title,
            onClose: onClose,
            primaryButton: primaryButton,
            secondaryButton: secondaryButton,
            tertiaryButton: tertiaryButton
          }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
        });
        it('should render the Footer component', function () {
          expect(wrapper.queryByTestId('modal-footer')).toBeInTheDocument();
        });
        it('should render all the buttons', function () {
          expect(wrapper.queryByTestId('modal-primary')).toBeInTheDocument();
          expect(wrapper.queryByTestId('modal-secondary')).toBeInTheDocument();
          expect(wrapper.queryByTestId('modal-tertiary')).toBeInTheDocument();
        });
        it('should render the primary button with an icon', function () {
          expect(wrapper.queryByTestId('modal-primary-confirm')).toBeInTheDocument();
        });
      });
      describe('and no primary button icon is specified', function () {
        beforeEach(function () {
          wrapper = render( /*#__PURE__*/React.createElement(Modal, {
            isOpen: true,
            title: title,
            onClose: onClose,
            primaryButton: _objectSpread(_objectSpread({}, primaryButton), {}, {
              icon: undefined
            })
          }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
        });
        it('does not render an icon for the primary button', function () {
          expect(wrapper.queryByTestId('modal-primary-confirm')).toBeNull();
        });
      });
    });
  });
  describe('when both title and aria-label are set', function () {
    beforeEach(function () {
      onClose = jest.fn();
      wrapper = render( /*#__PURE__*/React.createElement(Modal, {
        isOpen: true,
        title: "Title",
        "aria-label": "Accessible name"
      }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
    });
    it('does not set the `aria-labelledby` attribute', function () {
      expect(wrapper.getByTestId('modal-wrapper')).not.toHaveAttribute('aria-labelledby');
    });
    it('sets the `aria-label` attribute', function () {
      expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('aria-label', 'Accessible name');
    });
  });
  describe('when the state is being updated externally', function () {
    describe('and the model is instantiated as closed', function () {
      beforeEach(function () {
        var ModalWithUpdate = function ModalWithUpdate() {
          var _useState = useState(false),
            _useState2 = _slicedToArray(_useState, 2),
            isOpen = _useState2[0],
            setIsOpen = _useState2[1];
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
            onClick: function onClick() {
              setIsOpen(!isOpen);
            }
          }, isOpen ? 'Hide' : 'Show'), /*#__PURE__*/React.createElement(Modal, {
            isOpen: isOpen,
            onClose: function onClose() {
              setIsOpen(false);
            }
          }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
        };
        wrapper = render( /*#__PURE__*/React.createElement(ModalWithUpdate, null));
      });
      it('should be closed', function () {
        expect(wrapper.getByTestId('modal-wrapper')).toHaveStyleRule('display', 'none');
      });
      describe('and the button is clicked', function () {
        beforeEach(function () {
          wrapper.getByText('Show').click();
          return waitFor(function () {
            return wrapper.queryAllByText('Hide');
          });
        });
        it('should be open', function () {
          expect(wrapper.getByTestId('modal-wrapper')).toHaveStyleRule('display', 'block');
        });
        describe('and the button is clicked again', function () {
          beforeEach(function () {
            wrapper.getByText('Hide').click();
            return waitFor(function () {
              return wrapper.queryAllByText('Show');
            });
          });
          it('should be closed again', function () {
            expect(wrapper.getByTestId('modal-wrapper')).toHaveStyleRule('display', 'none');
          });
        });
      });
    });
  });
  describe('when the component has arbitrary props', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Modal, {
        "data-arbitrary": "arbitrary"
      }, /*#__PURE__*/React.createElement("span", null, "Example child JSX")));
    });
    it('should spread arbitrary props', function () {
      expect(wrapper.getByTestId('modal-wrapper')).toHaveAttribute('data-arbitrary', 'arbitrary');
    });
  });
  describe('when the Modal content changes', function () {
    var initialIds;
    var ExampleModal = function ExampleModal(_ref) {
      var content = _ref.content;
      return /*#__PURE__*/React.createElement(Modal, {
        title: "Example title",
        isOpen: true
      }, /*#__PURE__*/React.createElement("span", null, content));
    };
    function getIds() {
      return {
        titleId: wrapper.getByTestId('modal-header-text').id,
        descriptionId: wrapper.getByTestId('modal-body').id,
        labelledBy: wrapper.getByTestId('modal-wrapper').getAttribute('aria-labelledby'),
        describedBy: wrapper.getByTestId('modal-wrapper').getAttribute('aria-describedby')
      };
    }
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(ExampleModal, {
        content: "initial content"
      }));
      initialIds = getIds();
      wrapper.rerender( /*#__PURE__*/React.createElement(ExampleModal, {
        content: "new content"
      }));
    });
    it('does not generate new IDs', function () {
      expect(getIds()).toEqual(initialIds);
    });
  });
});