"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 _MediaPlayer = _interopRequireDefault(require("../MediaPlayer"));
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 blur = function blur(slider) {
  return _react.fireEvent.blur(slider);
};
var activate = function activate(slider) {
  return _react.fireEvent.keyUp(slider, {
    keyCode: 13
  });
};

// https://github.com/jsdom/jsdom/issues/2155
// jsdom doesn't support any loading or playback media operations. As a workaround we use these stubs:
window.HTMLMediaElement.prototype.load = function () {};
window.HTMLMediaElement.prototype.play = function () {};
window.HTMLMediaElement.prototype.pause = function () {};
describe('MediaPlayer', function () {
  var audioFiles = ['https://sampleswap.org/mp3/artist/254731/BossPlayer_Your-Right-Here-160.mp3', 'https://sampleswap.org/mp3/artist/78152/HiatusManJBanner_Show-Stopper-160.mp3', 'https://sampleswap.org/mp3/artist/47067/DJ-Masque_Oceanic-Dawn-160.mp3', 'https://sampleswap.org/mp3/artist/26546/benzoul_lovevoodoo-160.mp3', 'https://sampleswap.org/mp3/artist/19139/MarkNine_In-my-Place-160.mp3', 'https://sampleswap.org/mp3/artist/47067/DJ-Masque_Dont-Forget-To-Be-Yourself-160.mp3'];
  describe('changing sources', function () {
    test('should use the same node when changing the `source`', function () {
      var handleUpdate = jest.fn();
      // `onUpdate` added for code coverage purposes
      var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
          "data-testid": "media-player",
          onUpdate: handleUpdate,
          children: audioFiles.map(function (audioFile, index) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
              src: audioFile,
              type: "audio/mp3"
            }, index);
          })
        })),
        rerender = _render.rerender;
      var expected = _react.screen.getByTestId('media-player');
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onUpdate: handleUpdate,
        source: "abc.mp3"
      }));
      var actual = _react.screen.getByTestId('media-player');
      expect(actual).toBe(expected);
    });
    test('should use same `source` when removing `source`', function () {
      var _render2 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
          "data-testid": "media-player",
          children: audioFiles.map(function (audioFile, index) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
              src: audioFile,
              type: "audio/mp3"
            }, index);
          })
        })),
        rerender = _render2.rerender;
      var expected = _react.screen.getByTestId('media-player');
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        source: "no source"
      }));
      var actual = _react.screen.getByTestId('media-player');
      expect(actual).toBe(expected);
    });
    test('should reuse source node over two changes', function () {
      var _render3 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
          "data-testid": "media-player",
          children: audioFiles.map(function (audioFile, index) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
              src: audioFile,
              type: "audio/mp3"
            }, index);
          })
        })),
        rerender = _render3.rerender;
      var expected = _react.screen.getByTestId('media-player');
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        source: "abc.mp3"
      }));
      var actual = _react.screen.getByTestId('media-player');
      expect(actual).toBe(expected);
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        source: "def.mp3"
      }));
      var last = _react.screen.getByTestId('media-player');
      expect(last).toBe(expected);
    });
    test('should activate the mediaPlayer slider on enter keyup', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaSlider = _react.screen.getByRole('slider', {
        hidden: true
      });
      activate(mediaSlider);
      expect(mediaSlider).toHaveClass('active');
    });
    test('should deactivate the mediaPlayer slider on blur', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaSlider = _react.screen.getByRole('slider', {
        hidden: true
      });
      activate(mediaSlider);
      blur(mediaSlider);
      expect(mediaSlider).not.toHaveClass('active');
    });
  });
  describe('methods and props', function () {
    test('should have `audio` media', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var audioMedia = _react.screen.getByTestId('media-player').children.item(0);
      var expected = 'AUDIO';
      var actual = audioMedia.tagName;
      expect(actual).toBe(expected);
    });
    test('should play next audio media when `Next` button is clicked', /*#__PURE__*/_asyncToGenerator(function* () {
      var handleNext = jest.fn();
      var user = _userEvent["default"].setup();
      // onNext added for code coverage purposes
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onNext: handleNext,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var nextButton = _react.screen.getByLabelText('Next');
      yield user.click(nextButton);
      var audioSource = _react.screen.getByTestId('media-player').children.item(0).children.item(0);
      var expected = 'https://sampleswap.org/mp3/artist/78152/HiatusManJBanner_Show-Stopper-160.mp3'; // second mp3 file from audioFiles
      expect(audioSource).toHaveProperty('src', expected);
    }));
    test('should call `onPlay` when `Play` button is clicked', /*#__PURE__*/_asyncToGenerator(function* () {
      var handlePlay = jest.fn();
      var user = _userEvent["default"].setup();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        onPlay: handlePlay,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var playButton = _react.screen.getByLabelText('Play');
      yield user.click(playButton);
      yield (0, _react.waitFor)(function () {
        expect(handlePlay).toHaveBeenCalled();
      });
    }));
    test('should play previous audio media when `Previous` button is clicked', /*#__PURE__*/_asyncToGenerator(function* () {
      var handlePrevious = jest.fn();
      var user = _userEvent["default"].setup();
      // `onPrevious` added for code coverage purposes
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onPrevious: handlePrevious,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var nextButton = _react.screen.getByLabelText('Next');
      var previousButton = _react.screen.getByLabelText('Previous');
      yield user.click(nextButton); // go to next audio media - audioFiles[1]
      yield user.click(previousButton); // go back to first audio media - audioFiles[0]

      var audioSource = _react.screen.getByTestId('media-player').children.item(0).children.item(0);
      var expected = 'https://sampleswap.org/mp3/artist/254731/BossPlayer_Your-Right-Here-160.mp3';
      expect(audioSource).toHaveProperty('src', expected);
    }));
    test('should play last audio media in the playlist when `Previous` button is clicked and first media in playlist is playing and `repeat=all`', /*#__PURE__*/_asyncToGenerator(function* () {
      var handlePrevious = jest.fn();
      var user = _userEvent["default"].setup();
      // `onPrevious` added for code coverage purposes
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onPrevious: handlePrevious,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var previousButton = _react.screen.getByLabelText('Previous');
      var repeatButton = _react.screen.getByLabelText('Repeat');
      yield user.click(repeatButton);
      yield user.click(repeatButton);
      yield user.click(previousButton); // go back to last audio media - audioFiles[5]

      var audioSource = _react.screen.getByTestId('media-player').children.item(0).children.item(0);
      var expected = 'https://sampleswap.org/mp3/artist/47067/DJ-Masque_Dont-Forget-To-Be-Yourself-160.mp3';
      expect(audioSource).toHaveProperty('src', expected);
    }));
    test('should restart the playlist when `Next` button is clicked and last media in playlist is playing and `repeat=all`', /*#__PURE__*/_asyncToGenerator(function* () {
      var handleNext = jest.fn();
      var user = _userEvent["default"].setup();
      // `onNext` added for code coverage purposes
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onNext: handleNext,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var nextButton = _react.screen.getByLabelText('Next');
      var repeatButton = _react.screen.getByLabelText('Repeat');
      yield user.click(repeatButton);
      yield user.click(repeatButton);
      yield user.click(nextButton);
      yield user.click(nextButton);
      yield user.click(nextButton);
      yield user.click(nextButton);
      yield user.click(nextButton);
      yield user.click(nextButton); // go back to first audio media - audioFiles[0]

      var audioSource = _react.screen.getByTestId('media-player').children.item(0).children.item(0);
      var expected = 'https://sampleswap.org/mp3/artist/254731/BossPlayer_Your-Right-Here-160.mp3';
      expect(audioSource).toHaveProperty('src', expected);
    }));
    test('should play audio media in order when `Shuffle` button is clicked twice', /*#__PURE__*/_asyncToGenerator(function* () {
      var handleShuffle = jest.fn();
      var user = _userEvent["default"].setup();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onShuffle: handleShuffle,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var nextButton = _react.screen.getByLabelText('Next');
      var playButton = _react.screen.getByLabelText('Play');
      var shuffleButton = _react.screen.getByLabelText('Shuffle');
      yield user.click(shuffleButton);
      yield user.click(shuffleButton);
      yield user.click(playButton);
      yield user.click(nextButton);
      var audioSource = _react.screen.getByTestId('media-player').children.item(0).children.item(0);
      var expected = 'https://sampleswap.org/mp3/artist/78152/HiatusManJBanner_Show-Stopper-160.mp3'; // second audio media from audioFiles

      expect(audioSource).toHaveProperty('src', expected);
    }));
    test('should update `repeat` value of controls frame to `one` when `repeat` button is clicked once', /*#__PURE__*/_asyncToGenerator(function* () {
      var handleRepeat = jest.fn();
      var user = _userEvent["default"].setup();
      // `onRepeat` added for code coverage purposes
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        onRepeat: handleRepeat,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var repeatButton = _react.screen.getByLabelText('Repeat');
      var controlsFrame = _react.screen.getByTestId('media-player').children.item(4);
      yield user.click(repeatButton);
      expect(controlsFrame).toHaveAttribute('repeat', 'one');
    }));
    test('should update `repeat` value of controls frame to `all` when `repeat` button is clicked twice', /*#__PURE__*/_asyncToGenerator(function* () {
      var user = _userEvent["default"].setup();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var repeatButton = _react.screen.getByLabelText('Repeat');
      var controlsFrame = _react.screen.getByTestId('media-player').children.item(4);
      yield user.click(repeatButton);
      yield user.click(repeatButton);
      expect(controlsFrame).toHaveAttribute('repeat', 'all');
    }));
    test('should update `repeat` value of controls frame to back to `none` when `repeat` button is clicked 3 times', /*#__PURE__*/_asyncToGenerator(function* () {
      var user = _userEvent["default"].setup();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var repeatButton = _react.screen.getByLabelText('Repeat');
      var controlsFrame = _react.screen.getByTestId('media-player').children.item(4);
      yield user.click(repeatButton);
      yield user.click(repeatButton);
      yield user.click(repeatButton);
      expect(controlsFrame).toHaveAttribute('repeat', 'none');
    }));
    test('should have `disabled` attribute when `disabled` is defined', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        disabled: true,
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaSlider = _react.screen.getByRole('slider', {
        hidden: true
      });
      var mediaButtons = _react.screen.getAllByRole('button');
      expect(mediaSlider).toHaveAttribute('disabled');
      mediaButtons.forEach(function (button) {
        expect(button).toHaveAttribute('disabled');
      });
    });
    test('should render `currentTime`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var currentTime = _react.screen.getByText('00:00');
      expect(currentTime).toBeInTheDocument();
    });
    test('should have `paused` class on render', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaSlider = _react.screen.getByRole('slider', {
        hidden: true
      });
      expect(mediaSlider).toHaveClass('paused');
    });
    test('should have `full` class name when `type` is not specified', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaPlayer = _react.screen.getByTestId('media-player');
      var expected = 'full';
      expect(mediaPlayer).toHaveClass(expected);
    });
    test('should have `tiny` class name when `type="tiny"`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        "data-testid": "media-player",
        type: "tiny",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var mediaPlayer = _react.screen.getByTestId('media-player');
      var expected = 'tiny';
      expect(mediaPlayer).toHaveClass(expected);
    });
    test('should change the format of the displayed time based on the current `locale`', function () {
      _ilib["default"].setLocale('si-LK');
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaPlayer["default"], {
        locale: "si-LK",
        children: audioFiles.map(function (audioFile, index) {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("source", {
            src: audioFile,
            type: "audio/mp3"
          }, index);
        })
      }));
      var currentTime = _react.screen.getByText('00.00');
      expect(currentTime).toBeInTheDocument();
    });
  });
});