message: feat(videoplayerAPI): adding VideoPlayerAPI test coverage
diff: @@ -20,16 +20,68 @@ function _kWidgetMock() {
  };
  }
  },
  + embed: obj => {
  + obj.readyCallback(_kdpId);
  + },
  };
  }
  +/**
  + * Keeps track of the ID for the generated player element
  + * @type {string}
  + * @private
  + */
  +const _kdpId = '12345';
  +
  +/**
  + * Unmocks the kWidget
  + *
  + * @private
  + */
  +function _KWidgetUnMock() {
  + delete root.kWidget;
  +}
  +
  jest.mock('../../Analytics', () => ({
  AnalyticsAPI: {
  videoPlayerStats: jest.fn(),
  },
  }));
  +/**
  + * Prototypes the addJsListener method to the kdp
  + *
  + * @private
  + */
  +function _mockKdp() {
  + document.body.innerHTML = `<div id="${_kdpId}"></div>`;
  + Element.prototype.addJsListener = jest.fn((eventName, cb) => {
  + _jsListenerEvents.push({
  + eventName,
  + cb,
  + });
  + });
  +}
  +
  +/**
  + * Tracks the addJsListeners added
  + *
  + * @type {Array}
  + * @private
  + */
  +let _jsListenerEvents = [];
  +
  describe('VideoPlayerAPI', () => {
  + beforeEach(() => {
  + jest.useFakeTimers();
  + _jsListenerEvents = [];
  + });
  +
  + afterEach(() => {
  + _KWidgetUnMock();
  + jest.useRealTimers();
  + });
  +
  it('should return the api response', async () => {
  _kWidgetMock();
  const apiResponse = await VideoPlayerAPI.api('123456');
  @@ -37,6 +89,20 @@ describe('VideoPlayerAPI', () => {
  expect(apiResponse).toEqual(response);
  });
  + it('should check if kWidget is available', async () => {
  + const spy = jest.fn();
  + VideoPlayerAPI.checkScript().then(spy);
  +
  + jest.advanceTimersByTime(200);
  + await Promise.resolve();
  +
  + _kWidgetMock();
  + jest.advanceTimersByTime(200);
  + await Promise.resolve();
  +
  + expect(spy).toHaveBeenCalled();
  + });
  +
  it('should execute the video metrics call', () => {
  const kdp = {
  evaluate: query => {
  @@ -58,6 +124,23 @@ describe('VideoPlayerAPI', () => {
  expect(AnalyticsAPI.videoPlayerStats).toHaveBeenCalled();
  });
  + it('should embed the video player', async () => {
  + const videoId = '123';
  + _kWidgetMock();
  + _mockKdp();
  + VideoPlayerAPI.embedVideo(videoId, '12345');
  + await Promise.resolve();
  + expect(
  + _jsListenerEvents.some(event => event.eventName === 'playerPaused')
  + ).toBe(true);
  + expect(
  + _jsListenerEvents.some(event => event.eventName === 'playerPlayed')
  + ).toBe(true);
  + expect(
  + _jsListenerEvents.some(event => event.eventName === 'playerPlayEnd')
  + ).toBe(true);
  + });
  +
  it('should return the video duration', async () => {
  const duration = 60000;
  const time = VideoPlayerAPI.getVideoDuration(duration);
  