"use strict";

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

var _immutable = require("immutable");

var _SessionItem = _interopRequireDefault(require("../SessionItem"));

var _SessionItemResult = _interopRequireDefault(require("../SessionItemResult"));

var _Stimulus = _interopRequireDefault(require("../Stimulus"));

var _Item = _interopRequireDefault(require("../Item"));

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

var _recordTests = require("../../../../tests/util/recordTests");

describe('SessionItem Record', function () {
  describe('#constructor', function () {
    var defaultsToCheck = {
      item: {}
    };
    (0, _recordTests.defaultChecks)(_SessionItem.default, defaultsToCheck);
  });
  describe('#getItem', function () {
    it('returns an item using the data in the "item" key', function () {
      var itemData = {
        id: '2',
        itemBody: 'foo'
      };
      var sessionItem = new _SessionItem.default({
        item: itemData
      });
      var expectedItem = new _Item.default(itemData, {});
      expect((0, _immutable.is)(sessionItem.getItem(), expectedItem)).to.be.true();
    });
  });
  describe('#sessionItemResult', function () {
    var fakeState = (0, _immutable.fromJS)({
      sessionItems: {
        '10_1': {
          id: '10_1'
        }
      },
      sessionItemResults: {
        '10_1': {
          id: '10_1'
        }
      }
    });
    before(function () {
      (0, _reduxStore.setFakeStore)(fakeState);
    });
    after(function () {
      (0, _reduxStore.unsetFakeStore)();
    });
    it('returns the associated sessionItem', function () {
      var si = new _SessionItem.default({
        id: '10_1'
      });
      expect(si.sessionItemResult.id).to.eq(new _SessionItemResult.default({
        id: '10_1'
      }).id);
    });
    it('returns an empty SessionItemResult if not found', function () {
      var si = new _SessionItem.default({
        id: '10_2'
      });
      expect(si.sessionItemResult.id).to.eq(new _SessionItemResult.default().id);
    });
  });
  describe('#type', function () {
    it('returns Stimulus if stimulus is not null', function () {
      var si = new _SessionItem.default({
        stimulus: {
          id: '1'
        }
      });
      expect(si.type).to.eq('Stimulus');
    });
    it('returns Item if stimulus is null', function () {
      var si = new _SessionItem.default();
      expect(si.type).to.eq('Item');
    });
  });
  describe('#isStimulus', function () {
    it('returns true if stimulus is not null', function () {
      var si = new _SessionItem.default({
        stimulus: {
          id: '1'
        }
      });
      expect(si.isStimulus).to.be.true();
    });
    it('returns false if stimulus is null', function () {
      var si = new _SessionItem.default();
      expect(si.isStimulus).to.be.false();
    });
  });
  describe('#stimulusRecord', function () {
    it('returns a Stimulus record', function () {
      var si = new _SessionItem.default();
      expect(si.stimulusRecord).to.be.instanceOf(_Stimulus.default);
    });
    it('returns a Stimulus record with the data from the sessionItem', function () {
      var si = new _SessionItem.default({
        stimulus: {
          id: '1'
        }
      });
      expect(si.stimulusRecord.id).to.eq('1');
    });
  });
  describe('#hasNestedRecords', function () {
    it('returns true if stimulus', function () {
      var si = new _SessionItem.default({
        stimulus: (0, _immutable.Map)({
          id: '1'
        })
      });
      expect(si.hasNestedRecords).to.be.true();
    });
    it('returns false if not stimulus', function () {
      var si = new _SessionItem.default();
      expect(si.hasNestedRecords).to.be.false();
    });
  });
  describe('#positionSlots', function () {
    var fakeState = (0, _immutable.fromJS)({
      sessionItems: {
        1: {
          id: '1',
          stimulusKey: '1',
          quizSessionId: '1'
        },
        2: {
          id: '2',
          stimulusKey: '1',
          quizSessionId: '1'
        },
        4: {
          id: '4',
          stimulusKey: '1',
          quizSessionId: '1'
        },
        3: {
          id: '3'
        }
      }
    });
    before(function () {
      (0, _reduxStore.setFakeStore)(fakeState);
    });
    after(function () {
      (0, _reduxStore.unsetFakeStore)();
    });
    it('returns 0 if not a stimulus', function () {
      var si = new _SessionItem.default();
      expect(si.positionSlots).to.eq(0);
    });
    it('returns the number of items minus 1 if a stimulus', function () {
      var si = new _SessionItem.default({
        stimulus: {},
        stimulusKey: '1',
        quizSessionId: '1'
      });
      expect(si.positionSlots).to.eq(2);
    });
    it('returns 0 if an empty stimulus', function () {
      var si = new _SessionItem.default({
        stimulus: {},
        stimulusKey: '100',
        quizSessionId: '1'
      });
      expect(si.positionSlots).to.eq(0);
    });
  });
  describe('#displayPositionSlots', function () {
    var fakeState = (0, _immutable.fromJS)({
      sessionItems: {
        1: {
          id: '1',
          stimulusKey: '1',
          quizSessionId: '1'
        },
        2: {
          id: '2',
          stimulusKey: '1',
          quizSessionId: '1'
        },
        3: {
          id: '3'
        }
      }
    });
    before(function () {
      (0, _reduxStore.setFakeStore)(fakeState);
    });
    after(function () {
      (0, _reduxStore.unsetFakeStore)();
    });
    it('returns 1 if not a stimulus', function () {
      var si = new _SessionItem.default({
        type: 'Item'
      });
      expect(si.displayPositionSlots).to.eq(1);
    });
    it('returns number of nested items in the stimulus', function () {
      var si = new _SessionItem.default({
        stimulus: {},
        stimulusKey: '1',
        quizSessionId: '1'
      });
      expect(si.displayPositionSlots).to.eq(2);
    });
  });
  describe('#getNestedEntries', function () {
    var fakeState = (0, _immutable.fromJS)({
      sessionItems: {
        1: {
          id: '1',
          stimulus: {
            id: '2'
          },
          stimulusKey: '1',
          quizSessionId: '1'
        },
        2: {
          id: '2',
          stimulus: {
            id: '2'
          },
          stimulusKey: '1',
          quizSessionId: '1'
        },
        3: {
          id: '3',
          quizSessionId: '2'
        },
        4: {
          id: '4',
          stimulus: {
            id: '2'
          },
          stimulusKey: '1',
          quizSessionId: '2'
        },
        5: {
          id: '5',
          stimulus: {
            id: '2'
          },
          stimulusKey: '1',
          quizSessionId: '2'
        }
      }
    });
    before(function () {
      (0, _reduxStore.setFakeStore)(fakeState);
    });
    after(function () {
      (0, _reduxStore.unsetFakeStore)();
    });
    it('returns all the SessionItems that have the same stimulusKey and quizSessionId', function () {
      var si = new _SessionItem.default({
        stimulus: {},
        stimulusKey: '1',
        quizSessionId: '1'
      });
      expect(si.getNestedEntries().size).to.eq(2);
    });
    it('falls back to use the stimulus id when stimulusKey is not present', function () {
      var si = new _SessionItem.default({
        stimulus: (0, _immutable.Map)({
          id: '2'
        }),
        quizSessionId: '1'
      });
      expect(si.getNestedEntries().size).to.eq(2);
    });
  });
});