1
0
mirror of synced 2025-02-03 21:59:24 +03:00

only verify when maximumSelectionCount is greater than 0 and add tests

This commit is contained in:
Zubair 2015-01-13 11:34:52 -05:00
parent 25dade52e5
commit 7515fa7f46
7 changed files with 228 additions and 5 deletions

View File

@ -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: {

View File

@ -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: {

View File

@ -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: {

3
dist/js/select2.js vendored
View File

@ -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: {

View File

@ -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: {

View File

@ -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);
});

View File

@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
</div>
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
<script src="maximumSelectionLength-tests.js" type="text/javascript"></script>
</body>
</html>