"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _Button = _interopRequireDefault(require("../../Button"));
var _Header = _interopRequireDefault(require("../../Header"));
var _Panel = _interopRequireDefault(require("../Panel"));
var _Panels = _interopRequireWildcard(require("../Panels"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var keyDown = function keyDown(keyCode) {
  return function (elm) {
    return _react.fireEvent.keyDown(elm, {
      keyCode: keyCode
    });
  };
};
var keyUp = function keyUp(keyCode) {
  return function (elm) {
    return _react.fireEvent.keyUp(elm, {
      keyCode: keyCode
    });
  };
};
var escapeKeyUp = keyUp(27);
var escapeKeyDown = keyDown(27);
describe('Panels Specs', function () {
  test('should display the correct panel based on \'index\' prop', function () {
    var firstButton, secondButton;
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
        index: 0,
        noAnimation: true,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
            children: "One"
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
            children: "Two"
          })
        })]
      })),
      rerender = _render.rerender;
    firstButton = _react.screen.queryByText('One');
    secondButton = _react.screen.queryByText('Two');
    expect(firstButton).toBeInTheDocument();
    expect(secondButton).not.toBeInTheDocument();
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
      index: 1,
      noAnimation: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          children: "One"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          children: "Two"
        })
      })]
    }));
    firstButton = _react.screen.queryByText('One');
    secondButton = _react.screen.queryByText('Two');
    expect(firstButton).not.toBeInTheDocument();
    expect(secondButton).toBeInTheDocument();
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
      index: 0,
      noAnimation: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          children: "One"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          children: "Two"
        })
      })]
    }));
    firstButton = _react.screen.queryByText('One');
    secondButton = _react.screen.queryByText('Two');
    expect(firstButton).toBeInTheDocument();
    expect(secondButton).not.toBeInTheDocument();
  });
  test('should set {autoFocus} on child to \'default-element\' on first render', function () {
    var DivPanel = function DivPanel(_ref) {
      var autoFocus = _ref.autoFocus,
        id = _ref.id;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        "data-testid": "panel",
        id: id,
        children: autoFocus
      });
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
      index: 0,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {})
    }));
    var expected = 'default-element';
    var actual = _react.screen.getByTestId('panel').textContent;
    expect(actual).toBe(expected);
  });
  test('should set {autoFocus} on child to \'default-element\' when navigating to a higher index', function () {
    var DivPanel = function DivPanel(_ref2) {
      var autoFocus = _ref2.autoFocus,
        id = _ref2.id;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        "data-testid": "panel",
        id: id,
        children: autoFocus
      });
    };
    var _render2 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
        index: 0,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {
          id: "p2"
        })]
      })),
      rerender = _render2.rerender;
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
      index: 1,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {
        id: "p2"
      })]
    }));
    var expected = 'default-element';
    var actual = _react.screen.getAllByTestId('panel')[0].textContent;
    expect(actual).toBe(expected);
  });
  test('should not set {autoFocus} on child when navigating to a higher index when it has an autoFocus prop set', function () {
    var DivPanel = function DivPanel(_ref3) {
      var autoFocus = _ref3.autoFocus,
        id = _ref3.id;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        "data-testid": "panel",
        id: id,
        children: autoFocus
      });
    };
    var _render3 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
        index: 0,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {
          autoFocus: "last-focused",
          id: "p2"
        })]
      })),
      rerender = _render3.rerender;
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
      index: 1,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(DivPanel, {
        autoFocus: "last-focused",
        id: "p2"
      })]
    }));
    var expected = 'last-focused';
    var panel = _react.screen.getAllByTestId('panel')[0];
    expect(panel.textContent).toBe(expected);
    expect(panel.id).toBe('p2');
  });
  test('should set custom {autoFocus} on child panels', function () {
    var AutoFocusPanel = function AutoFocusPanel(_ref4) {
      var autoFocus = _ref4.autoFocus,
        id = _ref4.id;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        autoFocus: autoFocus,
        "data-testid": "panel",
        id: id,
        children: autoFocus
      });
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
      index: 0,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(AutoFocusPanel, {
        autoFocus: "focus-here",
        id: "p2"
      })
    }));
    var expected = 'focus-here';
    var panel = _react.screen.getByTestId('panel');
    expect(panel.textContent).toBe(expected);
    expect(panel.id).toBe('p2');
  });
  test('should call \'onBack\' for \'escape\' key if not on first panel', function () {
    var spy = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels["default"], {
      "data-testid": "panels",
      index: 1,
      onBack: spy,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: "First"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        children: "Second"
      })]
    }));
    var panels = _react.screen.getByTestId('panels');
    escapeKeyDown(panels);
    escapeKeyUp(panels);
    expect(spy).toHaveBeenCalled();
  });
  test('should not call \'onScroll\' for a scroll event applied to a child', function () {
    var spy = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
      index: 0,
      onScroll: spy,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
        "data-testid": "panel",
        children: "First"
      })
    }));
    var panel = _react.screen.getByTestId('panel');
    _react.fireEvent.scroll(panel, {
      target: {
        scrollY: 100
      }
    });
    expect(spy).not.toHaveBeenCalled();
  });
  describe('with Panel and Header', function () {
    test('should render close button', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
        index: 0,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Header["default"], {
            title: "Panel Title"
          })
        })
      }));
      var closeButton = _react.screen.getByRole('button');
      expect(closeButton).toBeInTheDocument();
    });
    test('should not render close button when \'noCloseButton\' is set to true', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
        index: 0,
        noCloseButton: true,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Header["default"], {
            title: "Panel Title"
          })
        })
      }));
      var closeButton = _react.screen.queryByRole('button');
      expect(closeButton).toBeNull();
    });
    test('should set close button \'aria-label\' to closeButtonAriaLabel', function () {
      var label = 'custom close button label';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
        closeButtonAriaLabel: label,
        index: 0,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Header["default"], {
            title: "Panel Title"
          })
        })
      }));
      var expected = label;
      var actual = _react.screen.getByRole('button');
      expect(actual).toHaveAttribute('aria-label', expected);
    });
    test('should insert additional Panels-level buttons into the global-navigation area when declaring controls prop', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels["default"], {
        controls: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          "data-testid": "controlButtonTest",
          children: "Control button"
        }),
        index: 0,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Panel["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Header["default"], {
            title: "Panel Title"
          })
        })
      }));
      var controlButton = _react.screen.getByTestId('controlButtonTest');
      expect(controlButton).toBeInTheDocument();
    });
  });
  describe('childProps', function () {
    test('should not add aria-owns when noCloseButton is true and no controls', function () {
      var id = 'id';
      var childProps = {};
      var props = {
        childProps: childProps,
        noCloseButton: true,
        id: id
      };
      var expected = childProps;
      var actual = _Panels.PanelsBase.computed.childProps(props);
      expect(actual).toBe(expected);
    });
    test('should not add aria-owns when id is not set', function () {
      var childProps = {};
      var props = {
        childProps: childProps,
        noCloseButton: false
      };
      var expected = childProps;
      var actual = _Panels.PanelsBase.computed.childProps(props);
      expect(actual).toBe(expected);
    });
    test('should add aria-owns', function () {
      var id = 'id';
      var childProps = {};
      var props = {
        childProps: childProps,
        noCloseButton: false,
        id: id
      };
      var expected = "".concat(id, "-controls");
      var actual = _Panels.PanelsBase.computed.childProps(props)['aria-owns'];
      expect(actual).toBe(expected);
    });
    test('should append aria-owns', function () {
      var id = 'id';
      var ariaOwns = ':allthethings:';
      var childProps = {
        'aria-owns': ariaOwns
      };
      var props = {
        childProps: childProps,
        noCloseButton: false,
        id: id
      };
      var expected = "".concat(ariaOwns, " ").concat(id, "-controls");
      var actual = _Panels.PanelsBase.computed.childProps(props)['aria-owns'];
      expect(actual).toBe(expected);
    });
    test('should append aria-owns with noCloseButton and controls', function () {
      var id = 'id';
      var ariaOwns = ':allthethings:';
      var childProps = {
        'aria-owns': ariaOwns
      };
      var props = {
        childProps: childProps,
        controls: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Hello"
        }),
        noCloseButton: true,
        id: id
      };
      var expected = "".concat(ariaOwns, " ").concat(id, "-controls");
      var actual = _Panels.PanelsBase.computed.childProps(props)['aria-owns'];
      expect(actual).toBe(expected);
    });
  });
});