"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("../../build/enzyme");

var _Highlighter = require("./Highlighter");

var _Store = _interopRequireDefault(require("../data/Store"));

describe('Highlighter', function () {
  var opts = {
    highlightRegexp: /highlight/gi,
    filterRegexp: /found/gi,
    highlight: function highlight(text) {
      return "<a>" + text + "</a>";
    }
  };
  it('check default emphazis', function () {
    var instance = (0, _enzyme.mount)(_react.default.createElement(_Highlighter.Highlighter, null)).instance();
    expect(instance.emphazis('matchedText')).toBe('<strong>matchedText</strong>');
  });
  it('check alternate emphazis', function () {
    var instance = (0, _enzyme.mount)(_react.default.createElement(_Highlighter.Highlighter, {
      listHighlightType: _Highlighter.HIGHLIGHT_TYPE.BACKGROUND
    })).instance();
    expect(instance.emphazis('matchedText')).toBe('<span class="rc-highlight">matchedText</span>');
  });
  it('check custom emphazis', function () {
    var instance = (0, _enzyme.mount)(_react.default.createElement(_Highlighter.Highlighter, {
      highlight: opts.highlight
    })).instance();
    expect(instance.emphazis('matchedText')).toBe('<a>matchedText</a>');
  });
  it('check default render', function () {
    var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, null, "The child text"));
    expect(rendered.html()).toBe('<span>The child text</span>');
  });
  it('check default render with title', function () {
    var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, {
      title: 'The Title'
    }, "The child text"));
    expect(rendered.html()).toBe('<span title="The Title">The child text</span>');
  });
  it('check render with rich text', function () {
    var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, opts, 'text <i>highlight</i> <b>found</b>'));
    expect(rendered.html()).toBe('<span>text <i><a>highlight</a></i> <b>found</b></span>');
  });
  it('should not highlight, becouse child has not been found', function () {
    var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, opts, "highlight"));
    expect(rendered.html()).toBe('<span>highlight</span>');
  });
  it('should highlight, becouse child has been found', function () {
    var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, opts, "highlight found"));
    expect(rendered.html()).toBe('<span><a>highlight</a> found</span>');
  });
  describe('Extra highlight cases', function () {
    it('should highlight escaped ampersand', function () {
      var newOpts = Object.assign({}, opts, {
        filterRegexp: /&/gi,
        highlightRegexp: /&/gi
      });
      var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, newOpts, "highlight & found"));
      expect(rendered.html()).toBe('<span>highlight <a>&amp;</a> found</span>');
    });
    it('should highlight escaped double quotes', function () {
      var newOpts = Object.assign({}, opts, {
        filterRegexp: /"/gi,
        highlightRegexp: /"/gi
      });
      var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, newOpts, "highlight \"found\""));
      expect(rendered.html()).toBe('<span>highlight <a>&quot;</a>found<a>&quot;</a></span>');
    });
    it('should not highlight specifically marked text', function () {
      var newOpts = Object.assign({}, opts, {
        filterRegexp: /high/gi,
        highlightRegexp: /high/gi
      });
      var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, newOpts, 'text highlight <nhl>highlight</nhl>'));
      expect(rendered.html()).toBe('<span>text <a>high</a>light <nhl>highlight</nhl></span>');
    });
  });
  describe('Default Store regexps', function () {
    it('should work too', function () {
      var newOpts = Object.assign({}, opts, {
        filterRegexp: _Store.default.searchStringToRegExp('text to match'),
        highlightRegexp: _Store.default.getHighlightRegExp('text to match')
      });
      var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, newOpts, "highlight found in text to match"));
      expect(rendered.html()).toBe('<span>highlight found in <a>text</a> <a>to</a> <a>match</a></span>');
    });
    it('should work on rich text', function () {
      var newOpts = Object.assign({}, opts, {
        filterRegexp: _Store.default.searchStringToRegExp('text to match'),
        highlightRegexp: _Store.default.getHighlightRegExp('text to match')
      });
      var rendered = (0, _enzyme.render)(_react.default.createElement(_Highlighter.Highlighter, newOpts, 'highlight found in text <b>to</b> match'));
      expect(rendered.html()).toBe('<span>highlight found in <a>text</a> <b><a>to</a></b> <a>match</a></span>');
    });
  });
});