"use strict";

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _immutable = require("immutable");

var _ToggleDetails = _interopRequireDefault(require("@instructure/ui-toggle-details/lib/components/ToggleDetails"));

var _multiple_choice = _interopRequireDefault(require("quiz-interactions/lib/records/interactions/multiple_choice"));

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

var _QuizSessionEvent = _interopRequireDefault(require("../../../../common/records/QuizSessionEvent"));

var _ResponseEvent = _interopRequireDefault(require("../ResponseEvent"));

describe('ResponseEvent', function () {
  var item = new _Item.default((0, _immutable.fromJS)({
    interactionData: {
      choices: [{
        id: '1',
        itemBody: '<p>hey</p>'
      }, {
        id: '2',
        itemBody: '<div>ho</div>'
      }, {
        id: '3',
        itemBody: '<h1>lets go</h1>'
      }]
    }
  }));
  sinon.stub(item, 'getInteractionType').returns(new _multiple_choice.default());

  var eventWithResponseVal = function eventWithResponseVal() {
    var userResponseVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '1';
    var event = new _QuizSessionEvent.default((0, _immutable.fromJS)({
      id: '22',
      createdAt: '2016-09-28T18:45:27.700Z|232982772',
      eventType: 'RESPONSE',
      eventData: {
        userResponse: {
          value: userResponseVal
        }
      }
    }));
    sinon.stub(event, 'getItem').returns(item);
    return event;
  };

  var testbed = createTestbed(_ResponseEvent.default, {
    event: eventWithResponseVal(),
    item: item
  });

  var toggle = function toggle() {
    var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

    var toggle = _reactDom.default.findDOMNode(testbed.findChildrenByType(_ToggleDetails.default)[index]);

    _testUtils.default.Simulate.click(toggle.querySelector('button'));
  };

  shouldRenderComponent(testbed, {}, _ResponseEvent.default);
  it('should render a ResponseEvent', function () {
    // testrail test_id:  C2954598, C2954599
    testbed.render();
    var responseEventComponent = testbed.findChildrenByType(_ResponseEvent.default)[0];
    expect(responseEventComponent).to.not.be.undefined();
  });
  describe('ResponseEvent text', function () {
    // testrail test_id:  C2954598, C2954599
    it('should include selected choice', function () {
      var event = eventWithResponseVal('1');
      testbed.render({
        event: event
      });
      toggle();
      var text = testbed.getText();
      expect(text).to.include('hey');
      expect(text).to.not.include('lets go');
    });
    describe('with HTML in question stem', function () {
      var HTMLItem = new _Item.default((0, _immutable.fromJS)({
        itemBody: "<p>hi</p><img src='images/test.png'/>",
        interactionData: {
          choices: [{
            id: '1',
            itemBody: '<p>hey</p>'
          }, {
            id: '2',
            itemBody: '<div>ho</div>'
          }, {
            id: '3',
            itemBody: '<h1>lets go</h1>'
          }]
        }
      }));
      sinon.stub(HTMLItem, 'getInteractionType').returns(new _multiple_choice.default());
      it('should sanitize HTML in question stem', function () {
        var event = eventWithResponseVal(['1', '2', '3']);
        testbed.render({
          event: event,
          item: HTMLItem
        });
        var text = testbed.getText(testbed.rootNode);
        expect(text).to.include('hi');
        expect(text).to.not.include('<p>');
      });
      it('should include media toggle', function () {
        var event = eventWithResponseVal(['1', '2', '3']);
        testbed.render({
          event: event,
          item: HTMLItem
        });
        toggle();
        expect(testbed.getText()).to.include('Included Media');
      });
      it('should include embedded image', function () {
        var event = eventWithResponseVal('1');
        testbed.render({
          event: event,
          item: HTMLItem
        });
        toggle();
        toggle(1);
        expect(testbed.rootNode.innerHTML).to.include('img src="images/test.png"');
      });
    });
  });
});