"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
var _IncrementSlider = _interopRequireDefault(require("../IncrementSlider"));
var _jsxRuntime = require("react/jsx-runtime");
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); }); }; }
var tap = function tap(node) {
  _react.fireEvent.mouseDown(node);
  _react.fireEvent.mouseUp(node);
};
var focus = function focus(slider) {
  return _react.fireEvent.focus(slider);
};
var keyDown = function keyDown(keyCode) {
  return function (slider) {
    return _react.fireEvent.keyDown(slider, {
      keyCode: keyCode
    });
  };
};
var downKeyDown = keyDown(40);
var leftKeyDown = keyDown(37);
var rightKeyDown = keyDown(39);
var upKeyDown = keyDown(38);
describe('IncrementSlider Specs', function () {
  test('should decrement value', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 50;
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      onChange: handleChange,
      value: value
    }));
    yield user.click(_react.screen.getAllByRole('button')[0]);
    var expected = value - 1;
    var actual = handleChange.mock.calls[0][0].value;
    expect(actual).toBe(expected);
  }));
  test('should increment value', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 50;
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      onChange: handleChange,
      value: value
    }));
    yield user.click(_react.screen.getAllByRole('button')[1]);
    var expected = value + 1;
    var actual = handleChange.mock.calls[0][0].value;
    expect(actual).toBe(expected);
  }));
  test('should increment value by knobStep', /*#__PURE__*/_asyncToGenerator(function* () {
    var spy = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      active: true,
      defaultValue: 50,
      knobStep: 2,
      onChange: spy
    }));
    yield user.click(_react.screen.getAllByRole('button')[1]);
    var expected = 52;
    var actual = spy.mock.calls[0][0].value;
    expect(actual).toBe(expected);
  }));
  test('should only call onChange once', function () {
    var handleChange = jest.fn();
    var value = 50;
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      onChange: handleChange,
      value: value
    }));
    var incrementButton = _react.screen.getAllByRole('button')[1];
    tap(incrementButton);
    var expected = 1;
    expect(handleChange).toHaveBeenCalledTimes(expected);
  });
  test('should not call onChange on prop change', function () {
    var handleChange = jest.fn();
    var value = 50;
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onChange: handleChange,
        value: value
      })),
      rerender = _render.rerender;
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      onChange: handleChange,
      value: value + 1
    }));
    expect(handleChange).not.toHaveBeenCalled();
  });
  test('should disable decrement button when value === min', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      min: 0,
      value: 0
    }));
    var expected = 'disabled';
    var actual = _react.screen.getAllByRole('button')[0];
    expect(actual).toHaveAttribute(expected);
  });
  test('should disable increment button when value === max', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      max: 10,
      value: 10
    }));
    var expected = 'disabled';
    var actual = _react.screen.getAllByRole('button')[1];
    expect(actual).toHaveAttribute(expected);
  });
  test('should use custom incrementIcon', function () {
    var icon = 'plus';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      incrementIcon: icon
    }));
    var expected = '+';
    var actual = _react.screen.getAllByRole('button')[1];
    expect(actual).toHaveTextContent(expected);
  });
  test('should use custom decrementIcon', function () {
    var icon = 'minus';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      decrementIcon: icon
    }));
    var expected = '-';
    var actual = _react.screen.getAllByRole('button')[0];
    expect(actual).toHaveTextContent(expected);
  });
  test('should set decrementButton `aria-label` to value and hint string', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      value: 10
    }));
    var expected = '10 press button to decrease the value';
    var actual = _react.screen.getAllByRole('button')[0];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set decrementButton `aria-label` to decrementAriaLabel', function () {
    var label = 'decrement aria label';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      decrementAriaLabel: label,
      value: 10
    }));
    var expected = "10 ".concat(label);
    var actual = _react.screen.getAllByRole('button')[0];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set decrementButton `aria-label` when decrementButton is disabled', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      disabled: true,
      value: 10
    }));
    var expected = '10 press button to decrease the value';
    var actual = _react.screen.getAllByRole('button')[0];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set incrementButton `aria-label` to value and hint string', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      value: 10
    }));
    var expected = '10 press button to increase the value';
    var actual = _react.screen.getAllByRole('button')[1];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set incrementButton `aria-label` to incrementAriaLabel', function () {
    var label = 'increment aria label';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      incrementAriaLabel: label,
      value: 10
    }));
    var expected = "10 ".concat(label);
    var actual = _react.screen.getAllByRole('button')[1];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set incrementButton `aria-label` when incrementButton is disabled', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      disabled: true,
      value: 10
    }));
    var expected = '10 press button to increase the value';
    var actual = _react.screen.getAllByRole('button')[1];
    expect(actual).toHaveAttribute('aria-label', expected);
  });
  test('should set the tooltip to visible when focused', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      tooltip: true
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    var actual = _react.screen.getByText('0');
    var expected = 'tooltipLabel';
    expect(actual).toHaveClass(expected);
  });
  test('should set the tooltip to not visible when unfocused', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
      tooltip: true
    }));
    var tooltip = _react.screen.queryByText('0');
    expect(tooltip).toBeNull();
  });
  describe('Directional events from IncrementSliderButtons', function () {
    test('should call onSpotlightLeft from the decrement button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightLeft: handleSpotlight
      }));
      leftKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightLeft from the decrement button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightLeft: handleSpotlight,
        orientation: "vertical"
      }));
      leftKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightLeft from the increment button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightLeft: handleSpotlight,
        orientation: "vertical"
      }));
      leftKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightRight from the increment button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightRight: handleSpotlight
      }));
      rightKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightRight from the increment button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightRight: handleSpotlight,
        orientation: "vertical"
      }));
      rightKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightRight from the decrement button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightRight: handleSpotlight,
        orientation: "vertical"
      }));
      rightKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightUp from the decrement button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightUp: handleSpotlight
      }));
      upKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightUp from the increment button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightUp: handleSpotlight
      }));
      upKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightUp from the increment button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightUp: handleSpotlight,
        orientation: "vertical"
      }));
      upKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightDown from the increment button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightDown: handleSpotlight
      }));
      downKeyDown(_react.screen.getAllByRole('button')[1]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightDown from the decrement button of horizontal IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightDown: handleSpotlight,
        orientation: "vertical"
      }));
      downKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightDown from the decrement button of vertical IncrementSlider', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        onSpotlightDown: handleSpotlight,
        orientation: "vertical"
      }));
      downKeyDown(_react.screen.getAllByRole('button')[0]);
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
  });
  describe('Directional events at bounds of slider', function () {
    test('should call onSpotlightLeft from slider of horizontal IncrementSlider when value is at min', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        min: 0,
        onSpotlightLeft: handleSpotlight,
        value: 0
      }));
      leftKeyDown(_react.screen.getByRole('slider'));
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightRight from slider of horizontal IncrementSlider when value is at max', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        max: 100,
        onSpotlightRight: handleSpotlight,
        value: 100
      }));
      rightKeyDown(_react.screen.getByRole('slider'));
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightDown from slider of vertical IncrementSlider when value is at min', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        min: 0,
        onSpotlightDown: handleSpotlight,
        orientation: "vertical",
        value: 0
      }));
      downKeyDown(_react.screen.getByRole('slider'));
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
    test('should call onSpotlightUp from slider of vertical IncrementSlider when value is at max', function () {
      var handleSpotlight = jest.fn();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_IncrementSlider["default"], {
        max: 100,
        onSpotlightUp: handleSpotlight,
        orientation: "vertical",
        value: 100
      }));
      upKeyDown(_react.screen.getByRole('slider'));
      var expected = 1;
      expect(handleSpotlight).toBeCalledTimes(expected);
    });
  });
});