"use strict";

require("core-js/modules/es.symbol");

require("core-js/modules/es.symbol.description");

require("core-js/modules/es.symbol.iterator");

require("core-js/modules/es.array.iterator");

require("core-js/modules/es.object.define-property");

require("core-js/modules/es.object.get-own-property-descriptor");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.string.iterator");

require("core-js/modules/es.weak-map");

require("core-js/modules/web.dom-collections.iterator");

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _ = _interopRequireWildcard(require("."));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

describe('components/Subscriber: Subscription', function () {
  test('subscribes and unsubscribes through context', function () {
    var subscribedWith;
    var mockSubscriber = {
      context: {
        formSubscription: {
          subscribe: jest.fn(function (callback) {
            expect(_typeof(callback)).toBe('function');
            subscribedWith = callback;
          }),
          unsubscribe: jest.fn(function (callback) {
            expect(_typeof(callback)).toBe('function');

            if (typeof subscribedWith !== 'undefined') {
              expect(callback).toBe(subscribedWith);
            }
          })
        }
      }
    };
    var subscription = new _.Subscription(mockSubscriber, function () {});
    expect(mockSubscriber.context.formSubscription.subscribe).not.toHaveBeenCalled();
    expect(mockSubscriber.context.formSubscription.unsubscribe).not.toHaveBeenCalled();
    subscription.trySubscribe();
    expect(mockSubscriber.context.formSubscription.subscribe).toHaveBeenCalledTimes(1);
    expect(mockSubscriber.context.formSubscription.unsubscribe).toHaveBeenCalledTimes(1); // Confirm that we correctly tracked the subscription callback before we test unsubscribe

    expect(_typeof(subscribedWith)).toBe('function');
    subscription.tryUnsubscribe();
    expect(mockSubscriber.context.formSubscription.unsubscribe).toHaveBeenCalledTimes(2);
  });
});
describe('components/Subscriber: Subscriber', function () {
  test('(un)subscribes when the component (un)mounts', function () {
    var context = {
      formSubscription: {
        subscribe: jest.fn(function () {}),
        unsubscribe: jest.fn(function () {})
      }
    };
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], null), {
      context: context
    });
    expect(context.formSubscription.subscribe).toHaveBeenCalledTimes(1);
    expect(context.formSubscription.unsubscribe).toHaveBeenCalledTimes(1);
    wrapper.unmount();
    expect(context.formSubscription.subscribe).toHaveBeenCalledTimes(1);
    expect(context.formSubscription.unsubscribe).toHaveBeenCalledTimes(2);
  });
  test('tries to trigger a render when subscription is updated', function () {
    var update;
    var context = {
      formSubscription: {
        subscribe: function subscribe(callback) {
          update = callback;
        },
        unsubscribe: function unsubscribe() {}
      }
    };
    var setStateSpy = jest.spyOn(_["default"].prototype, 'setState');
    var shouldComponentUpdateSpy = jest.spyOn(_["default"].prototype, 'shouldComponentUpdate');
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], null), {
      context: context
    });
    expect(setStateSpy).not.toHaveBeenCalled();
    expect(shouldComponentUpdateSpy).not.toHaveBeenCalled();
    update();
    expect(setStateSpy).toHaveBeenCalledTimes(1);
    expect(shouldComponentUpdateSpy).toHaveBeenCalledTimes(1);
    expect(shouldComponentUpdateSpy).toHaveLastReturnedWith(false);
    wrapper.setProps({
      a: true
    });
    expect(setStateSpy).toHaveBeenCalledTimes(1);
    expect(shouldComponentUpdateSpy).toHaveBeenCalledTimes(2);
    expect(shouldComponentUpdateSpy).toHaveLastReturnedWith(true);
  });
});