"use strict";

var _react = require("@testing-library/react");
var _useAnalytics = require("./use-analytics");
var _constants = require("./constants");
/**
 * Class that mocks AnalyticsService
 * to give ability to check all mediators that it triggers
 */
class InMemoryAnalyticsProvider extends _useAnalytics.AnalyticsProvider {
  constructor() {
    super();
    /** {AnalyticsEvent[]} */
    this.queue = [];
  }
  handle(event) {
    this.queue.push(event);
  }
  getLastMediator() {
    return this.queue.length ? this.queue[this.queue.length - 1] : null;
  }
  getLastContextEventMediator() {
    return this.queue.length ? this.queue[this.queue.length - 2] : null;
  }
}
describe('useAnalytics', function () {
  describe('Event tracking', function () {
    let provider;
    let hook;
    beforeEach(() => {
      provider = new InMemoryAnalyticsProvider();
      // eslint-disable-next-line testing-library/no-render-in-lifecycle
      hook = (0, _react.renderHook)(() => (0, _useAnalytics.useAnalytics)({
        analyticsProviders: [provider]
      }));
      (0, _react.act)(() => hook.result.current.pageView('page.key'));
    });
    it('should pass custom event to provider', function () {
      (0, _react.act)(() => hook.result.current.track({
        event: 'customEvent',
        label: 'label',
        property: 'property',
        value: 10
      }));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['page.key', 'customEvent', 'label', 'property', 10, false]);
    });
    it('should pass click event to provider', function () {
      (0, _react.act)(() => hook.result.current.click({
        label: 'label',
        property: 'property'
      }));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['page.key', _constants.ANALYTICS_EVENTS.ITEM_CLICK, 'label', 'property', null, false]);
    });
    it('should pass change event to provider', function () {
      (0, _react.act)(() => hook.result.current.change({
        element: 'label',
        property: 'property'
      }));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['page.key', _constants.ANALYTICS_EVENTS.ITEM_CHANGE, 'label', 'property', null, false]);
    });
    it('should track identity', function () {
      (0, _react.act)(() => hook.result.current.identify('user_id', {
        data: 'some_user_data'
      }));
      expect(provider.getLastContextEventMediator().method).toEqual(_constants.ANALYTICS_METHODS.IDENTIFY);
      expect(provider.getLastContextEventMediator().args).toEqual(['user_id', {
        data: 'some_user_data'
      }]);
    });
    it('should track page view', function () {
      (0, _react.act)(() => hook.result.current.pageView('some.page.key'));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['some.page.key', _constants.ANALYTICS_EVENTS.PAGE_VIEW, 'some.page.key']);
    });
    it('should track tab view', function () {
      (0, _react.act)(() => hook.result.current.tabView('tab-name'));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['page.key', _constants.ANALYTICS_EVENTS.TAB_VIEW, 'tab-name', null, null, false]);
    });
    it('should track dialog view', function () {
      (0, _react.act)(() => hook.result.current.dialogView('dialog-name', 'property'));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK);
      expect(provider.getLastMediator().args).toEqual(['page.key', _constants.ANALYTICS_EVENTS.DIALOG_VIEW, 'dialog-name', 'property', null, false]);
    });
    it('should pass data from internal track to provider', function () {
      (0, _react.act)(() => hook.result.current.trackInternal('eventType', {
        prop: 'value'
      }));
      expect(provider.getLastMediator().method).toEqual(_constants.ANALYTICS_METHODS.TRACK_INTERNAL);
      expect(provider.getLastMediator().args).toEqual(['eventType', {
        prop: 'value'
      }]);
    });
  });
});