"use strict";

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

var _immutable = require("immutable");

var _chai = require("chai");

var _quizEntries = _interopRequireDefault(require("../quizEntries"));

var _quizEntries2 = require("../../actions/quizEntries");

describe('quizEntries reducer', function () {
  describe('addQuizEntries', function () {
    it('should add quiz entry to state', function () {
      var initialState = (0, _immutable.fromJS)({});
      var action = (0, _quizEntries2.addQuizEntries)([{
        id: '1',
        pointsPossible: '5'
      }]);
      var nextState = (0, _quizEntries.default)(initialState, action);
      var nextQuizEntryPoints = nextState.get('1').get('pointsPossible');
      (0, _chai.expect)(nextQuizEntryPoints).to.equal('5');
    });
    it('does not merge new with old state', function () {
      var initialState = (0, _immutable.fromJS)({
        2: {
          id: '2',
          pointsPossible: '5'
        }
      });
      var action = (0, _quizEntries2.addQuizEntries)([{
        id: '1',
        pointsPossible: '5'
      }]);
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        1: {
          id: '1',
          pointsPossible: '5'
        }
      }));
    });
  });
  describe('addQuizEntry', function () {
    it('should add quiz entry to state', function () {
      var initialState = (0, _immutable.fromJS)({});
      var action = (0, _quizEntries2.addQuizEntry)({
        id: '1',
        pointsPossible: '5'
      });
      var nextState = (0, _quizEntries.default)(initialState, action);
      var nextQuizEntryPoints = nextState.get('1').get('pointsPossible');
      (0, _chai.expect)(nextQuizEntryPoints).to.equal('5');
    });
    it('does merge new with old state', function () {
      var initialState = (0, _immutable.fromJS)({
        2: {
          id: '2',
          pointsPossible: '5'
        }
      });
      var action = (0, _quizEntries2.addQuizEntry)({
        id: '1',
        pointsPossible: '5'
      });
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        1: {
          id: '1',
          pointsPossible: '5'
        },
        2: {
          id: '2',
          pointsPossible: '5'
        }
      }));
    });
  });
  describe('deleteQuizEntry', function () {
    it('removes the quiz entry from state', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1'
        },
        '2': {
          id: '2'
        }
      });
      var action = (0, _quizEntries2.deleteQuizEntry)('1');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '2': {
          id: '2'
        }
      }));
    });
    it('updates positions', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1
        },
        '2': {
          id: '2',
          position: 2
        },
        '3': {
          id: '3',
          position: 3
        }
      });
      var action = (0, _quizEntries2.deleteQuizEntry)('2');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1
        },
        '3': {
          id: '3',
          position: 2
        }
      }));
    });
  });
  describe('deleteStimulusEntry', function () {
    it('removes the stimulus entry from state', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.deleteStimulusEntry)('1');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '2': {
          id: '2',
          stimulusQuizEntryId: ''
        }
      }));
    });
    it('removes the given child entries from state', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          stimulusQuizEntryId: '1'
        },
        '3': {
          id: '3',
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.deleteStimulusEntry)('1', ['2']);
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '3': {
          id: '3',
          stimulusQuizEntryId: ''
        }
      }));
    });
    it('disassociates child entries from the stimulus', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          stimulusQuizEntryId: '1'
        }
      });
      var action = (0, _quizEntries2.deleteStimulusEntry)('1');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '2': {
          id: '2',
          stimulusQuizEntryId: ''
        }
      }));
    });
    it('updates positions', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 2,
          stimulusQuizEntryId: ''
        },
        '3': {
          id: '3',
          position: 3,
          stimulusQuizEntryId: '2'
        },
        '4': {
          id: '4',
          position: 4,
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.deleteStimulusEntry)('2', ['3']);
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '4': {
          id: '4',
          position: 2,
          stimulusQuizEntryId: ''
        }
      }));
    });
  });
  describe('moveQuizEntry', function () {
    it('updates stimulusQuizEntryId', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 2,
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.moveQuizEntry)('2', 2, '1');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 2,
          stimulusQuizEntryId: '1'
        }
      }));
    });
    it('updates positions when moving an entry up', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 2,
          stimulusQuizEntryId: ''
        },
        '3': {
          id: '3',
          position: 3,
          stimulusQuizEntryId: ''
        },
        '4': {
          id: '4',
          position: 4,
          stimulusQuizEntryId: ''
        },
        '5': {
          id: '5',
          position: 5,
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.moveQuizEntry)('4', 2, '');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 3,
          stimulusQuizEntryId: ''
        },
        '3': {
          id: '3',
          position: 4,
          stimulusQuizEntryId: ''
        },
        '4': {
          id: '4',
          position: 2,
          stimulusQuizEntryId: ''
        },
        '5': {
          id: '5',
          position: 5,
          stimulusQuizEntryId: ''
        }
      }));
    });
    it('updates positions when moving an entry down', function () {
      var initialState = (0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 2,
          stimulusQuizEntryId: ''
        },
        '3': {
          id: '3',
          position: 3,
          stimulusQuizEntryId: ''
        },
        '4': {
          id: '4',
          position: 4,
          stimulusQuizEntryId: ''
        },
        '5': {
          id: '5',
          position: 5,
          stimulusQuizEntryId: ''
        }
      });
      var action = (0, _quizEntries2.moveQuizEntry)('2', 4, '');
      var nextState = (0, _quizEntries.default)(initialState, action);
      (0, _chai.expect)(nextState).to.equal((0, _immutable.fromJS)({
        '1': {
          id: '1',
          position: 1,
          stimulusQuizEntryId: ''
        },
        '2': {
          id: '2',
          position: 4,
          stimulusQuizEntryId: ''
        },
        '3': {
          id: '3',
          position: 2,
          stimulusQuizEntryId: ''
        },
        '4': {
          id: '4',
          position: 3,
          stimulusQuizEntryId: ''
        },
        '5': {
          id: '5',
          position: 5,
          stimulusQuizEntryId: ''
        }
      }));
    });
  });
});