From acd33061925973c683dc5c00114ec4a020869cff Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 14 May 2016 21:04:03 -0400 Subject: [PATCH] Added tests for focusing results This adds three tests related to focusing selected items in the results. The tests are for e897d00 and 9f58128. --- tests/results/focusing-tests.js | 138 ++++++++++++++++++++++++++++++++ tests/unit.html | 2 + 2 files changed, 140 insertions(+) create mode 100644 tests/results/focusing-tests.js diff --git a/tests/results/focusing-tests.js b/tests/results/focusing-tests.js new file mode 100644 index 00000000..e75068a7 --- /dev/null +++ b/tests/results/focusing-tests.js @@ -0,0 +1,138 @@ +module('Results - highlighting results'); + +test('results:all with no data skips results:focus', function (assert) { + expect(0); + + var $ = require('jquery'); + + var $select = $(''); + var $parent = $('
'); + + var $container = $(''); + var container = new MockContainer(); + + $parent.appendTo($('#qunit-fixture')); + $select.appendTo($parent); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Results = require('select2/results'); + + var results = new Results($select, new Options({})); + + // Fake the data adapter for the `setClasses` method + results.data = {}; + results.data.current = function (callback) { + callback([{ id: 'test' }]); + }; + + results.render(); + + results.bind(container, $container); + + results.on('results:focus', function (params) { + assert.ok(false, 'The results:focus event was triggered'); + }); + + container.trigger('results:all', { + data: { + results: [] + } + }); +}); + +test('results:all triggers results:focus on the first item', function (assert) { + expect(2); + + var $ = require('jquery'); + + var $select = $(''); + var $parent = $('
'); + + var $container = $(''); + var container = new MockContainer(); + + $parent.appendTo($('#qunit-fixture')); + $select.appendTo($parent); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Results = require('select2/results'); + + var results = new Results($select, new Options({})); + + // Fake the data adapter for the `setClasses` method + results.data = {}; + results.data.current = function (callback) { + callback([{ id: 'test' }]); + }; + + results.render(); + + results.bind(container, $container); + + results.on('results:focus', function (params) { + assert.equal(params.data.id, 'test'); + assert.equal(params.data.text, 'Test'); + }); + + container.trigger('results:all', { + data: { + results: [ + { + id: 'test', + text: 'Test' + } + ] + } + }); +}); + +test('results:append does not trigger results:focus', function (assert) { + expect(0); + + var $ = require('jquery'); + + var $select = $(''); + var $parent = $('
'); + + var $container = $(''); + var container = new MockContainer(); + + $parent.appendTo($('#qunit-fixture')); + $select.appendTo($parent); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Results = require('select2/results'); + + var results = new Results($select, new Options({})); + + // Fake the data adapter for the `setClasses` method + results.data = {}; + results.data.current = function (callback) { + callback([{ id: 'test' }]); + }; + + results.render(); + + results.bind(container, $container); + + results.on('results:focus', function () { + assert.ok(false, 'The results:focus event was triggered'); + }); + + container.trigger('results:append', { + data: { + results: [ + { + id: 'test', + text: 'Test' + } + ] + } + }); +}); \ No newline at end of file diff --git a/tests/unit.html b/tests/unit.html index 67e18fa7..6756e584 100644 --- a/tests/unit.html +++ b/tests/unit.html @@ -79,6 +79,8 @@ + +