import prepareCustomEvent from '../prepare-custom-event';
import { VERSION } from '../../config/config';
import { BlinkEvent } from '../../types';
import { Page } from '../../selectors/get-page-state';

describe('prepareCustomEvent', () => {
  it('should format properly based on state', () => {
    const customDomain = 'fake-domain';
    const customType = 'fake-type';
    const customContent = 'content';
    const customValue = 12;
    const time = new Date(2);
    const page: Page = {
      id: 'default',
      state: {
        general: {
          pageView: 'some-pageview-uuid',
          site: 'www.dagbladet.no',
          referrer: 'www.dinside.no',
        },
      },
    };

    const expected: BlinkEvent = {
      type: 'custom',
      pageView: 'some-pageview-uuid',
      site: 'www.dagbladet.no',
      referrer: 'www.dinside.no',
      version: VERSION,
      id: 'fake-domainfake-type',
      customDomain: 'fake-domain',
      customType: 'fake-type',
      customContent: 'content',
      customValue: 12,
      time: new Date(2),
    };
    expect(
      prepareCustomEvent({
        page,
        customDomain,
        customType,
        customContent,
        customValue,
        time,
      }),
    ).toEqual(expected);
  });

  it('should work without time', () => {
    const customDomain = 'fake-domain';
    const customType = 'fake-type';
    const customContent = 'content';
    const customValue = 12;
    const page: Page = {
      id: 'default',
      state: {
        general: {
          pageView: 'some-pageview-uuid',
          site: 'www.dagbladet.no',
          referrer: 'www.dinside.no',
          customUserAgent: 'fake-ua',
        },
      },
    };

    const expected: BlinkEvent = {
      type: 'custom',
      pageView: 'some-pageview-uuid',
      site: 'www.dagbladet.no',
      referrer: 'www.dinside.no',
      version: VERSION,
      customUserAgent: 'fake-ua',
      id: 'fake-domainfake-type',
      customDomain: 'fake-domain',
      customType: 'fake-type',
      customContent: 'content',
      customValue: 12,
      time: new Date(1),
    };
    const expectedFields = Object.keys(expected).sort();
    const fields = Object.keys(
      prepareCustomEvent({
        page,
        customDomain,
        customType,
        customContent,
        customValue,
      }),
    ).sort();
    expect(fields).toEqual(expectedFields);
  });
});
