"use strict";

var _jsxRuntime = require("react/jsx-runtime");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _kind = _interopRequireDefault(require("@enact/core/kind"));

var _enzyme = require("enzyme");

var _BreadcrumbDecorator = _interopRequireDefault(require("../BreadcrumbDecorator"));

var _Panels = _interopRequireDefault(require("../Panels"));

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

/* eslint-disable react/jsx-no-bind */
describe('BreadcrumbDecorator', function () {
  var CustomBreadcrumb = (0, _kind["default"])({
    name: 'CustomBreadcrumb',
    propsTypes: {
      index: _propTypes["default"].number,
      onSelect: _propTypes["default"].func
    },
    render: function render(_ref) {
      var index = _ref.index,
          onSelect = _ref.onSelect;

      // eslint-disable-line enact/prop-types
      var handleSelect = function handleSelect() {
        return onSelect({
          index: index
        });
      };

      return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        onClick: handleSelect,
        children: index
      });
    }
  });

  var Panel = function Panel() {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {});
  };

  test('should wrap primitive breadcrumbs with Breadcrumb', function () {
    var SingleBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 1
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(SingleBreadcrumbPanels, {
      index: 2,
      breadcrumbs: ['1st', '2nd', '3rd'],
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    }));
    var expected = '2nd';
    var actual = subject.find('Breadcrumb').text();
    expect(actual).toBe(expected);
  });
  test('should support custom breadcrumbs', function () {
    var SingleBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 1
    }, _Panels["default"]);
    var breadcrumbs = [0, 1, 2].map(function (i) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomBreadcrumb, {
        index: i
      });
    });
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(SingleBreadcrumbPanels, {
      index: 2,
      breadcrumbs: breadcrumbs,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    }));
    var expected = 1;
    var actual = subject.find('CustomBreadcrumb').length;
    expect(actual).toBe(expected);
  });
  test('should generate {config.max} breadcrumbs', function () {
    var ThreeBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 3
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ThreeBreadcrumbPanels, {
      index: 3,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    }));
    var expected = 3;
    var actual = subject.find('Breadcrumb').length;
    expect(actual).toBe(expected);
  });
  test('should add {config.className} to the root node', function () {
    var className = 'root-node';
    var StyledBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      className: className
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledBreadcrumbPanels, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})
    }));
    var expected = true;
    var actual = subject.find('div').first().hasClass(className);
    expect(actual).toBe(expected);
  });
  test('should not set aria-owns when no breadcrumbs are needed', function () {
    var ThreeBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 3
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ThreeBreadcrumbPanels, {
      id: "test",
      index: 0,
      noCloseButton: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    })); // eslint-disable-next-line

    var expected = undefined;
    var actual = subject.find(Panel).first().prop('aria-owns');
    expect(actual).toBe(expected);
  });
  test('should set aria-owns on each Panel for the breadcrumbs', function () {
    var ThreeBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 3
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ThreeBreadcrumbPanels, {
      id: "test",
      index: 3,
      noCloseButton: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    })); // tests for {config.max} aria-owns entries in the format ${id}_bc_{$index}

    var expected = [0, 1, 2].map(function (n) {
      return "test_bc_".concat(n);
    }).join(' ');
    var actual = subject.find(Panel).first().prop('aria-owns');
    expect(actual).toBe(expected);
  });
  test('should set aria-owns on each Panel for the `max` breadcrumbs', function () {
    var ThreeBreadcrumbPanels = (0, _BreadcrumbDecorator["default"])({
      max: 1
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(ThreeBreadcrumbPanels, {
      id: "test",
      index: 3,
      noCloseButton: true,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    })); // tests for truncated {config.max} aria-owns entries in the format ${id}_bc_{$index}

    var expected = 'test_bc_2';
    var actual = subject.find(Panel).first().prop('aria-owns');
    expect(actual).toBe(expected);
  });
  test('should append breadcrumb aria-owns to set aria-owns value in childProps', function () {
    var Component = (0, _BreadcrumbDecorator["default"])({
      max: 1
    }, _Panels["default"]);
    var subject = (0, _enzyme.mount)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(Component, {
      id: "test",
      noCloseButton: true,
      index: 1,
      childProps: {
        'aria-owns': ':allthethings:'
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Panel, {})]
    }));
    var expected = ':allthethings: test_bc_0';
    var actual = subject.find(Panel).first().prop('aria-owns');
    expect(actual).toBe(expected);
  });
});