Commit 15ba56b2 authored by David Stutz's avatar David Stutz

Follow up on #666.

parent f6910e40
......@@ -706,72 +706,72 @@ describe('Bootstrap Multiselect Specific Issues', function() {
});
});
describe('Test knockout binding', function() {
var $testArea;
afterEach(function() {
if ($testArea) {
$testArea.multiselect('destroy').remove();
}
});
describe('Knockout Binding', function() {
var $testArea;
afterEach(function() {
if ($testArea) {
$testArea.multiselect('destroy').remove();
}
});
it('should update values and options with an observable array', function() {
jasmine.clock().install();
it('Should update values and options with an observable array', function() {
jasmine.clock().install();
$testArea = $('<select multiple="multiple" data-bind="selectedOptions: myValues, options: myOptions, multiselect: {numberDisplayed: 1}"></select>').appendTo(document.body);
var viewModel = {
myValues: ko.observableArray(),
myOptions: ko.observableArray([])
};
$testArea = $('<select multiple="multiple" data-bind="selectedOptions: myValues, options: myOptions, multiselect: {numberDisplayed: 1}"></select>').appendTo(document.body);
var viewModel = {
myValues: ko.observableArray(),
myOptions: ko.observableArray([])
};
expect(ko.bindingHandlers.multiselect.init !== undefined).toEqual(true);
var optionSpy = spyOn(ko.bindingHandlers.selectedOptions, 'init').and.callThrough(),
multiSpy = spyOn(ko.bindingHandlers.multiselect, 'init').and.callThrough();
expect(ko.bindingHandlers.multiselect.init !== undefined).toEqual(true);
ko.applyBindings(viewModel, $testArea[0]);
var optionSpy = spyOn(ko.bindingHandlers.selectedOptions, 'init').and.callThrough();
var multiSpy = spyOn(ko.bindingHandlers.multiselect, 'init').and.callThrough();
// knockout bindings were called
expect(optionSpy.calls.count()).toEqual(1);
expect(multiSpy.calls.count()).toEqual(1);
ko.applyBindings(viewModel, $testArea[0]);
// no options are present since myOptions was empty
expect($testArea.find('option').length).toEqual(0);
expect($testArea.val()).toEqual(null);
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('None selected');
expect($testArea.next().find('ul li').length).toEqual(0);
// knockout bindings were called
expect(optionSpy.calls.count()).toEqual(1);
expect(multiSpy.calls.count()).toEqual(1);
// Add more options
viewModel.myOptions(['option1', 'option2']);
jasmine.clock().tick(1000);
expect($testArea.next().find('ul li').length).toEqual(2);
expect($testArea.find('option').length).toEqual(2);
expect($testArea.find('option:checked').length).toEqual(0);
// no options are present since myOptions was empty
expect($testArea.find('option').length).toEqual(0);
expect($testArea.val()).toEqual(null);
// select one
viewModel.myValues(['option2']);
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(1);
expect($testArea.find('option:checked').text().trim()).toEqual('option2');
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('None selected');
expect($testArea.next().find('ul li').length).toEqual(0);
// select all
viewModel.myValues(['option1', 'option2']);
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(2);
expect($testArea.find('option:checked').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2']);
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('All selected (2)');
// Add more options
viewModel.myOptions(['option1', 'option2']);
jasmine.clock().tick(1000);
// add another option
viewModel.myOptions.push('wacky option');
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(2);
expect($testArea.find('option:checked').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2']);
expect($testArea.find('option').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2', 'wacky option']);
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('2 selected');
});
expect($testArea.next().find('ul li').length).toEqual(2);
expect($testArea.find('option').length).toEqual(2);
expect($testArea.find('option:checked').length).toEqual(0);
// select one
viewModel.myValues(['option2']);
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(1);
expect($testArea.find('option:checked').text().trim()).toEqual('option2');
// select all
viewModel.myValues(['option1', 'option2']);
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(2);
expect($testArea.find('option:checked').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2']);
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('All selected (2)');
// add another option
viewModel.myOptions.push('wacky option');
jasmine.clock().tick(1000);
expect($testArea.find('option:checked').length).toEqual(2);
expect($testArea.find('option:checked').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2']);
expect($testArea.find('option').map(function() { return $(this).text().trim() }).toArray()).toEqual(['option1', 'option2', 'wacky option']);
expect($testArea.next().find('button.multiselect').text().trim()).toEqual('2 selected');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment