"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

var _immutable = require("immutable");

var _tagAssociations = _interopRequireDefault(require("../tagAssociations"));

var actions = _interopRequireWildcard(require("../../actions/tagAssociations"));

var _items = require("../../actions/items");

var _stimuli = require("../../actions/stimuli");

describe('tag associations reducer', function () {
  var initialState = (0, _immutable.fromJS)({
    Item: {
      2: [{
        id: '99',
        tag_value: 'boo'
      }, {
        id: '100',
        tag_value: 'moo'
      }]
    },
    pendingAddition: {
      Item: {
        1: [{
          tag_value: 'tag-1'
        }]
      }
    },
    pendingDeletion: {
      Item: {
        1: [{
          id: '1'
        }]
      }
    }
  });
  describe('addTagAssociation', function () {
    it('creates new tag association list', function () {
      var action = actions.addTagAssociation('Item', '1', {
        id: '101',
        tag_value: 'zoo'
      });
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Item', '1']).size).to.eq(1);
    });
    it('append tag association to existing list', function () {
      var action = actions.addTagAssociation('Item', '2', {
        id: '101',
        tag_value: 'zoo'
      });
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Item', '2']).size).to.eq(3);
    });
  });
  describe('removePendingAdditionTagAssociation', function () {
    it('removes a pending addition tag association', function () {
      var action = actions.removePendingAdditionTagAssociation('Item', '1', 'tag-1');
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['pendingAddition', 'Item', '1']).size).to.eq(0);
    });
  });
  describe('removePendingDeletionTagAssociation', function () {
    it('removes a pending deletion tag association', function () {
      var action = actions.removePendingDeletionTagAssociation('Item', '1', '1');
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['pendingDeletion', 'Item', '1']).size).to.eq(0);
    });
  });
  describe('removeTagAssociation', function () {
    it('removes given tag', function () {
      var action = actions.removeTagAssociation('Item', '2', '99');
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Item', '2']).size).to.eq(1);
      expect(nextState.getIn(['Item', '2', 0, 'id'])).to.eq('100');
    });
    it('removes no tag', function () {
      var action = actions.removeTagAssociation('Item', '2', '191');
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Item', '2']).size).to.eq(2);
    });
  });
  describe('setPendingAdditionTagAssociations', function () {
    it('sets pending addition tag associations', function () {
      var action = actions.setPendingAdditionTagAssociations('Item', '2', [{
        id: '99',
        tag_value: 'boo'
      }]);
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['pendingAddition', 'Item', '2']).size).to.eq(1);
    });
  });
  describe('setPendingDeletionTagAssociations', function () {
    it('sets pending tag deletions', function () {
      var action = actions.setPendingDeletionTagAssociations('Item', '2', [{
        id: '99',
        tag_value: 'boo'
      }]);
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['pendingDeletion', 'Item', '2']).size).to.eq(1);
    });
  });
  describe('setTags', function () {
    it('sets item tags', function () {
      var action = (0, _items.addItems)([{
        id: '1',
        tagAssociations: [{
          id: '2',
          tagValue: 'blue',
          tagId: '3'
        }]
      }, {
        id: '2',
        tagAssociations: [{
          id: '4',
          tagValue: 'redx',
          tagId: '5'
        }]
      }]);
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Item', '1']).toJS()).to.deep.eq([{
        id: '2',
        tag_value: 'blue',
        tag_id: '3'
      }]);
      expect(nextState.getIn(['Item', '2']).toJS()).to.deep.eq([{
        id: '4',
        tag_value: 'redx',
        tag_id: '5'
      }]);
    });
    it('sets stimulus tags', function () {
      var action = (0, _stimuli.addStimuli)([{
        id: '1',
        tagAssociations: [{
          id: '2',
          tagValue: 'blue',
          tagId: '3'
        }]
      }, {
        id: '2',
        tagAssociations: [{
          id: '4',
          tagValue: 'redx',
          tagId: '5'
        }]
      }]);
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['Stimulus', '1']).toJS()).to.deep.eq([{
        id: '2',
        tag_value: 'blue',
        tag_id: '3'
      }]);
      expect(nextState.getIn(['Stimulus', '2']).toJS()).to.deep.eq([{
        id: '4',
        tag_value: 'redx',
        tag_id: '5'
      }]);
    });
  });
  describe('setTagInput', function () {
    it('sets the tag input', function () {
      var action = actions.setTagInput('Item', '1', 'hello');
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState.getIn(['tagInput', 'Item', '1'])).to.eq('hello');
    });
  });
  describe('setTagSuggestions', function () {
    it('sets tag suggestions', function () {
      var inputAction = actions.setTagInput('Item', '1', 'hi');
      var startState = (0, _tagAssociations.default)(initialState, inputAction);
      var action = actions.setTagSuggestions('Item', '1', [], 'hi');
      var nextState = (0, _tagAssociations.default)(startState, action);
      expect(nextState.getIn(['tagSuggestions', 'Item', '1'])).to.eq((0, _immutable.List)());
    });
    it('does not set tag suggestions since tag input has changed', function () {
      var inputAction = actions.setTagInput('Item', '1', 'hig');
      var startState = (0, _tagAssociations.default)(initialState, inputAction);
      var action = actions.setTagSuggestions('Item', '1', [], 'hi');
      var nextState = (0, _tagAssociations.default)(startState, action);
      expect(nextState.getIn(['tagSuggestions', 'Item', '1'])).to.be.undefined();
    });
  });
  describe('default', function () {
    it('does not update state', function () {
      var action = {
        action: 'IRRELEVANT_ACTION',
        payload: []
      };
      var nextState = (0, _tagAssociations.default)(initialState, action);
      expect(nextState).to.eq(initialState);
    });
  });
});