Jelajahi Sumber

Add test for focus event on inline search

This adds a test for
https://github.com/select2/select2/commit/02cca7baa7b78e73cdcf393172ee3a54be387167.
Kevin Brown 9 tahun lalu
induk
melakukan
5b207b287e
1 mengubah file dengan 52 tambahan dan 0 penghapusan
  1. 52 0
      tests/selection/search-tests.js

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

@@ -137,3 +137,55 @@ test('updating selection does not shift the focus', function (assert) {
     'The search did not have focus after the selection was updated'
   );
 });
+
+test('the focus event shifts the focus', function (assert) {
+  // Check for IE 8, which triggers a false negative during testing
+  if (window.attachEvent && !window.addEventListener) {
+    // We must expect 0 assertions or the test will fail
+    expect(0);
+    return;
+  }
+
+  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);
+
+  // Update the selection so the search is rendered
+  selection.update([]);
+
+  // Make it visible so the browser can place focus on the search
+  $container.append($selection);
+
+  // The search should not be automatically focused
+
+  var $search = $selection.find('input');
+
+  assert.notEqual(
+    document.activeElement,
+    $search[0],
+    'The search had focus originally'
+  );
+
+  assert.equal($search.length, 1, 'The search was not visible');
+
+  // Focus the container
+
+  container.trigger('focus');
+
+  // Make sure it focuses the search
+
+  assert.equal($search.length, 1, 'The search box disappeared');
+
+  assert.equal(
+    document.activeElement,
+    $search[0],
+    'The search did not have focus originally'
+  );
+});