Browse Source

Tests for the inline search box

This adds tests for the following commits

https://github.com/select2/select2/commit/5f80c5d9f81f3c5398c3e6e3e84fd6c67c8873f1

https://github.com/select2/select2/commit/395e06aff52059f654aff47f03a26cad8955a092
Kevin Brown 9 năm trước cách đây
mục cha
commit
e08222ef15
1 tập tin đã thay đổi với 81 bổ sung0 xóa
  1. 81 0
      tests/selection/search-tests.js

+ 81 - 0
tests/selection/search-tests.js

@@ -9,6 +9,87 @@ var Utils = require('select2/utils');
 
 var options = new Options({});
 
+test('backspace will remove a choice', function (assert) {
+  expect(3);
+
+  var KEYS = require('select2/keys');
+
+  var $container = $('#qunit-fixture .event-container');
+  var container = new MockContainer();
+
+  var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
+
+  var $element = $('#qunit-fixture .multiple');
+  var selection = new CustomSelection($element, options);
+
+  var $selection = selection.render();
+  selection.bind(container, $container);
+
+  // The unselect event should be triggered at some point
+  selection.on('unselect', function () {
+    assert.ok(true, 'A choice was unselected');
+  });
+
+  // Add some selections and render the search
+  selection.update([
+    {
+      id: '1',
+      text: 'One'
+    }
+  ]);
+
+  var $search = $selection.find('input');
+  var $choices = $selection.find('.select2-selection__choice');
+
+  assert.equal($search.length, 1, 'The search was visible');
+  assert.equal($choices.length, 1, 'The choice was rendered');
+
+  // Trigger the backspace on the search
+  var backspace = $.Event('keydown', {
+    which: KEYS.BACKSPACE
+  });
+  $search.trigger(backspace);
+});
+
+test('backspace will set the search text', function (assert) {
+  expect(3);
+
+  var KEYS = require('select2/keys');
+
+  var $container = $('#qunit-fixture .event-container');
+  var container = new MockContainer();
+
+  var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
+
+  var $element = $('#qunit-fixture .multiple');
+  var selection = new CustomSelection($element, options);
+
+  var $selection = selection.render();
+  selection.bind(container, $container);
+
+  // Add some selections and render the search
+  selection.update([
+    {
+      id: '1',
+      text: 'One'
+    }
+  ]);
+
+  var $search = $selection.find('input');
+  var $choices = $selection.find('.select2-selection__choice');
+
+  assert.equal($search.length, 1, 'The search was visible');
+  assert.equal($choices.length, 1, 'The choice was rendered');
+
+  // Trigger the backspace on the search
+  var backspace = $.Event('keydown', {
+    which: KEYS.BACKSPACE
+  });
+  $search.trigger(backspace);
+
+  assert.equal($search.val(), 'One', 'The search text was set');
+});
+
 test('updating selection does not shift the focus', function (assert) {
   // Check for IE 8, which triggers a false negative during testing
   if (window.attachEvent && !window.addEventListener) {