import {
  DFP_INSCREEN,
  DFP_LOADED,
  DFP_RENDERED,
  SCROLL_OFFSET_TOP,
  SCROLL_OFFSET_HEIGHT,
  DFP_ADVERTISER_ID,
  DFP_ADUNIT_PATH,
  DFP_CAMPAIGN_ID,
  DFP_CREATIVE_ID,
  DFP_LINE_ITEM_ID,
  DFP_NAME,
  SCROLL_POS_AD_LOAD,
  SCROLL_POS_INSCREEN_DFP,
  SCROLL_POS_SLOT_RENDER_ENDED,
  SCROLL_POS_SLOT_ONLOAD,
  DFP_PREBID_WINNING_BID,
  DFP_SIZE,
  DFP_SOURCE_AGNOSTIC_CREATIVE_ID,
  DFP_SOURCE_AGNOSTIC_LINE_ITEM_ID,
  DFP_BIDDER,
} from '../consts';
import prepareAdEvent from '../prepare-ad-event';
import { VERSION } from '../../config/config';
import { BlinkEvent } from '../../types';
import { Page } from '../../selectors/get-page-state';

describe('Setup Ad Event Body', () => {
  it('should format a typical event', () => {
    const id = 'ad-banner';

    const page: Page = {
      id: 'default-page',
      state: {
        general: {
          site: 'www.dagbladet.no',
          pageView: '32be2425-907a-4f46-be45-cea8b00f5d7c',
          referrer: 'www.sol.no',
          version: VERSION,
        },
        inscreen: {
          'db.no/123': [
            { type: 'start', time: new Date(2018, 0, 0, 0) },
            { type: 'stop', time: new Date(2018, 0, 0, 1) },
          ],
          'ad-banner': [
            { type: 'start', time: new Date(2018, 0, 0, 0, 0) },
            { type: 'stop', time: new Date(2018, 0, 0, 0, 4) },
          ],
        },
        inscreen0: {
          'ad-banner': [
            { type: 'start', time: new Date(2018, 0, 0, 0, 1) },
            { type: 'stop', time: new Date(2018, 0, 0, 0, 4) },
          ],
        },
        screen: { events: [] },
        ads: {
          'ad-banner': {
            id: 'ad-banner',
            [DFP_RENDERED]: 1,
            [DFP_LOADED]: 1,
            [DFP_INSCREEN]: 1,
            [SCROLL_OFFSET_TOP]: 1500,
            [SCROLL_OFFSET_HEIGHT]: 300,
            [DFP_ADVERTISER_ID]: 900,
            [DFP_ADUNIT_PATH]: '/8578/dagbladet.no/forside',
            [DFP_CAMPAIGN_ID]: 500,
            [DFP_CREATIVE_ID]: 300,
            [DFP_LINE_ITEM_ID]: 1900,
            [DFP_NAME]: 'skyscraper-right',
            [SCROLL_POS_AD_LOAD]: 2490,
            [SCROLL_POS_INSCREEN_DFP]: 1,
            [SCROLL_POS_SLOT_RENDER_ENDED]: 30,
            [SCROLL_POS_SLOT_ONLOAD]: 40,
            [DFP_PREBID_WINNING_BID]: '43.20',
            [DFP_SIZE]: [200],
            [DFP_SOURCE_AGNOSTIC_CREATIVE_ID]: 901,
            [DFP_SOURCE_AGNOSTIC_LINE_ITEM_ID]: 401,
            [DFP_BIDDER]: 'appnexus',
          },
        },
      },
    };

    const expectedAdBody: BlinkEvent = {
      // Typical data for all events
      id: 'ad-banner',
      adId: 'ad-banner',
      pageView: '32be2425-907a-4f46-be45-cea8b00f5d7c',
      type: 'ads',
      version: VERSION,
      site: 'www.dagbladet.no',
      referrer: 'www.sol.no',
      // Ad specific data
      inscreenTime: 240000,
      inscreenTime0: 180000,
      scroll: {
        pos: {
          adLoad: 2490,
          inscreenDFP: 1,
          slotRenderEnded: 30,
          slotOnload: 40,
        },
        offsetTop: 1500,
        offsetHeight: 300,
      },
      dfp: {
        inscreen: 1,
        name: 'skyscraper-right',
        advertiserId: 900,
        campaignId: 500,
        creativeId: 300,
        adUnitPath: '/8578/dagbladet.no/forside',
        rendered: 1,
        loaded: 1,
        lineItemId: 1900,
        size: [200],
        sourceAgnosticCreativeId: 901,
        sourceAgnosticLineItemId: 401,
        bidder: 'appnexus',
        prebidWinningBid: '43.20',
      },
    };

    expect(prepareAdEvent({ id, page })).toEqual(expectedAdBody);
  });
});
