import createBlink from '../main';
import { VERSION } from '../config/config';
import jest from 'jest-mock';

describe('Video start integration test', () => {
  it('should send a video play event for a single start and stop', () => {
    const mockSend = jest.fn();

    const blink = createBlink({
      send: mockSend,
      sendDirect: mockSend,
    });

    // First send of a pageInit event, to set the general state
    blink.pageInit({
      url: 'http://some.site',
      pageView: 'the-pageview-id',
      referrer: 'www.sol.no',
      site: 'www.kk.no',
    });

    blink.videoPlay({
      videoId: '123',
      playerId: 'test-player-id',
      time: new Date(1),
      position: 4,
      volume: 100,
      reason: 'autostart',
      muted: true,
    });

    blink.playerShown({
      playerId: 'test-player-id',
      time: new Date(2),
      position: 4,
      volume: 100,
      reason: 'viewable',
      muted: true,
    });

    blink.videoStop({
      videoId: '123',
      playerId: 'test-player-id',
      time: new Date(10),
      reason: 'pause',
      position: 10,
      volume: 50,
      muted: true,
    });

    expect(mockSend.mock.calls[1][0]).toEqual([
      {
        // generic fields
        id: '123',
        videoId: '123',
        type: 'videoWatch',
        pageView: 'the-pageview-id',
        referrer: 'www.sol.no',
        site: 'www.kk.no',
        version: VERSION,
        videoPlayPosition: 4,
        videoStopPosition: 10,
        videoPlayVolume: 100,
        videoStopVolume: 50,
        videoPlayReason: 'viewable',
        videoStopReason: 'pause',
        videoPlayMuted: true,
        videoStopMuted: true,
        activeTime: 8,
        videoSticky: false,
      },
    ]);
  });

  it('should send event when the the video gets hidden', () => {
    const mockSend = jest.fn();

    const blink = createBlink({
      send: mockSend,
      sendDirect: mockSend,
    });

    // First send of a pageInit event, to set the general state
    blink.pageInit({
      url: 'http://some.site',
      pageView: 'the-pageview-id',
      referrer: 'www.sol.no',
      site: 'www.kk.no',
    });

    blink.videoPlay({
      videoId: '123',
      playerId: 'test-player-id',
      time: new Date(1),
      position: 4,
      volume: 100,
      reason: 'autostart',
      muted: true,
    });

    blink.playerShown({
      playerId: 'test-player-id',
      time: new Date(2),
      position: 4,
      volume: 100,
      reason: 'viewable',
      muted: true,
    });

    blink.playerHidden({
      playerId: 'test-player-id',
      time: new Date(10),
      reason: 'tabhide',
      position: 10,
      volume: 50,
      muted: false,
    });

    expect(mockSend.mock.calls[1][0]).toEqual([
      {
        // generic fields
        id: '123',
        videoId: '123',
        type: 'videoWatch',
        pageView: 'the-pageview-id',
        referrer: 'www.sol.no',
        site: 'www.kk.no',
        version: VERSION,
        videoPlayPosition: 4,
        videoStopPosition: 10,
        videoPlayMuted: true,
        videoStopMuted: false,
        videoPlayVolume: 100,
        videoStopVolume: 50,
        videoPlayReason: 'viewable',
        videoStopReason: 'tabhide',
        activeTime: 8,
        videoSticky: false,
      },
    ]);
  });

  it('should send events when user watches consecutive videos', () => {
    const mockSend = jest.fn();

    const blink = createBlink({
      send: mockSend,
      sendDirect: mockSend,
    });

    // First send of a pageInit event, to set the general state
    blink.pageInit({
      url: 'http://some.site',
      pageView: 'the-pageview-id',
      referrer: 'www.sol.no',
      site: 'www.kk.no',
    });

    blink.videoPlay({
      videoId: '123',
      playerId: 'test-player-id',
      time: new Date(1),
      position: 4,
      volume: 100,
      reason: 'autostart',
      muted: true,
    });

    blink.playerShown({
      playerId: 'test-player-id',
      time: new Date(2),
      position: 4,
      volume: 100,
      reason: 'viewable',
      muted: true,
    });

    blink.videoStop({
      videoId: '123',
      playerId: 'test-player-id',
      time: new Date(10),
      reason: 'complete',
      position: 10,
      volume: 100,
      muted: true,
    });

    blink.videoPlay({
      videoId: '321',
      playerId: 'test-player-id',
      time: new Date(1),
      reason: 'related-auto',
      position: 4,
      volume: 100,
      muted: true,
    });

    blink.videoStop({
      videoId: '321',
      playerId: 'test-player-id',
      time: new Date(10),
      reason: 'complete',
      position: 10,
      volume: 100,
      muted: true,
    });

    expect(mockSend.mock.calls[1][0]).toEqual([
      {
        // generic fields
        id: '123',
        videoId: '123',
        type: 'videoWatch',
        pageView: 'the-pageview-id',
        referrer: 'www.sol.no',
        site: 'www.kk.no',
        version: VERSION,
        videoPlayPosition: 4,
        videoStopPosition: 10,
        videoPlayMuted: true,
        videoStopMuted: true,
        videoPlayVolume: 100,
        videoStopVolume: 100,
        videoPlayReason: 'viewable',
        videoStopReason: 'complete',
        activeTime: 8,
        videoSticky: false,
      },
    ]);

    expect(mockSend.mock.calls[3][0]).toEqual([
      {
        // generic fields
        id: '321',
        videoId: '321',
        type: 'videoWatch',
        pageView: 'the-pageview-id',
        referrer: 'www.sol.no',
        site: 'www.kk.no',
        version: VERSION,
        videoPlayPosition: 4,
        videoStopPosition: 10,
        videoPlayMuted: true,
        videoStopMuted: true,
        videoPlayVolume: 100,
        videoStopVolume: 100,
        videoPlayReason: 'viewable',
        videoStopReason: 'complete',
        activeTime: 8,
        videoSticky: false,
      },
    ]);
  });
});
