"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _Slider = _interopRequireDefault(require("../Slider"));
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 activate = function activate(slider) {
  return _react.fireEvent.keyUp(slider, {
    keyCode: 13
  });
};
var blur = function blur(slider) {
  return _react.fireEvent.blur(slider);
};
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);
var getElementClientCenter = function getElementClientCenter(element) {
  var _element$getBoundingC = element.getBoundingClientRect(),
    left = _element$getBoundingC.left,
    top = _element$getBoundingC.top,
    width = _element$getBoundingC.width,
    height = _element$getBoundingC.height;
  return {
    x: left + width / 2,
    y: top + height / 2
  };
};
var drag = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator(function* (element, _ref) {
    var delta = _ref.delta,
      _ref$steps = _ref.steps,
      steps = _ref$steps === void 0 ? 1 : _ref$steps;
    var from = getElementClientCenter(element);
    var to = {
      x: from.x + delta.x,
      y: from.y + delta.y
    };
    var step = {
      x: (to.x - from.x) / steps,
      y: (to.y - from.y) / steps
    };
    var current = {
      clientX: from.x,
      clientY: from.y
    };
    _react.fireEvent.mouseEnter(element, current);
    _react.fireEvent.mouseOver(element, current);
    _react.fireEvent.mouseMove(element, current);
    _react.fireEvent.mouseDown(element, current);
    for (var i = 0; i < steps; i++) {
      current.clientX += step.x;
      current.clientY += step.y;
      (0, _react.act)(function () {
        return jest.advanceTimersByTime(1000 / steps);
      });
      _react.fireEvent.mouseMove(element, current);
    }
    _react.fireEvent.mouseUp(element, current);
  });
  return function drag(_x, _x2) {
    return _ref2.apply(this, arguments);
  };
}();
describe('Slider', function () {
  beforeEach(function () {
    jest.useFakeTimers();
  });
  afterEach(function () {
    jest.useRealTimers();
  });
  test('should set `aria-valuetext` to hint string when knob is active and vertical is false', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {}));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = 'change a value with left right button';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should set `aria-valuetext` to hint string when knob is active and vertical is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = 'change a value with up down button';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should set `aria-valuetext` to value when value is changed', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true,
      defaultValue: 10
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    rightKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '11';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should activate the slider on enter keyup', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    var expected = 'active';
    expect(slider).toHaveClass(expected);
  });
  test('should change value of slider on drag', /*#__PURE__*/_asyncToGenerator(function* () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    yield drag(slider, {
      delta: {
        x: 50,
        y: 0
      }
    });
    var expectedAttribute = 'aria-valuetext';
    var unexpectedValue = '50 change a value with left right button';
    expect(slider).not.toHaveAttribute(expectedAttribute, unexpectedValue);
  }));
  test('should deactivate the slider on blur', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true
    }));
    var slider = _react.screen.getByRole('slider');
    var notExpected = 'active';
    activate(slider);
    expect(slider).toHaveClass(notExpected);
    blur(slider);
    expect(slider).not.toHaveClass(notExpected);
  });
  test('should not activate the slider on enter when activateOnFocus', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    var notExpected = 'active';
    expect(slider).not.toHaveClass(notExpected);
  });
  test('should decrement the value of horizontal slider on key left when active', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    leftKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '49';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should decrement the value of horizontal slider on key left when activateOnFocus is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    leftKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '49';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should decrement the value of vertical slider on key down when active', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      defaultValue: 50,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    downKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '49';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should decrement the value of vertical slider on key down when activateOnFocus is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true,
      defaultValue: 50,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    downKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '49';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should increment the value of horizontal slider on key right when active', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    rightKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '51';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should increment the value of horizontal slider on key right when activateOnFocus is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    rightKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '51';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should increment the value of vertical slider on key up when active', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      defaultValue: 50,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    upKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '51';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should increment the value of vertical slider on key up when activateOnFocus is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      activateOnFocus: true,
      defaultValue: 50,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    upKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '51';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should increment value by `knobStep`', function () {
    var spy = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      knobStep: 2,
      onChange: spy,
      defaultValue: 50
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    rightKeyDown(slider);
    var expected = 52;
    var actual = spy.mock.calls[0][0].value;
    expect(actual).toBe(expected);
  });

  // these tests validate behavior relating to `value` defaulting to `min`
  test('should not emit onChange when decrementing at the lower bound when value is unset', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      max: 10,
      min: 0,
      onChange: handleChange
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    leftKeyDown(slider);
    expect(handleChange).not.toBeCalled();
  });
  test('should increment from the lower bound when value is unset', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      active: true,
      max: 10,
      min: 0,
      onChange: handleChange
    }));
    var slider = _react.screen.getByRole('slider');
    activate(slider);
    rightKeyDown(slider);
    var expectedAttribute = 'aria-valuetext';
    var expectedValue = '1';
    expect(slider).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should call onSpotlightLeft on horizontal slider at min value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 0,
      onSpotlightLeft: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    leftKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should call onSpotlightLeft on vertical slider at any value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 50,
      onSpotlightLeft: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    leftKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should not call onSpotlightLeft on horizontal slider at greater than min value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 1,
      onSpotlightLeft: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    leftKeyDown(slider);
    expect(handleSpotlight).not.toBeCalled();
  });
  test('should call onSpotlightDown on vertical slider at min value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 0,
      onSpotlightDown: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    downKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should call onSpotlightDown on horizontal slider at any value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 50,
      onSpotlightDown: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    downKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should not call onSpotlightDown on vertical slider at greater than min value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 1,
      onSpotlightDown: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    downKeyDown(slider);
    expect(handleSpotlight).not.toBeCalled();
  });
  test('should call onSpotlightRight on horizontal slider at max value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 100,
      onSpotlightRight: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    rightKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should call onSpotlightRight on vertical slider at any value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 50,
      onSpotlightRight: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    rightKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should not call onSpotlightRight on horizontal slider at less than max value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 99,
      onSpotlightRight: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    rightKeyDown(slider);
    expect(handleSpotlight).not.toBeCalled();
  });
  test('should call onSpotlightUp on vertical slider at max value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 100,
      max: 100,
      onSpotlightUp: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    upKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should call onSpotlightUp on horizontal slider at any value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 50,
      onSpotlightUp: handleSpotlight
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    upKeyDown(slider);
    var expected = 1;
    expect(handleSpotlight).toBeCalledTimes(expected);
  });
  test('should not call onSpotlightUp on vertical slider at less than max value', function () {
    var handleSpotlight = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["default"], {
      defaultValue: 99,
      onSpotlightUp: handleSpotlight,
      orientation: "vertical"
    }));
    var slider = _react.screen.getByRole('slider');
    focus(slider);
    upKeyDown(slider);
    expect(handleSpotlight).not.toBeCalled();
  });
  test('should set the tooltip to visible when focused', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Slider["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)(_Slider["default"], {
      tooltip: true
    }));
    var tooltip = _react.screen.queryByText('0');
    expect(tooltip).toBeNull();
  });
});