1
0
mirror of synced 2024-11-27 07:16:03 +03:00
select2/tests/data/maximumSelectionLength-tests.js

199 lines
3.5 KiB
JavaScript
Raw Normal View History

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