| 1 |
describe("multiSelect", function() { |
| 2 |
|
| 3 |
describe('init', function(){ |
| 4 |
it ('should be chainable', function(){ |
| 5 |
select.multiSelect().addClass('chainable'); |
| 6 |
expect(select.hasClass('chainable')).toBeTruthy(); |
| 7 |
}); |
| 8 |
describe('without options', function(){ |
| 9 |
|
| 10 |
beforeEach(function() { |
| 11 |
select.multiSelect(); |
| 12 |
msContainer = select.next(); |
| 13 |
}); |
| 14 |
|
| 15 |
it('should hide the original select', function(){ |
| 16 |
expect(select.css('position')).toBe('absolute'); |
| 17 |
expect(select.css('left')).toBe('-9999px'); |
| 18 |
}); |
| 19 |
|
| 20 |
it('should create a container', function(){ |
| 21 |
expect(msContainer).toBe('div.ms-container'); |
| 22 |
}); |
| 23 |
|
| 24 |
it ('should create a selectable and a selection container', function(){ |
| 25 |
expect(msContainer).toContain('div.ms-selectable, div.ms-selection'); |
| 26 |
}); |
| 27 |
|
| 28 |
it ('should create a list for both selectable and selection container', function(){ |
| 29 |
expect(msContainer).toContain('div.ms-selectable ul.ms-list, div.ms-selection ul.ms-list'); |
| 30 |
}); |
| 31 |
|
| 32 |
it ('should populate the selectable list', function(){ |
| 33 |
expect($('.ms-selectable ul.ms-list li').length).toEqual(10); |
| 34 |
}); |
| 35 |
|
| 36 |
it ('should populate the selection list', function(){ |
| 37 |
expect($('.ms-selectable ul.ms-list li').length).toEqual(10); |
| 38 |
}); |
| 39 |
|
| 40 |
}); |
| 41 |
|
| 42 |
describe('with pre-selected options', function(){ |
| 43 |
|
| 44 |
var selectedValues = []; |
| 45 |
|
| 46 |
beforeEach(function() { |
| 47 |
var firstOption = select.children('option').first(); |
| 48 |
var lastOption = select.children('option').last(); |
| 49 |
firstOption.prop('selected', true); |
| 50 |
lastOption.prop('selected', true); |
| 51 |
selectedValues.push(firstOption.val(), lastOption.val()); |
| 52 |
select.multiSelect(); |
| 53 |
msContainer = select.next(); |
| 54 |
}); |
| 55 |
|
| 56 |
it ('should select the pre-selected options', function(){ |
| 57 |
$.each(selectedValues, function(index, value){ |
| 58 |
expect($('.ms-selectable ul.ms-list li#'+sanitize(value)+'-selectable')).toBe('.ms-selected'); |
| 59 |
}); |
| 60 |
expect($('.ms-selectable ul.ms-list li.ms-selected').length).toEqual(2); |
| 61 |
}); |
| 62 |
}); |
| 63 |
|
| 64 |
describe("with disabled pre-selected options", function(){ |
| 65 |
var selectedValues = ['value1', 'value2', 'value3']; |
| 66 |
|
| 67 |
beforeEach(function() { |
| 68 |
$('#multi-select').find('option') |
| 69 |
.first().prop('selected', true).prop('disabled', true) |
| 70 |
.next().prop('selected', true) |
| 71 |
.next().prop('selected', true).prop('disabled', true) |
| 72 |
; |
| 73 |
$('#multi-select').multiSelect(); |
| 74 |
}) |
| 75 |
|
| 76 |
it ('should select the disabled pre-selected options', function(){ |
| 77 |
$.each(selectedValues, function(index, value){ |
| 78 |
expect($('.ms-selectable ul.ms-list li#'+sanitize(value)+'-selectable')).toBe('.ms-selected'); |
| 79 |
}); |
| 80 |
expect($('.ms-selectable ul.ms-list li.ms-selected').length).toEqual(3); |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
describe("with disabled non-selected options", function(){ |
| 85 |
var selectedValues = ['value1', 'value3']; |
| 86 |
|
| 87 |
beforeEach(function() { |
| 88 |
$('#multi-select').find('option') |
| 89 |
.first().prop('selected', true) |
| 90 |
.next().prop('disabled', true) |
| 91 |
.next().prop('selected', true) |
| 92 |
; |
| 93 |
$('#multi-select').multiSelect(); |
| 94 |
}) |
| 95 |
|
| 96 |
it ('should not select the disabled non-selected options', function(){ |
| 97 |
$.each(selectedValues, function(index, value){ |
| 98 |
expect($('.ms-selectable ul.ms-list li#'+sanitize(value)+'-selectable')).toBe('.ms-selected'); |
| 99 |
}); |
| 100 |
expect($('.ms-selectable ul.ms-list li.ms-selected').length).toEqual(2); |
| 101 |
}); |
| 102 |
}); |
| 103 |
}); |
| 104 |
|
| 105 |
describe('destroy', function(){ |
| 106 |
|
| 107 |
describe('destroy multi select', function(){ |
| 108 |
beforeEach(function(){ |
| 109 |
select.multiSelect(); |
| 110 |
msContainer = select.next(); |
| 111 |
select.multiSelect('destroy'); |
| 112 |
}); |
| 113 |
|
| 114 |
it('should show the original select', function(){ |
| 115 |
expect(select.css('position')).not.toBe('absolute'); |
| 116 |
expect(select.css('left')).not.toBe('-9999px'); |
| 117 |
}); |
| 118 |
|
| 119 |
it('should destroy the multiSelect container', function(){ |
| 120 |
expect(select.next().size()).toEqual(0); |
| 121 |
}); |
| 122 |
}); |
| 123 |
}); |
| 124 |
|
| 125 |
describe('optgroup', function(){ |
| 126 |
var optgroupMsContainer, optgroupSelect, optgroupLabels; |
| 127 |
|
| 128 |
beforeEach(function() { |
| 129 |
$('<select id="multi-select-optgroup" multiple="multiple" name="testy[]"></select>').appendTo('body'); |
| 130 |
for (var o=1; o <= 10; o++) { |
| 131 |
var optgroup = $('<optgroup label="opgroup'+o+'"></optgroup>') |
| 132 |
for (var i=1; i <= 10; i++) { |
| 133 |
var value = i + (o * 10); |
| 134 |
$('<option value="value'+value+'">text'+value+'</option>').appendTo(optgroup); |
| 135 |
}; |
| 136 |
optgroup.appendTo($("#multi-select-optgroup")); |
| 137 |
} |
| 138 |
optgroupSelect = $("#multi-select-optgroup"); |
| 139 |
}); |
| 140 |
|
| 141 |
describe('init', function(){ |
| 142 |
describe('with selectableOptgroup option set to false', function(){ |
| 143 |
beforeEach(function(){ |
| 144 |
optgroupSelect.multiSelect({ selectableOptgroup: false }); |
| 145 |
optgroupMsContainer = optgroupSelect.next(); |
| 146 |
optgroupLabels = optgroupMsContainer.find('.ms-selectable .ms-optgroup-label'); |
| 147 |
}); |
| 148 |
|
| 149 |
it ('sould display all optgroups', function(){ |
| 150 |
expect(optgroupLabels.length).toEqual(10); |
| 151 |
}); |
| 152 |
|
| 153 |
it ('should do nothing when clicking on optgroup', function(){ |
| 154 |
var clickedOptGroupLabel = optgroupLabels.first(); |
| 155 |
clickedOptGroupLabel.trigger('click'); |
| 156 |
expect(optgroupSelect.val()).toBeNull(); |
| 157 |
}); |
| 158 |
}); |
| 159 |
|
| 160 |
describe('with selectableOptgroup option set to true', function(){ |
| 161 |
beforeEach(function(){ |
| 162 |
optgroupSelect.multiSelect({ selectableOptgroup: true }); |
| 163 |
optgroupMsContainer = optgroupSelect.next(); |
| 164 |
optgroupLabels = optgroupMsContainer.find('.ms-selectable .ms-optgroup-label'); |
| 165 |
}); |
| 166 |
|
| 167 |
it ('should select all nested options when clicking on optgroup', function(){ |
| 168 |
var clickedOptGroupLabel = optgroupLabels.first(); |
| 169 |
clickedOptGroupLabel.trigger('click'); |
| 170 |
expect(optgroupSelect.val().length).toBe(10); |
| 171 |
}); |
| 172 |
}); |
| 173 |
}); |
| 174 |
|
| 175 |
}); |
| 176 |
|
| 177 |
describe('select', function(){ |
| 178 |
|
| 179 |
describe('multiple values (Array)', function(){ |
| 180 |
var values = ['value1', 'value2', 'value7']; |
| 181 |
beforeEach(function(){ |
| 182 |
$('#multi-select').multiSelect(); |
| 183 |
$('#multi-select').multiSelect('select', values); |
| 184 |
}); |
| 185 |
|
| 186 |
it('should select corresponding option', function(){ |
| 187 |
expect(select.val()).toEqual(values); |
| 188 |
}); |
| 189 |
}); |
| 190 |
|
| 191 |
describe('single value (String)', function(){ |
| 192 |
var value = 'value1'; |
| 193 |
|
| 194 |
beforeEach(function(){ |
| 195 |
$('#multi-select').multiSelect(); |
| 196 |
$('#multi-select').multiSelect('select', value); |
| 197 |
}); |
| 198 |
|
| 199 |
it('should select corresponding option', function(){ |
| 200 |
expect($.inArray(value, select.val()) > -1).toBeTruthy(); |
| 201 |
}); |
| 202 |
}); |
| 203 |
|
| 204 |
describe("on click", function(){ |
| 205 |
var clickedItem, value; |
| 206 |
|
| 207 |
beforeEach(function() { |
| 208 |
$('#multi-select').multiSelect(); |
| 209 |
clickedItem = $('.ms-selectable ul.ms-list li').first(); |
| 210 |
value = clickedItem.data('ms-value'); |
| 211 |
spyOnEvent(select, 'change'); |
| 212 |
spyOnEvent(select, 'focus'); |
| 213 |
clickedItem.trigger('click'); |
| 214 |
}); |
| 215 |
|
| 216 |
it('should hide selected item', function(){ |
| 217 |
expect(clickedItem).toBeHidden(); |
| 218 |
}); |
| 219 |
|
| 220 |
it('should add the .ms-selected class to the selected item', function(){ |
| 221 |
expect(clickedItem.hasClass('ms-selected')).toBeTruthy(); |
| 222 |
}); |
| 223 |
|
| 224 |
it('should select corresponding option', function(){ |
| 225 |
expect(select.find('option[value="'+value+'"]')).toBeSelected(); |
| 226 |
}); |
| 227 |
|
| 228 |
it('should show the associated selected item', function(){ |
| 229 |
expect($('#'+sanitize(value)+'-selection')).toBe(':visible'); |
| 230 |
}); |
| 231 |
|
| 232 |
it('should trigger the original select change event', function(){ |
| 233 |
expect('change').toHaveBeenTriggeredOn("#multi-select"); |
| 234 |
}); |
| 235 |
|
| 236 |
afterEach(function(){ |
| 237 |
select.multiSelect('deselect_all'); |
| 238 |
}); |
| 239 |
}); |
| 240 |
|
| 241 |
describe("on click on disabled non-selected option", function(){ |
| 242 |
var clickedItem, value; |
| 243 |
|
| 244 |
beforeEach(function() { |
| 245 |
$('#multi-select').find('option').first().prop('disabled', true); |
| 246 |
$('#multi-select').multiSelect(); |
| 247 |
clickedItem = $('.ms-selectable ul.ms-list li').first(); |
| 248 |
value = clickedItem.data('ms-value'); |
| 249 |
spyOnEvent(select, 'change'); |
| 250 |
spyOnEvent(select, 'focus'); |
| 251 |
clickedItem.trigger('click'); |
| 252 |
}); |
| 253 |
|
| 254 |
it('should not hide selected item', function(){ |
| 255 |
expect(clickedItem).not.toBeHidden(); |
| 256 |
}); |
| 257 |
|
| 258 |
it('should not add the .ms-selected class to the selected item', function(){ |
| 259 |
expect(clickedItem.hasClass('ms-selected')).not.toBeTruthy(); |
| 260 |
}); |
| 261 |
|
| 262 |
it('should not select corresponding option', function(){ |
| 263 |
expect(select.find('option[value="'+value+'"]')).not.toBeSelected(); |
| 264 |
}); |
| 265 |
|
| 266 |
it('should not show the associated selected item', function(){ |
| 267 |
expect($('#'+sanitize(value)+'-selection')).not.toBe(':visible'); |
| 268 |
}); |
| 269 |
|
| 270 |
it('should not trigger the original select change event', function(){ |
| 271 |
expect('change').not.toHaveBeenTriggeredOn("#multi-select"); |
| 272 |
}); |
| 273 |
|
| 274 |
afterEach(function(){ |
| 275 |
select.multiSelect('deselect_all'); |
| 276 |
}); |
| 277 |
}); |
| 278 |
}); |
| 279 |
|
| 280 |
describe('deselect', function(){ |
| 281 |
describe('multiple values (Array)', function(){ |
| 282 |
var selectedValues = ['value1', 'value2', 'value7'], |
| 283 |
deselectValues = ['value1', 'value2']; |
| 284 |
beforeEach(function(){ |
| 285 |
$('#multi-select').multiSelect(); |
| 286 |
$('#multi-select').multiSelect('select', selectedValues); |
| 287 |
$('#multi-select').multiSelect('deselect', deselectValues); |
| 288 |
}); |
| 289 |
|
| 290 |
it('should select corresponding option', function(){ |
| 291 |
expect(select.val()).toEqual(['value7']); |
| 292 |
}); |
| 293 |
}); |
| 294 |
|
| 295 |
describe('single value (String)', function(){ |
| 296 |
var selectedValues = ['value1', 'value2', 'value7'], |
| 297 |
deselectValue = 'value2'; |
| 298 |
|
| 299 |
beforeEach(function(){ |
| 300 |
$('#multi-select').multiSelect(); |
| 301 |
$('#multi-select').multiSelect('select', selectedValues); |
| 302 |
$('#multi-select').multiSelect('deselect', deselectValue); |
| 303 |
}); |
| 304 |
|
| 305 |
it('should select corresponding option', function(){ |
| 306 |
expect($.inArray(deselectValue, select.val()) > -1).toBeFalsy(); |
| 307 |
}); |
| 308 |
}); |
| 309 |
|
| 310 |
describe("on click", function(){ |
| 311 |
var clickedItem, value; |
| 312 |
var correspondingSelectableItem; |
| 313 |
|
| 314 |
beforeEach(function() { |
| 315 |
$('#multi-select').find('option').first().prop('selected', true); |
| 316 |
$('#multi-select').multiSelect(); |
| 317 |
|
| 318 |
clickedItem = $('.ms-selection ul.ms-list li').first(); |
| 319 |
value = clickedItem.data('ms-value'); |
| 320 |
correspondingSelectableItem = $('.ms-selection ul.ms-list li').first(); |
| 321 |
spyOnEvent(select, 'change'); |
| 322 |
spyOnEvent(select, 'focus'); |
| 323 |
clickedItem.trigger('click'); |
| 324 |
}); |
| 325 |
|
| 326 |
it ('should hide clicked item', function(){ |
| 327 |
expect(clickedItem).toBe(':hidden'); |
| 328 |
}); |
| 329 |
|
| 330 |
it('should show associated selectable item', function(){ |
| 331 |
expect($('#'+sanitize(value)+'-selectable')).toBe(':visible'); |
| 332 |
}); |
| 333 |
|
| 334 |
it('should remove the .ms-selected class to the corresponding selectable item', function(){ |
| 335 |
expect(correspondingSelectableItem.hasClass('ms-selected')).toBeFalsy(); |
| 336 |
}); |
| 337 |
|
| 338 |
it('should deselect corresponding option', function(){ |
| 339 |
expect(select.find('option[value="'+value+'"]')).not.toBeSelected(); |
| 340 |
}); |
| 341 |
|
| 342 |
it('should trigger the original select change event', function(){ |
| 343 |
expect('change').toHaveBeenTriggeredOn("#multi-select"); |
| 344 |
}); |
| 345 |
|
| 346 |
afterEach(function(){ |
| 347 |
select.multiSelect('deselect_all'); |
| 348 |
}); |
| 349 |
}); |
| 350 |
|
| 351 |
describe("on click on disabled selected option", function(){ |
| 352 |
var clickedItem, value; |
| 353 |
var correspondingSelectableItem; |
| 354 |
|
| 355 |
beforeEach(function() { |
| 356 |
$('#multi-select').find('option').first().prop('selected', true).prop('disabled', true); |
| 357 |
$('#multi-select').multiSelect(); |
| 358 |
|
| 359 |
clickedItem = $('.ms-selection ul.ms-list li').first(); |
| 360 |
value = clickedItem.data('ms-value'); |
| 361 |
correspondingSelectableItem = $('.ms-selection ul.ms-list li').first(); |
| 362 |
spyOnEvent(select, 'change'); |
| 363 |
spyOnEvent(select, 'focus'); |
| 364 |
clickedItem.trigger('click'); |
| 365 |
}); |
| 366 |
|
| 367 |
it ('should not hide clicked item', function(){ |
| 368 |
expect(clickedItem).not.toBe(':hidden'); |
| 369 |
}); |
| 370 |
|
| 371 |
it('should not show associated selectable item', function(){ |
| 372 |
expect($('#'+value+'-selectable')).not.toBe(':visible'); |
| 373 |
}); |
| 374 |
|
| 375 |
it('should not remove the .ms-selected class to the corresponding selectable item', function(){ |
| 376 |
expect(correspondingSelectableItem.hasClass('ms-selected')).not.toBeFalsy(); |
| 377 |
}); |
| 378 |
|
| 379 |
it('should not deselect corresponding option', function(){ |
| 380 |
expect(select.find('option[value="'+value+'"]')).toBeSelected(); |
| 381 |
}); |
| 382 |
|
| 383 |
it('should not trigger the original select change event', function(){ |
| 384 |
expect('change').not.toHaveBeenTriggeredOn("#multi-select"); |
| 385 |
}); |
| 386 |
|
| 387 |
afterEach(function(){ |
| 388 |
select.multiSelect('deselect_all'); |
| 389 |
}); |
| 390 |
}); |
| 391 |
}); |
| 392 |
}); |
| 393 |
|