2015-01-22 03:03:39 +03:00
|
|
|
module('Selection containers - Placeholders - Allow clear');
|
|
|
|
|
|
|
|
var Placeholder = require('select2/selection/placeholder');
|
|
|
|
var AllowClear = require('select2/selection/allowClear');
|
|
|
|
|
|
|
|
var SingleSelection = require('select2/selection/single');
|
|
|
|
|
|
|
|
var $ = require('jquery');
|
|
|
|
var Options = require('select2/options');
|
|
|
|
var Utils = require('select2/utils');
|
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var AllowClearPlaceholder = Utils.Decorate(
|
2015-01-22 03:03:39 +03:00
|
|
|
Utils.Decorate(SingleSelection, Placeholder),
|
|
|
|
AllowClear
|
|
|
|
);
|
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var allowClearOptions = new Options({
|
2015-01-22 03:03:39 +03:00
|
|
|
placeholder: {
|
|
|
|
id: 'placeholder',
|
|
|
|
text: 'This is the placeholder'
|
|
|
|
},
|
|
|
|
allowClear: true
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clear is not displayed for single placeholder', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
|
|
|
$('#qunit-fixture .single-with-placeholder'),
|
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
|
|
|
selection.update([{
|
|
|
|
id: 'placeholder'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$selection.find('.select2-selection__clear').length,
|
|
|
|
0,
|
|
|
|
'The clear icon should not be displayed'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clear is not displayed for multiple placeholder', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
2017-09-26 17:20:08 +03:00
|
|
|
$('#qunit-fixture .multiple'),
|
2015-02-14 07:28:36 +03:00
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
|
|
|
selection.update([]);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$selection.find('.select2-selection__clear').length,
|
|
|
|
0,
|
|
|
|
'The clear icon should not be displayed'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('clear is displayed for placeholder', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
|
|
|
$('#qunit-fixture .single-with-placeholder'),
|
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
|
|
|
selection.update([{
|
|
|
|
id: 'one',
|
|
|
|
test: 'one'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$selection.find('.select2-selection__clear').length,
|
|
|
|
1,
|
|
|
|
'The clear icon should be displayed'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking clear will set the placeholder value', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var $element = $('#qunit-fixture .single-with-placeholder');
|
2015-01-22 03:03:39 +03:00
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
2015-01-22 03:03:39 +03:00
|
|
|
$element,
|
2015-02-14 07:28:36 +03:00
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
2017-09-26 17:20:08 +03:00
|
|
|
selection.bind(container, $('<div></div>'));
|
2015-01-22 03:03:39 +03:00
|
|
|
|
|
|
|
$element.val('One');
|
|
|
|
selection.update([{
|
|
|
|
id: 'One',
|
|
|
|
text: 'One'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
var $remove = $selection.find('.select2-selection__clear');
|
|
|
|
$remove.trigger('mousedown');
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$element.val(),
|
|
|
|
'placeholder',
|
|
|
|
'The value should have been reset to the placeholder'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking clear will trigger the unselect event', function (assert) {
|
2017-09-26 17:20:08 +03:00
|
|
|
assert.expect(4);
|
2015-01-22 03:03:39 +03:00
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var $element = $('#qunit-fixture .single-with-placeholder');
|
2015-01-22 03:03:39 +03:00
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
2015-01-22 03:03:39 +03:00
|
|
|
$element,
|
2015-02-14 07:28:36 +03:00
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
2017-09-26 17:20:08 +03:00
|
|
|
selection.bind(container, $('<div></div>'));
|
2015-01-22 03:03:39 +03:00
|
|
|
|
|
|
|
$element.val('One');
|
|
|
|
selection.update([{
|
|
|
|
id: 'One',
|
|
|
|
text: 'One'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
selection.on('unselect', function (ev) {
|
|
|
|
assert.ok(
|
|
|
|
'data' in ev && ev.data,
|
|
|
|
'The event should have been triggered with the data property'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
$.isPlainObject(ev.data),
|
|
|
|
'The data should be an object'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
ev.data.id,
|
|
|
|
'One',
|
2017-09-26 17:20:08 +03:00
|
|
|
'The data should be the unselected object'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$element.val(),
|
|
|
|
'placeholder',
|
|
|
|
'The previous value should be unselected'
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
var $remove = $selection.find('.select2-selection__clear');
|
|
|
|
$remove.trigger('mousedown');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('preventing the unselect event cancels the clearing', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var $element = $('#qunit-fixture .single-with-placeholder');
|
2015-01-22 03:03:39 +03:00
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
2015-01-22 03:03:39 +03:00
|
|
|
$element,
|
2015-02-14 07:28:36 +03:00
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
2017-09-26 17:20:08 +03:00
|
|
|
selection.bind(container, $('<div></div>'));
|
2015-01-22 03:03:39 +03:00
|
|
|
|
|
|
|
$element.val('One');
|
|
|
|
selection.update([{
|
|
|
|
id: 'One',
|
|
|
|
text: 'One'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
selection.on('unselect', function (ev) {
|
|
|
|
ev.prevented = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
var $remove = $selection.find('.select2-selection__clear');
|
|
|
|
$remove.trigger('mousedown');
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$element.val(),
|
|
|
|
'One',
|
|
|
|
'The placeholder should not have been set'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clear does not work when disabled', function (assert) {
|
2015-02-14 07:28:36 +03:00
|
|
|
var $element = $('#qunit-fixture .single-with-placeholder');
|
2015-01-22 03:03:39 +03:00
|
|
|
|
2015-02-14 07:28:36 +03:00
|
|
|
var selection = new AllowClearPlaceholder(
|
2015-01-22 03:03:39 +03:00
|
|
|
$element,
|
2015-02-14 07:28:36 +03:00
|
|
|
allowClearOptions
|
2015-01-22 03:03:39 +03:00
|
|
|
);
|
|
|
|
var container = new MockContainer();
|
|
|
|
|
|
|
|
var $selection = selection.render();
|
|
|
|
|
2017-09-26 17:20:08 +03:00
|
|
|
selection.bind(container, $('<div></div>'));
|
2015-01-22 03:03:39 +03:00
|
|
|
|
|
|
|
selection.update([{
|
|
|
|
id: 'One',
|
|
|
|
text: 'One'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
$element.val('One');
|
|
|
|
selection.options.set('disabled', true);
|
|
|
|
|
|
|
|
var $remove = $selection.find('.select2-selection__clear');
|
|
|
|
$remove.trigger('mousedown');
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$element.val(),
|
|
|
|
'One',
|
|
|
|
'The placeholder should not have been set'
|
|
|
|
);
|
|
|
|
});
|