"use strict";

var _helpers = require("../helpers");

var _constants = require("@instructure/quiz-common/lib/constants");

describe('api helpers', function () {
  var dispatchStub = sinon.stub();
  afterEach(function () {
    dispatchStub.resetHistory();
  });
  it('opens the tray alert if 404 or 401 error', function (done) {
    (0, _helpers.handleError)(dispatchStub)({
      response: {
        status: 404
      }
    });
    setTimeout(function () {
      expect(dispatchStub).to.have.been.calledWith({
        payload: {
          key: _constants.TRAY_ALERT_OPEN,
          value: true
        },
        type: _constants.SET_UI
      });
      done();
    }, 500);
  });
  it('dispatches a generic error if not 404 or 401', function () {
    (0, _helpers.handleError)(dispatchStub)({
      response: {
        status: 500
      }
    });
    expect(dispatchStub).to.have.been.calledOnce();
    expect(dispatchStub).to.have.been.calledWithMatch({
      type: _constants.ADD_ALERT,
      payload: {
        message: 'Sorry, there was a 500 Error.',
        variant: 'error'
      }
    });
  });
  it('uses custom error message if given', function () {
    var errorMessage = 'O NOES';
    (0, _helpers.handleError)(dispatchStub, errorMessage)({
      response: {
        status: 500
      }
    });
    expect(dispatchStub).to.have.been.calledOnce();
    expect(dispatchStub).to.have.been.calledWithMatch({
      type: _constants.ADD_ALERT,
      payload: {
        message: errorMessage,
        variant: 'error'
      }
    });
  });
});