1
0
mirror of synced 2024-12-01 00:56:03 +03:00
select2/tests/data/maximumSelectionLength-tests.js
Kevin Brown 2fce8ae6c4
Fix maximumSelectionLength being ignored by closeOnSelect (#5581)
* Rewrote maximumSelectionLength tests to use container

These brings the tests in line with other tests which we have, and
makes it easier to understand what is actually going on in the tests.

This also removes a redundant set of tests where we were testing with
=> 2 options being allowed. There are no current edge cases that would
have required this.

* Fix maximumSelectionLength being ignored by closeOnSelect

There was a bug where the `maximumSelectionLength` option would not
kick in if the `closeOnSelect` option was enabled. Normally, this
was enabled by someone in their global configuration, but it could
also be seen when somoene selected an option while holding the
meta/ctrl/alt keys. This would implicitly enable the `closeOnSelect`
behaviour, even when it was not globally enabled, and cause the bug.

This fixes that issue by listening to the `select` event which is
triggered whenever an option is selected, and triggers the "maximum
selected" message based on that event. This should now force the
message to be displayed, even when the results did not have to be
queried another time.

Fixes #3514
Fixes #3860
Closes #5333
2019-07-21 15:44:37 -04:00

153 lines
3.2 KiB
JavaScript

module('Data adapters - Maximum selection length');
var SelectData = require('select2/data/select');
var MaximumSelectionLength = require('select2/data/maximumSelectionLength');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
var MaximumSelectionData = Utils.Decorate(SelectData, MaximumSelectionLength);
test('0 never displays the notice', function (assert) {
assert.expect(3);
var $select = $('#qunit-fixture .multiple');
var zeroOptions = new Options({
maximumSelectionLength: 0
});
var container = new MockContainer();
var data = new MaximumSelectionData($select, zeroOptions);
data.bind(container, null);
data.on('results:message', function () {
assert.ok(false, 'The message should not be displayed');
});
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
$select.val(['One']);
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
$select.val(['One', 'Two']);
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
});
test('< 0 never displays the notice', function (assert) {
assert.expect(3);
var $select = $('#qunit-fixture .multiple');
var negativeOptions = new Options({
maximumSelectionLength: -1
});
var container = new MockContainer();
var data = new MaximumSelectionData($select, negativeOptions);
data.bind(container, null);
data.on('results:message', function () {
assert.ok(false, 'The message should not be displayed');
});
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
$select.val(['One']);
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
$select.val(['One', 'Two']);
data.query({
term: ''
}, function () {
assert.ok(true, 'The results should be queried');
});
});
test('triggers when >= 1 selection' , function (assert) {
assert.expect(2);
var $select = $('#qunit-fixture .multiple');
var maxOfOneOptions = new Options({
maximumSelectionLength: 1
});
var container = new MockContainer();
var data = new MaximumSelectionData($select, maxOfOneOptions);
data.bind(container, null);
data.on('results:message', function () {
assert.ok(true, 'The message should be displayed');
});
$select.val(['One']);
data.query({
term: ''
}, function () {
assert.ok(false, 'The results should not be queried');
});
$select.val(['One', 'Two']);
data.query({
term: ''
}, function () {
assert.ok(false, 'The results should not be queried');
});
});
test('triggers after selection' , function (assert) {
assert.expect(1);
var $select = $('#qunit-fixture .multiple');
var maxOfOneOptions = new Options({
maximumSelectionLength: 1
});
var container = new MockContainer();
var data = new MaximumSelectionData($select, maxOfOneOptions);
data.bind(container, null);
data.on('results:message', function () {
assert.ok(true, 'The message should be displayed');
});
$select.val(['One']);
container.trigger('select', {
data: {}
});
});