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

describe('Click intregration test', () => {
  it('should send a single click event for url click', () => {
    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',
    });

    // Then send a article impression event, so we get the impression data
    blink.articlePreviewScreenEnter({
      url: 'https://www.dagbladet.no/a/2313',
      title: 'Another day',
      height: 250,
      width: 480,
      position: 5,
      personalizationSystemUsed: 'cerebro',
      personalizationParametersRequested: 'xavier-pluss',
    });

    // The event we want to test
    blink.click({
      url: 'https://www.dagbladet.no/a/2313',
      context: ['tag=a', 'tag=article&class=important'],
    });

    expect(mockSend.mock.calls[1][0]).toEqual([
      {
        id: 'dagbladet.no/2313',
        clickId: '',
        context: ['tag=a', 'tag=article&class=important'],
        type: 'click',
        pageView: 'the-pageview-id',
        referrer: 'www.sol.no',
        site: 'www.kk.no',
        version: VERSION,
        article: {
          harvesterId: 'dagbladet.no/2313',
          url: 'https://www.dagbladet.no/a/2313',
        },
        title: 'Another day',
        height: 250,
        width: 480,
        position: 5,
        personalizationSystemUsed: 'cerebro',
        personalizationParametersRequested: 'xavier-pluss',
      },
    ]);

    expect(
      blink.getArticlePreview({
        url: 'https://www.dagbladet.no/a/2313',
        time: new Date(2000),
      }),
    ).toEqual({
      height: 250,
      id: 'dagbladet.no/2313',
      personalizationSystemUsed: 'cerebro',
      personalizationParametersRequested: 'xavier-pluss',
      position: 5,
      title: 'Another day',
      url: 'https://www.dagbladet.no/a/2313',
      width: 480,
      clicked: true,
      context: undefined,
    });
  });

  it('should send a single click event for element clicked with id', () => {
    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.kk.no',
      site: 'www.dagbladet.no',
    });

    // The event we want to test
    blink.click({ id: 'cta-modal-16166' });

    expect(mockSend.mock.calls[0][0]).toEqual([
      {
        type: 'click',
        clickId: 'cta-modal-16166',
        harvesterId: 'some.site/a0bb0a25c11044eedb8005b99f9590a6',
        context: [],
        id: 'cta-modal-16166',
        pageView: 'the-pageview-id',
        referrer: 'www.kk.no',
        site: 'www.dagbladet.no',
        version: VERSION,
        // Undefined things
        article: undefined,
        height: 0,
        personalizationParametersRequested: undefined,
        personalizationSystemUsed: undefined,
        position: undefined,
        title: undefined,
        width: 0,
      },
    ]);
  });

  it('should send a two click events for two elements clicked with id', () => {
    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://www.dagbladet.no/192823',
      pageView: 'the-pageview-id',
      referrer: 'www.kk.no',
      site: 'www.dagbladet.no',
    });

    // Click first element with id
    blink.click({ id: 'cta-modal-1' });
    // Click second element with id
    blink.click({ id: 'cta-modal-2' });

    expect(mockSend.mock.calls[0][0]).toEqual([
      {
        type: 'click',
        clickId: 'cta-modal-1',
        harvesterId: 'dagbladet.no/192823',
        id: 'cta-modal-1',
        context: [],
        pageView: 'the-pageview-id',
        referrer: 'www.kk.no',
        site: 'www.dagbladet.no',
        version: VERSION,
        // Undefined things
        article: undefined,
        height: 0,
        personalizationParametersRequested: undefined,
        personalizationSystemUsed: undefined,
        position: undefined,
        title: undefined,
        width: 0,
      },
    ]);

    expect(mockSend.mock.calls[1][0]).toEqual([
      {
        type: 'click',
        clickId: 'cta-modal-2',
        harvesterId: 'dagbladet.no/192823',
        id: 'cta-modal-2',
        context: [],
        pageView: 'the-pageview-id',
        referrer: 'www.kk.no',
        site: 'www.dagbladet.no',
        version: VERSION,
        // Undefined things
        article: undefined,
        height: 0,
        personalizationParametersRequested: undefined,
        personalizationSystemUsed: undefined,
        position: undefined,
        title: undefined,
        width: 0,
      },
    ]);
  });

  it('should send two separate events for two separate pages', () => {
    enum PAGES {
      FIRST = 'FIRST_PAGE',
      SECOND = 'SECOND_PAGE',
    }

    const mockSend = jest.fn();
    const mockSendDirect = jest.fn();

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

    // FIRST PAGE INIT
    blink.pageInit({
      url: 'http://some.site',
      pageView: `${PAGES.FIRST}-pageview`,
      pageId: PAGES.FIRST,
      referrer: 'www.sol.no',
      site: 'www.kk.no',
    });

    // SECOND PAGE INIT
    blink.pageInit({
      url: 'http://other.site',
      pageView: `${PAGES.SECOND}-pageview`,
      pageId: PAGES.SECOND,
      referrer: 'www.sol.no',
      site: 'www.kk.no',
    });

    // FIRST PAGE CLICK
    blink.click({
      url: 'https://www.dagbladet.no/a/111',
      pageId: PAGES.FIRST,
    });

    // FIRST PAGE CLICK
    blink.click({
      url: 'https://www.dagbladet.no/a/222',
      pageId: PAGES.SECOND,
    });

    expect(mockSend.mock.calls.length).toBe(0);
    expect(mockSendDirect.mock.calls.length).toBe(2);

    // Check first event
    const firstEvent = mockSendDirect.mock.calls[0][0][0];
    expect(firstEvent).toEqual({
      article: {
        harvesterId: 'dagbladet.no/111',
        url: 'https://www.dagbladet.no/a/111',
      },
      personalizationParametersRequested: undefined,
      personalizationSystemUsed: undefined,
      position: undefined,
      id: 'dagbladet.no/111',
      clickId: '',
      context: [],
      height: 0,
      pageView: 'FIRST_PAGE-pageview',
      referrer: 'www.sol.no',
      site: 'www.kk.no',
      type: 'click',
      title: undefined,
      version: VERSION,
      width: 0,
    });

    // Check second event
    const secondEvent = mockSendDirect.mock.calls[1][0][0];
    expect(secondEvent).toEqual({
      id: 'dagbladet.no/222',
      clickId: '',
      context: [],
      article: {
        harvesterId: 'dagbladet.no/222',
        url: 'https://www.dagbladet.no/a/222',
      },
      height: 0,
      personalizationParametersRequested: undefined,
      personalizationSystemUsed: undefined,
      position: undefined,
      pageView: 'SECOND_PAGE-pageview',
      referrer: 'www.sol.no',
      site: 'www.kk.no',
      title: undefined,
      type: 'click',
      version: VERSION,
      width: 0,
    });
  });
});
