<commit_before>import {
  closest,
  domify,
  query
} from '../lib';


describe('closest', function() {

  it('should get closest parent', function() {

    var node = domify('<div class="root">' +
                        '<div class="parent">' +
                          '<div class="child"></div>' +
                        '</div>' +
                      '</div>');

    var child = query('.child', node);

    // when
    expect(closest(child, '.root')).to.equal(node);
    expect(closest(node, '.root', true)).to.equal(node);
  });

});<commit_msg>feat(lib): closest Shadow DOM and Document Fragment tests<commit_after>import {
  closest,
  domify,
  query
} from '../lib';


describe('closest', function() {

  it('should get closest parent', function() {

    // given
    var node = domify('<div class="root">' +
                        '<div class="parent">' +
                          '<div class="child"></div>' +
                        '</div>' +
                      '</div>');

    var child = query('.child', node);

    // then
    expect(closest(child, '.root')).to.equal(node);
    expect(closest(node, '.root', true)).to.equal(node);
  });


  it('should work with document fragments', function() {

    // given
    var node = domify('<div class="root">' +
                        '<div class="parent">' +
                          '<div class="child"></div>' +
                        '</div>' +
                      '</div>');
    var child = query('.child', node);

    var fragment = document.createDocumentFragment();
    fragment.appendChild(node);

    // then
    expect(closest(child, '.root')).to.equal(node);
    expect(closest(node, '.root', true)).to.equal(node);
    expect(closest(node, '.outside')).to.be.null;
  });


  // Shadow DOM is not supported by PhantomJS headless browser. In order to
  // execute this test, use 'karma-chrome-launcher' and set the browser to
  // 'Chrome' in karma.conf.js
  (window.HTMLShadowElement ? it : it.skip)('should work with shadow dom', function() {

    //  .root
    //    | -> (shadow - This is not a HTML document nor HTML element)
    //          | -> .innerShadowRoot
    //                    | -> .innerShadowChild

    // given
    var root = createDivWithClassname('root');
    var shadow = root.attachShadow({ mode: 'open' });

    var innerShadowRoot = createDivWithClassname('innerShadowRoot');
    shadow.appendChild(innerShadowRoot);
    var innerShadowChild = createDivWithClassname('innerShadowChild');
    innerShadowRoot.appendChild(innerShadowChild);

    // then
    expect(closest(innerShadowChild, '.innerShadowRoot')).to.equal(innerShadowRoot);
    expect(closest(innerShadowRoot, '.innerShadowRoot', true)).to.equal(innerShadowRoot);
    expect(closest(innerShadowChild, '.outside')).to.be.null;
  });
});

// helper ////
function createDivWithClassname(className) {
  var div = document.createElement('div');
  div.className = className;
  return div;
}
{'fraction_non_alphanumeric': 0.16751097602161433, 'fraction_numerical': 0.0, 'mean_word_length': 2.3431151241534987, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5063210', 'n_tokens_mistral': 861, 'n_tokens_neox': 842, 'n_words': 188}