<commit_before>//
// To fake scrolling in tests, do:
//
//     $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
//
// to bypass event handling and callback throttling.

var $container,
    xsy,
    TARGET_HEIGHT = 20,
    n_changeFired = 0,  // number of times `change` was fired if set up
    countChange = function() { ++n_changeFired; };

test('basic properties are set after init', function() {
  var xsy = new XScrollY({
    targets: '#qunit-fixture li'
  });

  ok(xsy.$scrollElement.length);
  ok(xsy.$targets.length);
  equal(xsy.$targets.length, $('#qunit-fixture li').length);
  equal(xsy.$targets.length, xsy.offsets.length);
});


module('basics', {
  setup: function() {
    n_changeFired = 0;
    $container = $('#qunit-fixture ul');
  }
});

test('change event fires', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0], xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3);
  equal(xsy.$scrollElement.scrollTop(), TARGET_HEIGHT * 3);
  equal(n_changeFired, 1);
});

test('change event fires twice', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
  equal(n_changeFired, 2);
});

test('getActiveOffset', function() {
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li'
  });
  equal(xsy.activeOffset, TARGET_HEIGHT * 0,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT / 2); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT * 0,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT * 2 - 1); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT * 2 + 1); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT * 2,
    "Scroll depth: " + $container.scrollTop());
});

test('scroll', function() {
  var n_scrollFired = 0;
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange,
    scroll: function() {
      ++n_scrollFired;
    }
  });

  equal(n_changeFired, 1);
  equal(n_scrollFired, 1);
  $container.scrollTop(TARGET_HEIGHT / 2); xsy.process();
  equal(n_changeFired, 1);
  equal(n_scrollFired, 2);
  $container.scrollTop(TARGET_HEIGHT); xsy.process();
  equal(n_changeFired, 2);
  equal(n_scrollFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 3 / 2); xsy.process();
  equal(n_changeFired, 2);
  equal(n_scrollFired, 4);
});

test('active property is updated on scroll', function() {
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0], xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.$active[0].innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 2);
});

test('offset option works', function() {
  var xsy = new XScrollY({
    container: $container,
    offset: TARGET_HEIGHT * 3,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal($container.find('li').height(), TARGET_HEIGHT,
    "TARGET_HEIGHT variable should be the same as <li> height for testing");
  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0].innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 1);
});

test('unveil is only called once', function() {
  var n_unveilFired = 0;
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange,
    unveil: function($e) {
      ++n_unveilFired;
    }
  });

  equal(n_changeFired, 1);
  equal(n_unveilFired, 1);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 2);
  equal(n_unveilFired, 2);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 3);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 0); xsy.process();
  equal(n_changeFired, 4);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 5);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 6);
  equal(n_unveilFired, 3);
});


module('target selection', {
  setup: function() {
    $container = $('#qunit-fixture ul');
    xsy = new XScrollY({
      container: $container,
      targets: '#qunit-fixture li'
    });
  }
});

test('visible', function() {
  equal(xsy.visible().text(), '0123456789');
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.visible().text(), '3456789ABC');<commit_msg>add test to verify basic slice functionality<commit_after>//
// To fake scrolling in tests, do:
//
//     $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
//
// to bypass event handling and callback throttling.

var $container,
    xsy,
    TARGET_HEIGHT = 20,
    n_changeFired = 0,  // number of times `change` was fired if set up
    countChange = function() { ++n_changeFired; };

test('basic properties are set after init', function() {
  var xsy = new XScrollY({
    targets: '#qunit-fixture li'
  });

  ok(xsy.$scrollElement.length);
  ok(xsy.$targets.length);
  equal(xsy.$targets.length, $('#qunit-fixture li').length);
  equal(xsy.$targets.length, xsy.offsets.length);
});


module('basics', {
  setup: function() {
    n_changeFired = 0;
    $container = $('#qunit-fixture ul');
  }
});

test('change event fires', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0], xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3);
  equal(xsy.$scrollElement.scrollTop(), TARGET_HEIGHT * 3);
  equal(n_changeFired, 1);
});

test('change event fires twice', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
  equal(n_changeFired, 2);
});

test('getActiveOffset', function() {
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li'
  });
  equal(xsy.activeOffset, TARGET_HEIGHT * 0,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT / 2); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT * 0,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT * 2 - 1); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT,
    "Scroll depth: " + $container.scrollTop());
  $container.scrollTop(TARGET_HEIGHT * 2 + 1); xsy.process();
  equal(xsy.activeOffset, TARGET_HEIGHT * 2,
    "Scroll depth: " + $container.scrollTop());
});

test('scroll', function() {
  var n_scrollFired = 0;
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange,
    scroll: function() {
      ++n_scrollFired;
    }
  });

  equal(n_changeFired, 1);
  equal(n_scrollFired, 1);
  $container.scrollTop(TARGET_HEIGHT / 2); xsy.process();
  equal(n_changeFired, 1);
  equal(n_scrollFired, 2);
  $container.scrollTop(TARGET_HEIGHT); xsy.process();
  equal(n_changeFired, 2);
  equal(n_scrollFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 3 / 2); xsy.process();
  equal(n_changeFired, 2);
  equal(n_scrollFired, 4);
});

test('active property is updated on scroll', function() {
  xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0], xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.$active[0].innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 2);
});

test('offset option works', function() {
  var xsy = new XScrollY({
    container: $container,
    offset: TARGET_HEIGHT * 3,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal($container.find('li').height(), TARGET_HEIGHT,
    "TARGET_HEIGHT variable should be the same as <li> height for testing");
  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.$active[0].innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 1);
});

test('unveil is only called once', function() {
  var n_unveilFired = 0;
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange,
    unveil: function($e) {
      ++n_unveilFired;
    }
  });

  equal(n_changeFired, 1);
  equal(n_unveilFired, 1);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 2);
  equal(n_unveilFired, 2);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 3);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 0); xsy.process();
  equal(n_changeFired, 4);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 5);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 6);
  equal(n_unveilFired, 3);
});


module('target selection', {
  setup: function() {
    $container = $('#qunit-fixture ul');
    xsy = new XScrollY({
      container: $container,
      targets: '#qunit-fixture li'
    });
  }
});

test('slice -  slicing at the limits returns all', function() {
  total = xsy.$targets.length;
  equal(xsy.slice(-Infinity, Infinity).length, total);
  equal(xsy.slice(0, Infinity).length, total);
});

test('visible', function() {
  equal(xsy.visible().text(), '0123456789');
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.visible().text(), '3456789ABC');
{'fraction_non_alphanumeric': 0.1621146190167577, 'fraction_numerical': 0.01546153133969077, 'mean_word_length': 4.215837759536456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '189006', 'n_tokens_mistral': 3946, 'n_tokens_neox': 3763, 'n_words': 825}