"use strict";

var _ilib = _interopRequireDefault(require("ilib"));
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
var _TimePicker = _interopRequireWildcard(require("../TimePicker"));
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 }; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } // Note: Tests pass 'locale' because there's no I18nDecorator to provide a value via context and
// otherwise, nothing renders in the label.
describe('TimePicker', function () {
  // Suite-wide setup

  test('should emit an onChange event when changing the hour', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      onChange: handleChange,
      value: new Date(2000, 6, 15, 12, 30)
    }));
    yield user.click(_react.screen.getByLabelText('12 hour next item'));
    var expected = 1;
    expect(handleChange).toHaveBeenCalledTimes(expected);
  }));
  test('should emit an onChange event when changing the minute', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      onChange: handleChange,
      value: new Date(2000, 6, 15, 12, 30)
    }));
    yield user.click(_react.screen.getByLabelText('30 minute increase the value'));
    var expected = 1;
    expect(handleChange).toHaveBeenCalledTimes(expected);
  }));
  test('should emit an onChange event when changing the meridiem', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      onChange: handleChange,
      value: new Date(2000, 6, 15, 12, 30)
    }));
    yield user.click(_react.screen.getByLabelText('PM previous item'));
    var expected = 1;
    expect(handleChange).toHaveBeenCalledTimes(expected);
  }));
  test('should emit an onChange event when changing the meridiem on a 5 meridiem locale', /*#__PURE__*/_asyncToGenerator(function* () {
    _ilib["default"].setLocale('am-ET');
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "am-ET",
      onChange: handleChange,
      value: new Date(2000, 6, 15, 12, 30)
    }));
    yield user.click(_react.screen.getByLabelText('ከሰዓት previous item'));
    var expected = 1;
    expect(handleChange).toHaveBeenCalledTimes(expected);
  }));
  test('should accept a JavaScript Date for its value prop', function () {
    _ilib["default"].setLocale('en-US');
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      value: new Date(2000, 0, 1, 12, 30)
    }));
    var minute = _react.screen.getAllByRole('spinbutton')[1];
    var expectedMinute = '30';
    expect(minute).toHaveTextContent(expectedMinute);
  });
  test('should set "hourAriaLabel" to hour picker', function () {
    var label = 'custom hour aria-label';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      hourAriaLabel: label,
      locale: "en-US",
      value: new Date(2000, 0, 1, 12, 30)
    }));
    var hourPicker = _react.screen.getByLabelText(label);
    var expected = '12';
    expect(hourPicker).toHaveTextContent(expected);
  });
  test('should set "meridiemAriaLabel" to meridiem picker', function () {
    var label = 'custom meridiem aria-label';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      meridiemAriaLabel: label,
      value: new Date(2000, 0, 1, 12, 30)
    }));
    var meridiemPicker = _react.screen.getByLabelText(label);
    var expected = 'PM';
    expect(meridiemPicker).toHaveTextContent(expected);
  });
  test('should set "minuteAriaLabel" to minute picker', function () {
    var label = 'custom minute aria-label';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "en-US",
      minuteAriaLabel: label,
      value: new Date(2000, 0, 1, 12, 30)
    }));
    var minutePicker = _react.screen.getByLabelText(label);
    var expected = '30';
    expect(minutePicker).toHaveTextContent(expected);
  });
  test('should display the correct meridiem for cases of more than 2 meridiems', function () {
    _ilib["default"].setLocale('am-ET');
    var time = new Date(2000, 0, 1, 12, 30);
    var secondTime = new Date(2000, 0, 1, 11, 30);
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
        locale: "am-ET",
        value: time
      })),
      rerender = _render.rerender;
    var firstMeridiemDisplayed = _react.screen.queryByText('ከሰዓት');
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_TimePicker["default"], {
      locale: "am-ET",
      value: secondTime
    }));
    var secondMeridiemDisplayed = _react.screen.queryByText('ጥዋት');
    expect(firstMeridiemDisplayed).not.toBeNull();
    expect(secondMeridiemDisplayed).not.toBeNull();
  });
  describe('#timeToLocaleString', function () {
    test('method should convert time to a localized string', function () {
      _ilib["default"].setLocale('en-US');
      var date = new Date(2000, 0, 1);
      var expected = '12:00 AM';
      var actual = (0, _TimePicker.timeToLocaleString)(date);
      expect(actual).toBe(expected);
    });
    test('method should return `null` for an invalid time', function () {
      var time = (0, _TimePicker.timeToLocaleString)(null);
      expect(time).toBeNull();
    });
  });
});