"use strict";

var _chai = require("chai");

var _modifications = require("../modifications");

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

describe('modifications actions', function () {
  var expectedActionForModsAndId = function expectedActionForModsAndId(type, id, mods) {
    return {
      type: type,
      payload: {
        id: id,
        modifications: mods
      }
    };
  };

  var expectActionsEqual = function expectActionsEqual(a1, a2) {
    (0, _chai.expect)(a1).to.deep.equal(a2);
  };

  describe('quizzes', function () {
    var quizId = '1';
    it('returns a changeQuizTitle action', function () {
      var newTitle = 'new quiz title';
      var changeQuizTitleAction = (0, _modifications.changeQuizTitle)(quizId, newTitle);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        title: newTitle
      });
      expectActionsEqual(changeQuizTitleAction, expectedAction);
    });
    it('returns a changeQuizInstructions action ', function () {
      var newInstructions = 'some new instructions';
      var changeInstructionsAction = (0, _modifications.changeQuizInstructions)(quizId, newInstructions);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        instructions: newInstructions
      });
      expectActionsEqual(changeInstructionsAction, expectedAction);
    });
    it('returns a changeQuizShuffleQuestions action ', function () {
      var newShuffleQuestions = true;
      var changeShuffleQuestionsAction = (0, _modifications.changeQuizShuffleQuestions)(quizId, newShuffleQuestions);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        shuffleQuestions: newShuffleQuestions
      });
      expectActionsEqual(changeShuffleQuestionsAction, expectedAction);
    });
    it('returns a changeQuizRequireStudentAccessCode action ', function () {
      var newRequireStudentAccessCode = true;
      var action = (0, _modifications.changeQuizRequireStudentAccessCode)(quizId, newRequireStudentAccessCode);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        requireStudentAccessCode: newRequireStudentAccessCode
      });
      expectActionsEqual(action, expectedAction);
    });
    it('returns a changeQuizStudentAccessCode action ', function () {
      var newStudentAccessCode = 'code2';
      var action = (0, _modifications.changeQuizStudentAccessCode)(quizId, newStudentAccessCode);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        studentAccessCode: newStudentAccessCode
      });
      expectActionsEqual(action, expectedAction);
    });
    it('returns a changeQuizAllowBacktracking action ', function () {
      var newBacktracking = true;
      var backtrackingAction = (0, _modifications.changeQuizAllowBacktracking)(quizId, newBacktracking);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        allowBacktracking: newBacktracking
      });
      expectActionsEqual(backtrackingAction, expectedAction);
    });
    it('returns a changeQuizOneAtATimeType action ', function () {
      var newOQAAT = 'question';
      var oqaatAction = (0, _modifications.changeQuizOneAtATimeType)(quizId, newOQAAT);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        oneAtATimeType: newOQAAT
      });
      expectActionsEqual(oqaatAction, expectedAction);
    });
    it('returns a changeQuizHasTimeLimit action ', function () {
      var newHasTimeLimit = true;
      var changeHasTimeLimitAction = (0, _modifications.changeQuizHasTimeLimit)(quizId, newHasTimeLimit);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        hasTimeLimit: newHasTimeLimit
      });
      expectActionsEqual(changeHasTimeLimitAction, expectedAction);
    });
    it('returns a changeSessionTimeLimitInSeconds action ', function () {
      var newSessionTimeLimitInSeconds = 100;
      var changeSessionTimeLimitInSecondsAction = (0, _modifications.changeSessionTimeLimitInSeconds)(quizId, newSessionTimeLimitInSeconds);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        sessionTimeLimitInSeconds: newSessionTimeLimitInSeconds
      });
      expectActionsEqual(changeSessionTimeLimitInSecondsAction, expectedAction);
    });
    it('returns a changeQuizCalculatorType action ', function () {
      var newCalculatorType = 'scientific';
      var changeCalculatorTypeAction = (0, _modifications.changeQuizCalculatorType)(quizId, newCalculatorType);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        calculatorType: newCalculatorType
      });
      expectActionsEqual(changeCalculatorTypeAction, expectedAction);
    });
    it('returns a changeQuizOutcomeAlignmentSetGuid action ', function () {
      var newGuid = 'xxxx-xxxx-xxxx-xxxx';
      var changeQuizOutcomeAlignmentSetGuidAction = (0, _modifications.changeQuizOutcomeAlignmentSetGuid)(quizId, newGuid);
      var expectedAction = expectedActionForModsAndId(_constants.QUIZ_MODIFICATION, quizId, {
        outcomeAlignmentSetGuid: newGuid
      });
      expectActionsEqual(changeQuizOutcomeAlignmentSetGuidAction, expectedAction);
    });
  });
  describe('#changeQuizEntryPoints', function () {
    it('returns a changeQuizEntryPoints action', function () {
      (0, _chai.expect)((0, _modifications.changeQuizEntryPoints)('1', 1)).to.deep.eq({
        type: _constants.QUIZ_ENTRIES_MODIFICATION,
        payload: {
          id: '1',
          modifications: {
            pointsPossible: 1,
            regradeAdjustedPointsPossible: null
          }
        }
      });
    });
  });
  describe('#changeQuizEntryEntry', function () {
    it('returns a changeQuizEntryEntry action', function () {
      (0, _chai.expect)((0, _modifications.changeQuizEntryEntry)('1', '2')).to.deep.eq({
        type: _constants.QUIZ_ENTRIES_MODIFICATION,
        payload: {
          id: '1',
          modifications: {
            entry: {
              id: '2'
            }
          }
        }
      });
    });
    it('includes entryType only if specified', function () {
      (0, _chai.expect)((0, _modifications.changeQuizEntryEntry)('1', '3', 'BankEntry')).to.deep.eq({
        type: _constants.QUIZ_ENTRIES_MODIFICATION,
        payload: {
          id: '1',
          modifications: {
            entry: {
              id: '3'
            },
            entryType: 'BankEntry'
          }
        }
      });
    });
  });
  describe('#createStimulus', function () {
    it('dispatches a createStimulus action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.createStimulus)('1', 1)(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CREATE_TEMPORARY_STIMULUS,
        payload: {
          quizId: '1',
          position: 1
        }
      })).to.be.true();
    });
  });
  describe('#modifyStimulus', function () {
    it('dispatches a modify stimulus action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      var fakeStimulus = {
        id: 1,
        title: 'hello'
      };
      (0, _modifications.modifyStimulus)(fakeStimulus)(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.STIMULUS_MODIFICATION,
        payload: {
          stimulus: fakeStimulus
        }
      })).to.be.true();
    });
  });
  describe('#clearTemporaryStimulus', function () {
    it('dispatches a modify stimulus action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.clearTemporaryStimulus)('1')(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CLEAR_TEMPORARY_STIMULUS,
        payload: {
          quizId: '1'
        }
      })).to.be.true();
    });
  });
  describe('#createItem', function () {
    it('dispatches a createItem action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.createItem)('1', 1, {}, '55', '4')(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CREATE_TEMPORARY_ITEM,
        payload: {
          quizId: '1',
          position: 1,
          interactionTypeData: {},
          parentEntryId: '55',
          stimulusId: '4'
        }
      })).to.be.true();
    });
  });
  describe('#createBankItem', function () {
    it('dispatches a createBankItem action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.createBankItem)('1', 'Item', {})(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CREATE_TEMPORARY_BANK_ITEM,
        payload: {
          bankId: '1',
          interactionTypeData: {},
          bankEntryType: 'Item'
        }
      })).to.be.true();
    });
  });
  describe('#createBank', function () {
    it('dispatches a createBank action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.createBank)()(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CREATE_TEMPORARY_BANK,
        payload: {
          language: null
        }
      })).to.be.true();
    });
  });
  describe('#clearTemporaryItem', function () {
    it('dispatches a clearTemporaryItem action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.clearTemporaryItem)('1')(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CLEAR_TEMPORARY_ITEM,
        payload: {
          quizId: '1'
        }
      })).to.be.true();
    });
    it('returns a changeBank action', function () {
      var newName = 'new name';
      var bankId = '1';
      var changeBankAction = (0, _modifications.changeBank)(bankId, {
        name: newName
      });
      var expectedAction = expectedActionForModsAndId(_constants.BANK_MODIFICATION, bankId, {
        name: newName
      });
      expectActionsEqual(changeBankAction, expectedAction);
    });
  });
  describe('#clearTemporaryBankItem', function () {
    it('dispatches a clearTemporaryBankItem action', function () {
      var dispatch = sinon.stub();
      var getState = sinon.stub();
      (0, _modifications.clearTemporaryBankItem)('1')(dispatch, getState);
      (0, _chai.expect)(dispatch.calledWith({
        type: _constants.CLEAR_TEMPORARY_BANK_ITEM,
        payload: {
          bankId: '1'
        }
      })).to.be.true();
    });
  });
  describe('#clearModifications', function () {
    it('returns a clearModifications actions', function () {
      var clearCall = (0, _modifications.clearModifications)('testType', 'testId', [1, 2]);
      (0, _chai.expect)(clearCall).to.deep.eq({
        type: _constants.CLEAR_MODIFICATIONS,
        payload: {
          objectType: 'testType',
          objectId: 'testId',
          deepKeys: [1, 2]
        }
      });
    });
  });
  describe('modify items', function () {
    var itemId = '1';
    it('returns a changeItemTitle action', function () {
      var newTitle = 'new quiz title';
      var changeItemTitleAction = (0, _modifications.changeItemTitle)(itemId, newTitle);
      var expectedAction = expectedActionForModsAndId(_constants.ITEM_MODIFICATION, itemId, {
        title: newTitle
      });
      expectActionsEqual(changeItemTitleAction, expectedAction);
    });
    it('returns a changeItemBody action ', function () {
      var newInstructions = 'some new instructions';
      var changeItemBodyAction = (0, _modifications.changeItemBody)(itemId, newInstructions);
      var expectedAction = expectedActionForModsAndId(_constants.ITEM_MODIFICATION, itemId, {
        itemBody: newInstructions
      });
      expectActionsEqual(changeItemBodyAction, expectedAction);
    });
    it('can modify an item with arbitrary changes ', function () {
      var modifications = {
        foo: 'bar'
      };
      var changeItemStateAction = (0, _modifications.changeItemState)(itemId, modifications);
      var expectedAction = expectedActionForModsAndId(_constants.ITEM_MODIFICATION, itemId, modifications);
      expectActionsEqual(changeItemStateAction, expectedAction);
    });
    it('can change item feedback', function () {
      var newFeedback = {
        correct: 'correct',
        neutral: 'neutral'
      };
      var changeItemFeedbackAction = (0, _modifications.changeItemFeedback)(itemId, newFeedback);
      var expectedAction = expectedActionForModsAndId(_constants.ITEM_MODIFICATION, itemId, {
        feedback: newFeedback
      });
      expectActionsEqual(changeItemFeedbackAction, expectedAction);
    });
    it('returns a changeOutcomeAlignmentSetGuid action ', function () {
      var newGuid = '54084912-f7a9-4bf5-96e3-887f67fe11c7';
      var changeOutcomeAlignmentSetGuidAction = (0, _modifications.changeOutcomeAlignmentSetGuid)(itemId, newGuid);
      var expectedAction = expectedActionForModsAndId(_constants.ITEM_MODIFICATION, itemId, {
        outcomeAlignmentSetGuid: newGuid
      });
      expectActionsEqual(changeOutcomeAlignmentSetGuidAction, expectedAction);
    });
  });
  describe('modify session item results', function () {
    it('returns a well-formed action', function () {
      var id = '1_5';
      var modifications = {
        bar: 'baz'
      };
      var modificationAction = (0, _modifications.sessionItemResultsModification)(id, modifications);
      var expectedAction = {
        type: _constants.SESSION_ITEM_RESULTS_MODIFICATION,
        payload: {
          id: id,
          modifications: modifications
        }
      };
      expectActionsEqual(expectedAction, modificationAction);
    });
  });
  describe('change fudge points', function () {
    it('sets fudge points in session results', function () {
      var resultId = '15';
      var fudgePoints = 5;
      var modificationAction = (0, _modifications.changeFudgePoints)(resultId, fudgePoints);
      var expectedAction = {
        type: _constants.QUIZ_SESSION_RESULT_MODIFICATION,
        payload: {
          id: resultId,
          modifications: {
            fudgePoints: fudgePoints
          }
        }
      };
      expectActionsEqual(expectedAction, modificationAction);
    });
  });
  describe('#moveTemporaryQuizEntry', function () {
    it('returns a payload with direction if up or down', function () {
      var changeItemStateAction = (0, _modifications.moveTemporaryQuizEntry)('up', null, false);
      var payload = {
        direction: 'up',
        movingDown: false,
        stimulusQuizEntryId: null
      };
      var expectedAction = {
        type: _constants.MOVE_TEMPORARY_ITEM,
        payload: payload
      };
      expectActionsEqual(changeItemStateAction, expectedAction);
    });
    it('returns a payload with position if integer given', function () {
      var changeItemStateAction = (0, _modifications.moveTemporaryQuizEntry)(20, null, false);
      var payload = {
        position: 20,
        movingDown: false,
        stimulusQuizEntryId: null
      };
      var expectedAction = {
        type: _constants.MOVE_TEMPORARY_ITEM,
        payload: payload
      };
      expectActionsEqual(changeItemStateAction, expectedAction);
    });
  });
});