"use strict";

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

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

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

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

var _presenter = _interopRequireWildcard(require("../presenter"));

var styles = {
  componentId: 'owVFv',
  template: function template(theme) {
    return "\n\n.Distribution__styles__bar {\n  fill: ".concat(theme.barFillColor || 'inherit', ";\n}\n\n.Distribution__styles__barStar {\n  fill: ").concat(theme.barStarFillColor || 'inherit', ";\n}\n\n.Distribution__styles__barContainer {\n}\n\n.Distribution__styles__xAxis {\n  color: ").concat(theme.xAxisColor || 'inherit', ";\n}\n\n.Distribution__styles__barLabel {\n  color: ").concat(theme.barLabelColor || 'inherit', ";\n  font-family: ").concat(theme.barLabelFontFamily || 'inherit', ";\n  font-size: ").concat(theme.barLabelFontSize || 'inherit', ";\n  text-anchor: middle;\n}\n");
  },
  'bar': 'Distribution__styles__bar',
  'barStar': 'Distribution__styles__barStar',
  'barContainer': 'Distribution__styles__barContainer',
  'xAxis': 'Distribution__styles__xAxis',
  'barLabel': 'Distribution__styles__barLabel'
};
describe('Distribution', function () {
  var testbed = createTestbed(_presenter.default, {
    animateDelay: null,
    animateDuration: null,
    chartHeight: 120,
    chartWidth: 200,
    visualDistribution: [0, 1, 2, 3]
  });

  var getBars = function getBars() {
    return testbed.findChildrenBySelector(".".concat(styles.barContainer, " rect"));
  };

  shouldRenderComponent(testbed, {}, _presenter.default);
  it('does not render a star by default', function () {
    testbed.render();
    var bars = testbed.findChildrenByClass(styles.barStar);
    expect(bars).to.have.length(0);
  });
  it('should render bars with relative height', function () {
    testbed.render();
    var bars = getBars();
    expect(bars.map(function (b) {
      return b.attributes.y.value;
    })).to.eql(['120', '80', '40', '0']);
    expect(bars.map(function (b) {
      return b.attributes.height.value;
    })).to.eql(['0', '40', '80', '120']);
  });
  it('should determine number of bands from distribution', function () {
    testbed.render({
      barPaddingInner: 0
    });
    var bars = getBars();
    expect(bars.length).to.eq(4);
    bars.forEach(function (b) {
      expect(b.attributes.width.value).to.eq('50');
    });
  });
  context('with star index', function () {
    it('styles starred bar', function () {
      testbed.render({
        starIndex: 1
      });
      var bars = testbed.findChildrenBySelector(".".concat(styles.barContainer, " rect"));
      expect(bars.findIndex(function (b) {
        return b.className.baseVal.includes(styles.barStar);
      })).to.eq(1);
    });
    it('renders the star', function () {
      testbed.render({
        starIndex: 1
      });
      var stars = testbed.findChildrenBySelector("path[d=\"".concat(_presenter.starPathData, "\""));
      expect(stars).to.have.length(1);
    });
  });
  describe('summary', function () {
    var srText = function srText() {
      var srNode = (0, _reactDom.findDOMNode)(testbed.findChildByType(_ScreenReaderContent.default));
      return testbed.getText(srNode);
    };

    it('should render', function () {
      testbed.render();
      var text = srText();
      expect(text).to.include(' 1 student scored between 25% and 50%.');
      expect(text).to.include(' 2 students scored between 50% and 75%.');
      expect(text).to.match(/ 3 students scored between 75% and 100%\.$/);
    });
    it('should not render for nonexistent distribution data', function () {
      testbed.render({
        summaryDistribution: []
      });
      expect(srText()).to.eq('');
    });
    it('should not render for empty distribution data', function () {
      testbed.render({
        summaryDistribution: [0, 0, 0]
      });
      expect(srText()).to.eq('');
    });
    it('should not render for insufficient distribution data', function () {
      testbed.render({
        summaryDistribution: [100]
      });
      expect(srText()).to.eq('');
    });
    it('should calculate average', function () {
      testbed.render({
        summaryDistribution: [2, 1, 2]
      });
      expect(srText()).to.match(/^3 students scored above or at the average, and 2 below\./);
    });
    it('should calculate average for low scores', function () {
      testbed.render({
        summaryDistribution: [3, 0, 0]
      });
      expect(srText()).to.match(/^3 students scored above or at the average, and 0 below\./);
    });
    it('should calculate average for high scores', function () {
      testbed.render({
        summaryDistribution: [0, 0, 3]
      });
      expect(srText()).to.match(/^3 students scored above or at the average, and 0 below\./);
    });
  });
});