"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _formatMessage = _interopRequireDefault(require("quiz-interactions/lib/format-message"));

var _reduxStore = _interopRequireDefault(require("../../../reduxStore"));

var _formatMessage2 = _interopRequireDefault(require("@instructure/quiz-common/lib/format-message"));

var _SDKApp = require("../SDKApp");

var _ConfirmationModal = _interopRequireDefault(require("../ConfirmationModal"));

var _config = require("../../actions/config");

var _outcomes = require("../../actions/outcomes");

describe('SDKApp', function () {
  var alert = document.createElement('div');
  alert.setAttribute('role', 'alert');
  alert.setAttribute('aria-live', 'assertive');
  var sandbox = sinon.createSandbox();
  sandbox.stub(_reduxStore.default, 'dispatch');
  beforeEach(function () {
    document.body.appendChild(alert);
  });
  afterEach(function () {
    document.body.removeChild(alert);
    sandbox.resetHistory();
  });
  var defaultProps = {
    apiEndpoint: ''
  };
  var testbed = createTestbed(_SDKApp.SDKApp, defaultProps);
  shouldRenderComponent(testbed, {}, _SDKApp.SDKApp);
  shouldRenderComponent(testbed, {}, _ConfirmationModal.default);
  it('should dispatch actions for api endpoint change in props', function () {
    testbed.render({
      apiEndpoint: 'foo'
    });
    expect(_reduxStore.default.dispatch.getCall(0).args[0]).to.deep.include((0, _config.setAPIEndpoint)('foo'));
    testbed.render({
      apiEndpoint: 'bar'
    });
    expect(_reduxStore.default.dispatch).to.have.been.calledWith((0, _config.setAPIEndpoint)('bar'));
  });
  it('should dispatch actions for userToken change in props', function () {
    testbed.render({
      apiEndpoint: 'stuff',
      userToken: 'foo'
    });
    expect(_reduxStore.default.dispatch.getCall(0).args[0]).to.deep.include((0, _config.setUserToken)('foo'));
    testbed.render({
      apiEndpoint: 'stuff',
      userToken: 'bar'
    });
    expect(_reduxStore.default.dispatch).to.have.been.calledWith((0, _config.setUserToken)('bar'));
  });
  it('should dispatch actions for outcomes endpoint change in props', function () {
    testbed.render({
      outcomesEndpoint: 'foo'
    });
    expect(_reduxStore.default.dispatch.getCall(0).args[0]).to.deep.include((0, _outcomes.setOutcomesEndpoint)('foo'));
    testbed.render({
      outcomesEndpoint: 'bar'
    });
    expect(_reduxStore.default.dispatch).to.have.been.calledWith((0, _outcomes.setOutcomesEndpoint)('bar'));
  });
  it('should dispatch actions for outcomes token change in props', function () {
    testbed.render({
      outcomesToken: 'foo'
    });
    expect(_reduxStore.default.dispatch.getCall(0).args[0]).to.deep.include((0, _outcomes.setOutcomesToken)('foo'));
    testbed.render({
      outcomesToken: 'bar'
    });
    expect(_reduxStore.default.dispatch).to.have.been.calledWith((0, _outcomes.setOutcomesToken)('bar'));
  });
  it('should dispatch actions for context uuid change in props', function () {
    testbed.render({
      contextUuid: 'foo'
    });
    expect(_reduxStore.default.dispatch.getCall(0).args[0]).to.deep.include((0, _outcomes.setContextUuid)('foo'));
    testbed.render({
      contextUuid: 'bar'
    });
    expect(_reduxStore.default.dispatch).to.have.been.calledWith((0, _outcomes.setContextUuid)('bar'));
  });
  it('should configure format-message with the given locale and translations', function () {
    var setup = sandbox.spy(_formatMessage2.default, 'setup');
    var timeZone = 'America/Denver';
    testbed.render({
      timeZone: timeZone,
      locale: 'ru'
    });
    expect(setup).to.have.been.calledWithMatch({
      formats: {
        number: {
          decimal_2_min_integer_no_fraction: {
            style: 'decimal',
            maximumFractionDigits: 0,
            minimumIntegerDigits: 2
          },
          decimal_2_fraction_digits: {
            style: 'decimal',
            minimumFractionDigits: 2,
            maximumFractionDigits: 2
          }
        },
        date: {
          datetime_long: {
            weekday: 'long',
            year: 'numeric',
            month: 'long',
            day: 'numeric',
            hour: 'numeric',
            minute: 'numeric',
            timeZone: timeZone
          },
          datetime_medium: {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: 'numeric',
            minute: 'numeric',
            timeZone: timeZone
          },
          default: {
            timeZone: timeZone
          }
        },
        time: {
          hour_minute: {
            hour: 'numeric',
            minute: 'numeric',
            timeZone: timeZone
          }
        }
      },
      missingTranslation: 'ignore',
      locale: 'ru'
    });
  });
  it("should configure quiz-interactions' format-message with the given locale", function () {
    var setup = sandbox.spy(_formatMessage.default, 'setup');
    var locale = 'fr';
    testbed.render({
      locale: locale
    });
    expect(setup).to.have.been.calledWith({
      locale: locale
    });
  });
  it('should support Etc/GMT timezones', function () {
    testbed.render({
      timeZone: 'Etc/GMT+7'
    }); // Unpatched, this will raise a RangeError
    // - Expected Area/Location(/Location)* for time zone, got Etc/GMT+7

    expect(_formatMessage2.default.date(new Date(), 'medium')).to.not.be.empty();
  });
  it('should default to UTC for unrecognized time zones', function () {
    testbed.render({
      timeZone: 'fakeZone'
    });
    expect(_formatMessage2.default.date(new Date(), 'medium')).to.not.be.empty();
  });
});