From 7515fa7f46a243a7f9ff8aab4d2466a050752ec4 Mon Sep 17 00:00:00 2001 From: Zubair Date: Tue, 13 Jan 2015 11:34:52 -0500 Subject: [PATCH] only verify when maximumSelectionCount is greater than 0 and add tests --- dist/js/select2.amd.full.js | 3 +- dist/js/select2.amd.js | 3 +- dist/js/select2.full.js | 3 +- dist/js/select2.js | 3 +- src/js/select2/data/maximumSelectionLength.js | 3 +- tests/data/maximumSelectionLength-tests.js | 199 ++++++++++++++++++ tests/data/maximumSelectionLength.html | 19 ++ 7 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 tests/data/maximumSelectionLength-tests.js create mode 100644 tests/data/maximumSelectionLength.html diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index 7659954b..8fae50a1 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -2867,7 +2867,8 @@ define('select2/data/maximumSelectionLength',[ this.current(function (currentData) { var count = currentData != null ? currentData.length : 0; - if (count >= self.maximumSelectionLength) { + if (self.maximumSelectionLength > 0 && + count >= self.maximumSelectionLength) { self.trigger('results:message', { message: 'maximumSelected', args: { diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index c8cd09ea..008c91c5 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -2867,7 +2867,8 @@ define('select2/data/maximumSelectionLength',[ this.current(function (currentData) { var count = currentData != null ? currentData.length : 0; - if (count >= self.maximumSelectionLength) { + if (self.maximumSelectionLength > 0 && + count >= self.maximumSelectionLength) { self.trigger('results:message', { message: 'maximumSelected', args: { diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 5793d9ae..43965987 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -12402,7 +12402,8 @@ define('select2/data/maximumSelectionLength',[ this.current(function (currentData) { var count = currentData != null ? currentData.length : 0; - if (count >= self.maximumSelectionLength) { + if (self.maximumSelectionLength > 0 && + count >= self.maximumSelectionLength) { self.trigger('results:message', { message: 'maximumSelected', args: { diff --git a/dist/js/select2.js b/dist/js/select2.js index 6cf3d582..ab432c57 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -3295,7 +3295,8 @@ define('select2/data/maximumSelectionLength',[ this.current(function (currentData) { var count = currentData != null ? currentData.length : 0; - if (count >= self.maximumSelectionLength) { + if (self.maximumSelectionLength > 0 && + count >= self.maximumSelectionLength) { self.trigger('results:message', { message: 'maximumSelected', args: { diff --git a/src/js/select2/data/maximumSelectionLength.js b/src/js/select2/data/maximumSelectionLength.js index ebca2f60..ae727529 100644 --- a/src/js/select2/data/maximumSelectionLength.js +++ b/src/js/select2/data/maximumSelectionLength.js @@ -13,7 +13,8 @@ define([ this.current(function (currentData) { var count = currentData != null ? currentData.length : 0; - if (count >= self.maximumSelectionLength) { + if (self.maximumSelectionLength > 0 && + count >= self.maximumSelectionLength) { self.trigger('results:message', { message: 'maximumSelected', args: { diff --git a/tests/data/maximumSelectionLength-tests.js b/tests/data/maximumSelectionLength-tests.js new file mode 100644 index 00000000..dccaf08f --- /dev/null +++ b/tests/data/maximumSelectionLength-tests.js @@ -0,0 +1,199 @@ +module('Data adapters - Maximum selection length'); + +var MaximumSelectionLength = require('select2/data/maximumSelectionLength'); + +var $ = require('jquery'); +var Options = require('select2/options'); +var Utils = require('select2/utils'); + +function StubData () { + this.called = false; + this.currentData = []; +} + +StubData.prototype.current = function (callback) { + callback(this.currentData); +}; + +StubData.prototype.val = function (val) { + this.currentData.push(val); +}; + +StubData.prototype.query = function (params, callback) { + this.called = true; +}; + +var MaximumData = Utils.Decorate(StubData, MaximumSelectionLength); + +test('0 never displays the notice', function (assert) { + var zeroOptions = new Options({ + maximumSelectionLength: 0 + }); + + var data = new MaximumData(null, zeroOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, zeroOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.val('1'); + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, zeroOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.val('1'); + data.val('2'); + + data.query({ + term: '' + }); + + assert.ok(data.called); +}); + +test('< 0 never displays the notice', function (assert) { + var negativeOptions = new Options({ + maximumSelectionLength: -1 + }); + + var data = new MaximumData(null, negativeOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, negativeOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.val('1'); + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, negativeOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.val('1'); + data.val('2'); + + data.query({ + term: '' + }); + + assert.ok(data.called); +}); + +test('triggers when >= 1 selection' , function (assert) { + var maxOfOneOptions = new Options({ + maximumSelectionLength: 1 + }); + var data = new MaximumData(null, maxOfOneOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, maxOfOneOptions); + + data.trigger = function () { + assert.ok(true, 'The event should be triggered.'); + }; + + data.val('1'); + + data.query({ + term: '' + }); + + assert.ok(!data.called); + +}); + +test('triggers when >= 2 selections' , function (assert) { + var maxOfTwoOptions = new Options({ + maximumSelectionLength: 2 + }); + var data = new MaximumData(null, maxOfTwoOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, maxOfTwoOptions); + + data.trigger = function () { + assert.ok(false, 'No events should be triggered'); + }; + + data.val('1'); + + data.query({ + term: '' + }); + + assert.ok(data.called); + + data = new MaximumData(null, maxOfTwoOptions); + + data.trigger = function () { + assert.ok(true, 'The event should be triggered.'); + }; + + data.val('1'); + data.val('2'); + + data.query({ + term: '' + }); + + assert.ok(!data.called); + +}); \ No newline at end of file diff --git a/tests/data/maximumSelectionLength.html b/tests/data/maximumSelectionLength.html new file mode 100644 index 00000000..afb4ec25 --- /dev/null +++ b/tests/data/maximumSelectionLength.html @@ -0,0 +1,19 @@ + + + + + + + +
+
+
+ + + + + + + + +