123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328 |
- describe('Bootstrap Multiselect "Core".', function() {
- var onInitialized = false;
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 100; i++) {
- var $option = $('<option value="' + i + '">' + i + '</option>');
- if (i < 10) {
- $option.prop('selected', true);
- }
- $select.append($option);
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- onInitialized: function($select) {
- onInitialized = true;
- },
- checkboxName: function($option) {
- return 'value-' + $($option).attr('value');
- }
- });
- });
- it('Should add the container after the select.', function() {
- expect($('#multiselect-container').length).toBe(1);
- });
- it('Should add the multiselect button.', function() {
- expect($('#multiselect-container .multiselect').length).toBe(1);
- });
- it('Should add the dropdown menu.', function() {
- expect($('#multiselect-container .dropdown-menu').length).toBe(1);
- });
- it('Should add an li element with checkbox and label for each option.', function() {
- expect($('#multiselect-container li').length).toBe(99);
- expect($('#multiselect-container label').length).toBe(99);
- expect($('#multiselect-container input[type="checkbox"]').length).toBe(99);
- });
- it('Should preserve selected options.', function() {
- expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(9);
- expect($('#multiselect option:selected').length).toBe(9);
- });
- it('Should be able to select options by value.', function() {
- $('#multiselect').multiselect('select', '10');
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
- });
- it('Select method should handle "null" and "undefined" correctly.', function() {
- expect($('#multiselect option:selected').length).toBe(9);
- $('#multiselect').multiselect('select', null);
- expect($('#multiselect option:selected').length).toBe(9);
- $('#multiselect').multiselect('select', undefined);
- expect($('#multiselect option:selected').length).toBe(9);
- });
- it('Should be able to deselect options by value.', function() {
- $('#multiselect').multiselect('select', '10');
- $('#multiselect').multiselect('deselect', '10');
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
- });
- it('Deselect method should handle "null" and "undefined" correctly.', function() {
- expect($('#multiselect option:selected').length).toBe(9);
- $('#multiselect').multiselect('deselect', null);
- expect($('#multiselect option:selected').length).toBe(9);
- $('#multiselect').multiselect('deselect', undefined);
- expect($('#multiselect option:selected').length).toBe(9);
- });
- it('Should be able to select options using an array of values.', function() {
- $('#multiselect').multiselect('select', ['10', '11']);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="11"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="11"]').prop('checked')).toBe(true);
- });
- it('Should be able to deselect options using an array of values.', function() {
- $('#multiselect').multiselect('select', ['10', '11']);
- $('#multiselect').multiselect('deselect', ['10', '11']);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="11"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="11"]').prop('checked')).toBe(false);
- });
- it('Should be able to disable the multiselect', function() {
- $('#multiselect').multiselect('disable');
- expect($('#multiselect').prop('disabled')).toBe(true);
- });
- it('Should be able to enable the multiselect', function() {
- $('#multiselect').multiselect('disable');
- $('#multiselect').multiselect('enable');
- expect($('#multiselect').prop('disabled')).toBe(false);
- });
- it('Should be able to select all options.', function() {
- $('#multiselect').multiselect('selectAll');
- for (var i = 1; i < 100; i++) {
- expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(true);
- }
- });
- it('Should be able to deselect all options.', function() {
- $('#multiselect').multiselect('selectAll');
- for (var i = 1; i < 100; i++) {
- expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(true);
- }
- $('#multiselect').multiselect('deselectAll');
- for (var i = 1; i < 100; i++) {
- expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(false);
- }
- });
- it('Should update the checkboxes according to the selected options after using refresh.', function() {
- for (var i = 10; i < 20; i++) {
- $('#multiselect option[value="' + i + '"]').prop('selected', true);
- }
- expect($('#multiselect option:selected').length).toBe(19);
- expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(9);
- $('#multiselect').multiselect('refresh');
- expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(19);
- for (var i = 10; i < 20; i++) {
- expect($('#multiselect option[value="' + i + '"]').prop('selected')).toBe(true);
- }
- });
- it('Should remove container, button and ul after destroy.', function() {
- $('#multiselect').multiselect('destroy');
- // Destroy should remove container, button and ul.
- expect($('#multiselect-container.multiselect-container').length).toBe(0);
- expect($('#multiselect-container .multiselect').length).toBe(0);
- expect($('#multiselect-container .dropdown-menu').length).toBe(0);
- });
- it('Should select an option when checkbox is changed (change event).', function() {
- $('#multiselect-container li input[value="10"]').prop('checked', true);
- $('#multiselect-container li input[value="10"]').trigger('change');
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
- });
- it('Should deselect an option when checkbox is changed (change event).', function() {
- $('#multiselect-container li input[value="10"]').prop('checked', true);
- $('#multiselect-container li input[value="10"]').trigger('change');
- // Already checked above.
- $('#multiselect-container li input[value="10"]').prop('checked', false);
- $('#multiselect-container li input[value="10"]').trigger('change');
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
- });
- it('Should select an option when checkbox is clicked.', function() {
- $('#multiselect-container li input[value="10"]').click();
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
- });
- it('Should deselect an option when checkbox is clicked.', function() {
- $('#multiselect-container li input[value="10"]').click();
- $('#multiselect-container li input[value="10"]').click();
- expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
- });
- it('Should trigger onInitialized.', function() {
- expect(onInitialized).toBe(true);
- });
- it('Should correctly apply checkboxName.', function() {
- $('#multiselect-container input').each(function() {
- expect($(this).attr('name')).toBe('value-' + $(this).attr('value'));
- });
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Multiple Multiselects".', function() {
- var multiselects = ['one', 'two', 'three'];
- var onSelectAllTriggered = 0;
- var onDeselectAllTriggered = 0;
- var onChangeFired = 0;
- beforeEach(function() {
- onSelectAllTriggered = 0;
- onDeselectAllTriggered = 0;
- onChangeFired = 0;
- for (var m = 0; m < 3; m++) {
- var $select = $('<select id="multiselect-' + multiselects[m] + '" multiple="multiple"></select>');
- for (var i = 1; i < 10; i++) {
- $select.append('<option value="' + i + '">Option ' + i + '</option>');
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container-' + multiselects[m] + '"></div>',
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all',
- onSelectAll: function() {
- onSelectAllTriggered++;
- },
- onDeselectAll: function() {
- onDeselectAllTriggered++;
- },
- onChange: function(option, checked) {
- onChangeFired++;
- }
- });
- }
- });
- it('Should initialize all multiselects.', function() {
- expect($('.multiselect').length).toBe(3);
- for (var m = 0; m < 3; m++) {
- expect($('#multiselect-' + multiselects[m]).length).toBe(1);
- expect($('#multiselect-container-' + multiselects[m]).length).toBe(1);
- expect($('#multiselect-container-' + multiselects[m] + ' .multiselect').length).toBe(1);
- expect($('#multiselect-container-' + multiselects[m] + ' .dropdown-menu').length).toBe(1);
- expect($('#multiselect-container-' + multiselects[m] + ' li').length).toBe(10); // including select all
- expect($('#multiselect-container-' + multiselects[m] + ' label').length).toBe(10); // including select all
- expect($('#multiselect-container-' + multiselects[m] + ' input[type="checkbox"]').length).toBe(10); // including select all
- }
- });
- it('Should not select/deselect options in other multiselects.', function() {
- for (var m = 0; m < 3; m++) {
- for (var i = 1; i < 10; i++) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="' + i + '"]').click();
- expect($('#multiselect-container-' + multiselects[m] + ' input[value!="multiselect-all"]:checked').length).toBe(i);
- for (var n = 0; n < 3; n++) {
- if (m != n) {
- expect($('#multiselect-container-' + multiselects[n] + ' input[value!="multiselect-all"]:checked').length).toBe(0);
- }
- }
- }
- for (var i = 9; i >= 1; i--) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="' + i + '"]').click();
- expect($('#multiselect-container-' + multiselects[m] + ' input[value!="multiselect-all"]:checked').length).toBe(i - 1);
- for (var n = 0; n < 3; n++) {
- if (m != n) {
- expect($('#multiselect-container-' + multiselects[n] + ' input[value!="multiselect-all"]:checked').length).toBe(0);
- }
- }
- }
- }
- });
- it('Should not trigger onChange in other multiselects.', function() {
- for (var m = 0; m < 3; m++) {
- for (var i = 1; i < 10; i++) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="' + i + '"]').click();
- expect(onChangeFired).toBe(1);
- onChangeFired = 0;
- }
- }
- });
- it('Should not select all/deselect all options in other multiselects.', function() {
- for (var m = 0; m < 3; m++) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- expect($('#multiselect-container-' + multiselects[m] + ' input[value!="multiselect-all"]:checked').length).toBe(9);
- for (var n = 0; n < 3; n++) {
- if (n != m) {
- expect($('#multiselect-container-' + multiselects[n] + ' input[value!="multiselect-all"]:checked').length).toBe(0);
- expect($('#multiselect-container-' + multiselects[n] + ' input[value="multiselect-all"]:checked').length).toBe(0);
- }
- }
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- expect($('#multiselect-container-' + multiselects[m] + ' input[value!="multiselect-all"]:checked').length).toBe(0);
- }
- });
- it('Should not trigger onSelectAll in other multiselects.', function() {
- for (var m = 0; m < 3; m++) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- expect(onSelectAllTriggered).toBe(1);
- expect(onChangeFired).toBe(0);
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- onSelectAllTriggered = 0;
- }
- });
- it('Should not trigger onDeselectAll in other multiselects', function() {
- for (var m = 0; m < 3; m++) {
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- $('#multiselect-container-' + multiselects[m] + ' input[value="multiselect-all"]').click();
- expect(onSelectAllTriggered).toBe(1);
- expect(onDeselectAllTriggered).toBe(1);
- expect(onChangeFired).toBe(0);
- onSelectAllTriggered = 0;
- onDeselectAllTriggered = 0;
- }
- });
- afterEach(function() {
- for (var m = 0; m < 3; m++) {
- $('#multiselect-' + multiselects[m]).multiselect('destroy');
- $('#multiselect-' + multiselects[m]).remove();
- }
- });
- });
- describe('Bootstrap Multiselect "Single Selection".', function() {
- beforeEach(function() {
- var $select = $('<select id="multiselect"></select>');
- for (var i = 1; i < 100; i++) {
- $select.append('<option value="' + i + '">Option ' + i + '</option>');
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>'
- });
- });
- it('Should create radio buttons instead of checkboxes.', function() {
- expect($('#multiselect-container input[type="radio"]').length).toBe(99);
- expect($('#multiselect-container input[type="checkbox"]').length).toBe(0);
- // Browser selects one option per default.
- expect($('#multiselect option:selected').length).toBe(1);
- });
- it('Only one option at a time can be selected.', function() {
- expect($('#multiselect option:selected').length).toBe(1);
- var i = 0;
- $('#multiselect-container input').each(function() {
- if (i === 0) {
- expect($(this).prop('checked')).toBe(true);
- i++;
- }
- else {
- expect($(this).prop('checked')).toBe(false);
- $(this).click();
- expect($('#multiselect option:selected').length).toBe(1);
- expect($(this).prop('checked')).toBe(true);
- i++;
- }
- });
- });
- it('Deselect all should work.', function() {
- expect($('#multiselect option:selected').length).toBe(1);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Individual Configuration Options".', function() {
- describe('disableIfEmpty.', function() {
- var $select = null;
- beforeEach(function() {
- $select = $('<select id="multiselect"></select>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- disableIfEmpty: true
- });
- });
- it('Should disable button if emppty.', function() {
- expect($('#multiselect-container button').prop('disabled')).toBe(true);
- });
- it('Should still be disabled after invoking rebuild.', function() {
- $select.multiselect('rebuild');
- expect($('#multiselect-container button').prop('disabled')).toBe(true);
- });
- it('Should not be disabled after invoking rebuild after adding options.', function() {
- $select.append('<option value="value-1">Option 1</option>');
- $select.multiselect('rebuild');
- expect($('#multiselect-container button').prop('disabled')).toBe(false);
- });
- it('Should not be disabled after rebuilding with more options after invoking destroy.', function() {
- $select.append('<option value="value-1">Option 1</option>');
- $select.multiselect('destroy');
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- disableIfEmpty: true
- });
- expect($('#multiselect-container button').prop('disabled')).toBe(false);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- });
- describe('Bootstrap Multiselect "individual Methods".', function() {
- describe('Method "clearSelection" should clear selection in multiple mode.', function() {
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<option value="value-1">Option 1</option>');
- $select.append('<option value="value-2">Option 2</option>');
- $select.append('<option value="value-3">Option 3</option>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>'
- });
- });
- it('Method "clearSelection" should clear selection.', function() {
- $('#multiselect-container input[value="value-1"]').click();
- $('#multiselect-container input[value="value-2"]').click();
- expect($('#multiselect-container input:checked').length).toBe(2);
- expect($('#multiselect option:selected').length).toBe(2);
- $('#multiselect').multiselect('clearSelection');
- expect($('#multiselect-container input:checked').length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Method "clearSelection" should correctly update select all.', function() {
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<option value="value-1">Option 1</option>');
- $select.append('<option value="value-2">Option 2</option>');
- $select.append('<option value="value-3">Option 3</option>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all'
- });
- });
- it('Method "clearSelection" should clear selection.', function() {
- $('#multiselect-container input[value="multiselect-all"]').click();
- expect($('#multiselect-container input:checked').length).toBe(4);
- expect($('#multiselect option:selected').length).toBe(3);
- $('#multiselect').multiselect('clearSelection');
- expect($('#multiselect-container input:checked').length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Method "clearSelection" should clear selection in single mode.', function() {
- beforeEach(function() {
- var $select = $('<select id="multiselect"></select>');
- $select.append('<option value="value-1">Option 1</option>');
- $select.append('<option value="value-2">Option 2</option>');
- $select.append('<option value="value-3">Option 3</option>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>'
- });
- });
- it('Method "clearSelection" is NOT able to clear selection.', function() {
- $('#multiselect-container input[value="value-2"]').click();
- expect($('#multiselect-container input:checked').length).toBe(1);
- expect($('#multiselect option:selected').length).toBe(1);
- $('#multiselect').multiselect('clearSelection');
- expect($('#multiselect-container input:checked').length).toBe(1);
- expect($('#multiselect option:selected').length).toBe(1);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- });
- describe('Bootstrap Multiselect "Clickable Optgroups"', function() {
- // Count the number of onChanges fired.
- var fired = 0;
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 11; i++) {
- var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
- for (var j = 1; j < 11; j++) {
- if (i == 1) {
- $optgroup.append('<option value="' + i + '-' + j + '" selected="selected">Option ' + i + '.' + j + '</option>');
- }
- else {
- $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
- }
- }
- $select.append($optgroup);
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- numberDisplayed: 10,
- onChange: function(option, checked) {
- fired++;
- }
- });
- });
- it('Should correctly create inputs for optgroups.', function() {
- expect($('#multiselect-container li.multiselect-group').length).toBe(10);
- expect($('#multiselect-container li.multiselect-group input').length).toBe(10);
- $('#multiselect-container label.multiselect-group').each(function() {
- expect($('input', $(this)).length).toBe(10);
- });
- });
- it('Groups should be clickable and correctly initialized.', function() {
- expect($('#multiselect option:selected').length).toBe(10);
- expect(fired).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- if (i == 0) {
- $('label', $(this)).click();
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- $('label', $(this)).click()
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe(10);
- }
- else {
- $('label', $(this)).click();
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe(20);
- $('label', $(this)).click();
- }
- i++;
- });
- });
- it('Clickable groups should fire onChange only once.', function() {
- expect($('#multiselect option:selected').length).toBe(10);
- fired = 0;
- expect(fired).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- // Selected
- expect(fired).toBe(1);
- fired = 0;
- $('label', $(this)).click();
- // Deselected
- expect(fired).toBe(1);
- fired = 0;
- i++;
- });
- });
- it('Clickable groups should fire change only once', function() {
- expect($('#multiselect option:selected').length).toBe(10);
- var changed = 0;
- $('#multiselect').on('change', function() {
- changed++;
- });
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- // Selected
- expect(changed).toBe(1);
- changed = 0;
- $('label', $(this)).click();
- // Deselected
- expect(changed).toBe(1);
- changed = 0;
- });
- fired = 0;
- });
- it('Should update button text.', function() {
- expect($('#multiselect option:selected').length).toBe(10);
- expect(fired).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- if (i == 0) {
- var text = ''
- $('option:selected', $('#multiselect optgroup')[i]).each(function() {
- text += $(this).text() + ', '
- });
- text = text.substr(0, text.length - 2);
- expect($('#multiselect-container .multiselect-selected-text').text()).toBe(text);
- $('label', $(this)).click();
- }
- else {
- $('label', $(this)).click();
- var text = ''
- $('option:selected', $('#multiselect optgroup')[i]).each(function() {
- text += $(this).text() + ', '
- });
- text = text.substr(0, text.length - 2);
- expect($('#multiselect-container .multiselect-selected-text').text()).toBe(text);
- $('label', $(this)).click();
- }
- i++;
- });
- });
- it('Should be updated by clicking corresponding options.', function() {
- for (var i = 1; i < 10; i++) {
- expect($('option:selected', $('#multiselect optgroup')[0]).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe(10);
- var $group = $($('#multiselect-container li.multiselect-group')[i]);
- var $optGroup = $($('#multiselect optgroup')[i]);
- $group.nextUntil('li.multiselect-item').each(function() {
- var $input = $('input', this);
- $input.click();
- expect($input.prop('checked')).toBe(true);
- });
- expect($('option:selected', $optGroup).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe(20);
- expect($('input', $group).prop('checked')).toBe(true);
- // Undo changes
- $group.nextUntil('li.multiselect-item').each(function() {
- var $input = $('input', this);
- $input.click();
- expect($input.prop('checked')).toBe(false);
- });
- expect($('#multiselect option:selected').length).toBe(10);
- expect($('input', $group).prop('checked')).toBe(false);
- }
- });
- it('Should be updated through select/deselect.', function() {
- var values = [];
- for (var i = 1; i < 11; i++) {
- values.push('1-' + i)
- }
- var $group = $('#multiselect-container li.multiselect-group')[0];
- $('#multiselect').multiselect('deselect', values);
- expect($('input', $group).prop('checked')).toBe(false);
- $('#multiselect').multiselect('select', values);
- expect($('input', $group).prop('checked')).toBe(true);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Collapsible Optgroups"', function() {
- // Count the number of onChanges fired.
- var fired = 0;
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 11; i++) {
- var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
- for (var j = 1; j < 11; j++) {
- $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
- }
- $select.append($optgroup);
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableCollapsibleOptGroups: true,
- onChange: function(option, checked) {
- fired++;
- }
- });
- });
- it('Should correctly create headers for optgroups.', function() {
- expect($('#multiselect-container li.multiselect-group').length).toBe(10);
- $('#multiselect-container label.multiselect-group').each(function() {
- expect($('input', $(this)).length).toBe(10);
- });
- });
- if ('Should not create inputs.', function() {
- expect($('#multiselect-container li.multiselect-group input').length).toBe(0);
- });
- it('Groups should not be clickable.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- $('label', $(this)).click();
- i++;
- });
- });
- it('Should be collapsible.', function() {
- var $group = $('#multiselect-container li.multiselect-group:first');
- $('.caret-container', $group).click();
- var $lis = $group.nextUntil('li.multiselect-group');
- $lis.each(function() {
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe(true);
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe($(this).is(':hidden'));
- });
- $('.caret-container', $group).click();
- var $lis = $group.nextUntil('li.multiselect-group');
- $lis.each(function() {
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe(false);
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe($(this).is(':hidden'));
- });
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Clickable+Collapsible Optgroups"', function() {
- // Count the number of onChanges fired.
- var fired = 0;
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 11; i++) {
- var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
- for (var j = 1; j < 11; j++) {
- $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
- }
- $select.append($optgroup);
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- enableCollapsibleOptGroups: true,
- onChange: function(option, checked) {
- fired++;
- }
- });
- });
- it('Should correctly create inputs for optgroups.', function() {
- expect($('#multiselect-container li.multiselect-group').length).toBe(10);
- expect($('#multiselect-container li.multiselect-group input').length).toBe(10);
- $('#multiselect-container label.multiselect-group').each(function() {
- expect($('input', $(this)).length).toBe(10);
- });
- });
- it('Groups should be clickable.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe(10);
- $('label', $(this)).click();
- i++;
- });
- });
- it('Clickable groups should fire onChange only once.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- fired = 0;
- expect(fired).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- // Selected
- expect(fired).toBe(1);
- fired = 0;
- $('label', $(this)).click();
- // Deselected
- expect(fired).toBe(1);
- fired = 0;
- i++;
- });
- });
- it('Should be collapsible.', function() {
- var $group = $('#multiselect-container li.multiselect-group:first');
- $('.caret-container', $group).click();
- var $lis = $group.nextUntil('li.multiselect-group');
- $lis.each(function() {
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe(true);
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe($(this).is(':hidden'));
- });
- $('.caret-container', $group).click();
- var $lis = $group.nextUntil('li.multiselect-group');
- $lis.each(function() {
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe(false);
- expect($(this).hasClass('multiselect-collapsible-hidden')).toBe($(this).is(':hidden'));
- });
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Clickable+Collapsible+SelectAll Optgroups"', function() {
- // Count the number of onChanges fired.
- var fired = 0;
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 11; i++) {
- var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
- for (var j = 1; j < 11; j++) {
- $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
- }
- $select.append($optgroup);
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- enableCollapsibleOptGroups: true,
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all'
- });
- });
- it('Should NOT handle option groups differently, i.e. not set class to active.', function() {
- // Otherwise they are hidden.
- $('#multiselect-container input[value="multiselect-all"]').click();
- var $groups = $('#multiselect-container li.multiselect-group');
- $groups.each(function() {
- expect($(this).hasClass('active')).toBe(true);
- });
- var $lis = $('#multiselect-container li:not(.multiselect-group)');
- $lis.each(function() {
- expect($(this).hasClass('active')).toBe(true);
- });
- });
- it('Should select all options (including option groups).', function() {
- //$('#multiselect-container li.multiselect-group .caret-container').click();
- $('#multiselect-container input[value="multiselect-all"]').click();
- var $lis = $('#multiselect-container li');
- $lis.each(function() {
- expect($('input', this).prop('checked')).toBe(true);
- });
- });
- it('Should automatically update select all.', function() {
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(10);
- expect($('#multiselect option:selected').length).toBe((i + 1)*10);
- i++;
- });
- expect($('#multiselect-container li input[value="multiselect-all"]').prop('checked')).toBe(true);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Dataprovider"', function() {
- beforeEach(function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>'
- });
- });
- var options = [
- {label: 'Option 1', value: '1', selected: true, title: 'Option 1 Title'},
- {label: 'Option 2', value: '2', title: 'Option 2 Title'},
- {label: 'Option 3', value: '3', selected: true, title: 'Option 3 Title'},
- {label: 'Option 4', value: '4', title: 'Option 4 Title'},
- {label: 'Option 5', value: '5', title: 'Option 5 Title'},
- {label: 'Option 6', value: '6', title: 'Option 6 Title'}
- ];
- var options_attributes = [
- {label: 'Option 1', value: '1', attributes: {'some-attribute': 'test'}}
- ];
- it("Should be able to add options.", function() {
- $('#multiselect').multiselect('dataprovider', options);
- expect($('#multiselect option').length).toBe(6);
- expect($('#multiselect-container input').length).toBe(6);
- expect($('#multiselect option[value="1"]').length).toBe(1);
- expect($('#multiselect option[value="1"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="1"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="2"]').length).toBe(1);
- expect($('#multiselect option[value="2"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="2"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="3"]').length).toBe(1);
- expect($('#multiselect option[value="3"]').prop('selected')).toBe(true);
- expect($('#multiselect-container input[value="3"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="4"]').length).toBe(1);
- expect($('#multiselect option[value="4"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="4"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="5"]').length).toBe(1);
- expect($('#multiselect option[value="5"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="5"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="6"]').length).toBe(1);
- expect($('#multiselect option[value="6"]').prop('selected')).toBe(false);
- expect($('#multiselect-container input[value="6"]').prop('checked')).toBe(false);
- });
- it("Should be able to define title.", function() {
- $('#multiselect').multiselect('dataprovider', options);
- expect($('#multiselect option[value="1"]').attr('title')).toBe('Option 1 Title');
- expect($('#multiselect option[value="2"]').attr('title')).toBe('Option 2 Title');
- expect($('#multiselect option[value="3"]').attr('title')).toBe('Option 3 Title');
- expect($('#multiselect option[value="4"]').attr('title')).toBe('Option 4 Title');
- expect($('#multiselect option[value="5"]').attr('title')).toBe('Option 5 Title');
- expect($('#multiselect option[value="6"]').attr('title')).toBe('Option 6 Title');
- expect($('#multiselect-container input[value="1"]').closest('label').attr('title')).toBe('Option 1 Title');
- expect($('#multiselect-container input[value="2"]').closest('label').attr('title')).toBe('Option 2 Title');
- expect($('#multiselect-container input[value="3"]').closest('label').attr('title')).toBe('Option 3 Title');
- expect($('#multiselect-container input[value="4"]').closest('label').attr('title')).toBe('Option 4 Title');
- expect($('#multiselect-container input[value="5"]').closest('label').attr('title')).toBe('Option 5 Title');
- expect($('#multiselect-container input[value="6"]').closest('label').attr('title')).toBe('Option 6 Title');
- });
- it("Should be able to define data attributes.", function() {
- $('#multiselect').multiselect('dataprovider', options_attributes)
- expect($('#multiselect option[value="1"]').attr('value')).toBe('1');
- expect($('#multiselect option[value="1"]').attr('data-some-attribute')).toBe('test');
- });
- var optgroups = [
- {
- label: 'Group 1', children: [
- {label: 'Option 1.1', value: '1-1'},
- {label: 'Option 1.2', value: '1-2'},
- {label: 'Option 1.3', value: '1-3'}
- ]
- },
- {
- label: 'Group 2', children: [
- {label: 'Option 2.1', value: '1'},
- {label: 'Option 2.2', value: '2'},
- {label: 'Option 2.3', value: '3'}
- ]
- }
- ];
- it('Should be able to handle optgroups.', function() {
- $('#multiselect').multiselect('dataprovider', optgroups);
- expect($('#multiselect optgroup').length).toBe(2);
- expect($('#multiselect option').length).toBe(6);
- expect($('#multiselect-container input').length).toBe(6);
- expect($('#multiselect optgroup[label="Group 1"] option').length).toBe(3);
- expect($('#multiselect optgroup[label="Group 2"] option').length).toBe(3);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Select All".', function() {
- var onSelectAllTriggered = false;
- var onDeselectAllTriggered = false;
- var fired = 0;
- beforeEach(function() {
- onSelectAllTriggered = false;
- onDeselectAllTriggered = false;
- fired = 0;
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 100; i++) {
- $select.append('<option value="' + i + '">1</option>');
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all',
- onSelectAll: function() {
- onSelectAllTriggered = true;
- },
- onDeselectAll: function() {
- onDeselectAllTriggered = true;
- },
- onChange: function(option, checked) {
- fired++;
- }
- });
- });
- it('Should not add an additional option to the select.', function() {
- expect($('#multiselect option[value="multiselect-all"]').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').length).toBe(1);
- expect($('#multiselect option').length).toBe(99);
- expect($('#multiselect-container input').length).toBe(100);
- });
- it('Should update the select all when all options are selected by the "select" method.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- for (var i = 1; i < 100; i++) {
- $('#multiselect').multiselect('select', i.toString());
- expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
- }
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input').length).toBe(100);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- });
- it('Should update the select all when all options are deselected by the "deselect" method (first all options are selected as before).', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- for (var i = 1; i < 100; i++) {
- $('#multiselect').multiselect('select', i.toString());
- }
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input:checked').length).toBe(100);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- for (var i = 1; i < 100; i++) {
- $('#multiselect').multiselect('deselect', i.toString());
- }
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- });
- it('Should update the select all option when all options are selected by the change event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- // Change all checkboxes except the select all one.
- $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
- $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
- // 100 options seleted including the select all.
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- });
- it('Should update the select all option when all options are deselected by the change event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
- $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- // Change all checkboxes except the select all one.
- $('#multiselect-container input[value!="multiselect-all"]').prop('checked', false);
- $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- });
- it('Should update the select all option when all options are selected by the click event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- });
- it('Should update the select all option when all options are deselected by the click event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- $('#multiselect-container input[value!="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- });
- it('Should trigger onSelectAll/onDeselectAll when changing the select all option.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value="multiselect-all"]').prop('checked', true);
- $('#multiselect-container input[value="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input:checked').length).toBe(100); // including select all
- expect(onSelectAllTriggered).toBe(true);
- $('#multiselect-container input[value="multiselect-all"]').prop('checked', false);
- $('#multiselect-container input[value="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- expect(onDeselectAllTriggered).toBe(true);
- });
- it('Should trigger the onSelectAll/onDeselectAll when clicking the select all option.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input:checked').length).toBe(100); // including select all
- expect(onSelectAllTriggered).toBe(true);
- $('#multiselect-container input[value="multiselect-all"]').click();
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- expect(onDeselectAllTriggered).toBe(true);
- });
- it('Should NOT trigger onSelectAll/onDeselectAll based on the change event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
- $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- expect(onSelectAllTriggered).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').prop('checked', false);
- $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
- expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(0);
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- expect(onDeselectAllTriggered).toBe(false);
- });
- it('Should NOT trigger onSelectAll/onDeselectAll based on the click event.', function() {
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- expect(onSelectAllTriggered).toBe(false);
- $('#multiselect-container input[value!="multiselect-all"]').click();
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- expect(onDeselectAllTriggered).toBe(false);
- });
- it('Should trigger onSelectAll/onDeselectAll on function call.', function() {
- $('#multiselect').multiselect('selectAll', true, true);
- expect(onSelectAllTriggered).toBe(true);
- $('#multiselect').multiselect('deselectAll', true, true);
- expect(onDeselectAllTriggered).toBe(true);
- });
- it('Should NOT trigger onSelectAll on initialization but initialize the select all option correctly.', function() {
- var $select = $('<select id="multiselect-onSelectAll" multiple="multiple"></select>');
- for (var i = 1; i < 10; i++) {
- $select.append('<option value="' + i + '" selected>1</option>');
- }
- $('body').append($select);
- onSelectAllTriggered = false;
- $select.multiselect({
- buttonContainer: '<div id="multiselect-onSelectAll-container"></div>',
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all',
- onSelectAll: function() {
- onSelectAllTriggered = true;
- },
- });
- expect($('#multiselect-onSelectAll-container input[value="multiselect-all"]').prop('checked')).toBe(true);
- expect(onSelectAllTriggered).toBe(false);
- $('#multiselect-onSelectAll').multiselect('destroy');
- $('#multiselect-onSelectAll').remove();
- });
- it('Should NOT trigger onDeselectAll on initialization but initialize the select all option correctly.', function() {
- var $select = $('<select id="multiselect-onDeselectAll" multiple="multiple"></select>');
- for (var i = 1; i < 10; i++) {
- $select.append('<option value="' + i + '">1</option>');
- }
- $('body').append($select);
- onDeselectAllTriggered = false;
- $select.multiselect({
- buttonContainer: '<div id="multiselect-onDeselectAll-container"></div>',
- includeSelectAllOption: true,
- selectAllValue: 'multiselect-all',
- onDeselectAll: function() {
- onDeselectAllTriggered = true;
- },
- });
- expect($('#multiselect-onDeselectAll-container input[value="multiselect-all"]').prop('checked')).toBe(false);
- expect(onDeselectAllTriggered).toBe(false);
- $('#multiselect-onDeselectAll').multiselect('destroy');
- $('#multiselect-onDeselectAll').remove();
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Filter".', function() {
- var $select = null;
- beforeEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 10; i++) {
- $select.append('<option value="value-' + i + '">Option ' + i + '</option>');
- }
- $('body').append($select);
- });
- describe('Should create filter.', function() {
- beforeEach(function() {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value'
- });
- });
- it('Should create filter.', function() {
- expect($('#multiselect-container li.multiselect-filter').length).toBe(1);
- expect($('#multiselect-container li.multiselect-filter input').length).toBe(1);
- expect($('#multiselect-container li.multiselect-filter .multiselect-clear-filter').length).toBe(1);
- });
- });
- describe('Should filter elements by value.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- }
- });
- });
- describe('Should filter elements by value only.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('Option').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- });
- });
- describe('Should filter elements by text.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'text',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('Option 9').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- }
- });
- });
- describe('Should filter elements by text only.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'text',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('value').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- });
- });
- describe('Should filter elements by text and value - text.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'both',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('Option 9').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- }
- });
- });
- describe('Should filter elements by text and value - value.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'both',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should filter elements.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- }
- });
- });
- describe('Should remove filter on clicking the clear button.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should remove filter.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(true, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- }
- $('#multiselect-container li.multiselect-filter .multiselect-clear-filter').click();
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').closest('li').hasClass('multiselect-filter-hidden')).toBe(false, i);
- }
- });
- });
- describe('Filtering and removing the filter should not alter selection.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li input[value="value-1"]').click();
- $('#multiselect-container li input[value="value-9"]').click();
- for (var i = 1; i < 10; i++) {
- if (i != 9 && i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should not alter selection.', function() {
- for (var i = 1; i < 10; i++) {
- if (i != 9 && i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- $('#multiselect-container li.multiselect-filter .multiselect-clear-filter').click();
- for (var i = 1; i < 10; i++) {
- if (i != 9 && i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- });
- });
- describe('Select method should select both hidden and visible options.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should not alter selection.', function() {
- $('#multiselect').multiselect('select', 'value-1');
- expect($('#multiselect-container li input[value="value-1"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="value-1"]').prop('selected')).toBe(true);
- $('#multiselect').multiselect('select', 'value-9');
- expect($('#multiselect-container li input[value="value-9"]').prop('checked')).toBe(true);
- expect($('#multiselect option[value="value-9"]').prop('selected')).toBe(true);
- });
- });
- describe('Deselect method should select both hidden and visible options.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li input[value="value-9"]').click();
- $('#multiselect-container li input[value="value-1"]').click();
- for (var i = 1; i < 10; i++) {
- if (i != 9 && i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should not alter selection.', function() {
- $('#multiselect').multiselect('deselect', 'value-1');
- expect($('#multiselect-container li input[value="value-1"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="value-1"]').prop('selected')).toBe(false);
- $('#multiselect').multiselect('deselect', 'value-9');
- expect($('#multiselect-container li input[value="value-9"]').prop('checked')).toBe(false);
- expect($('#multiselect option[value="value-9"]').prop('selected')).toBe(false);
- });
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Select All+Filter+selectAllJustVisible".', function() {
- var $select = null;
- beforeEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 10; i++) {
- $select.append('<option value="value-' + i + '">Option ' + i + '</option>');
- }
- $('body').append($select);
- });
- describe('Should select only visible options if selectAllJustVisible is true.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- includeSelectAllOption: true,
- selectAllJustVisible: true,
- selectAllValue: 'multiselect-all',
- onFiltering: function() {
- done();
- }
- });
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should select one option.', function() {
- $('#multiselect-container li input[value="multiselect-all"]').click();
- for (var i = 1; i < 10; i++) {
- if (i != 9) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- });
- });
- describe('Should deselect only visible options if selectAllJustVisible is true.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- includeSelectAllOption: true,
- selectAllJustVisible: true,
- selectAllValue: 'multiselect-all',
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li input[value="value-1"]').click();
- $('#multiselect-container li input[value="value-9"]').click();
- for (var i = 1; i < 10; i++) {
- if (i != 9 && i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should deselect one option.', function() {
- $('#multiselect-container li input[value="multiselect-all"]').click();
- for (var i = 1; i < 10; i++) {
- if (i != 1) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- else {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- }
- });
- });
- describe('Should select all options if selectAllJustVisible is false.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- includeSelectAllOption: true,
- selectAllJustVisible: false,
- selectAllValue: 'multiselect-all',
- onFiltering: function() {
- done();
- }
- });
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should select all options.', function() {
- $('#multiselect-container li input[value="multiselect-all"]').click();
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- });
- });
- describe('Should deselect all options if selectAllJustVisible is false.', function() {
- beforeEach(function(done) {
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- filterBehavior: 'value',
- includeSelectAllOption: true,
- selectAllJustVisible: false,
- selectAllValue: 'multiselect-all',
- onFiltering: function() {
- done();
- }
- });
- for (var i = 1; i < 10; i++) {
- $('#multiselect-container li input[value="value-' + i + '"]').click();
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(true, i);
- }
- $('#multiselect-container li.multiselect-filter input').val('value-9').trigger('keydown');
- });
- it('Should select all options.', function() {
- $('#multiselect-container li input[value="multiselect-all"]').click();
- for (var i = 1; i < 10; i++) {
- expect($('#multiselect-container li input[value="value-' + i + '"]').prop('checked')).toBe(false, i);
- }
- });
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- describe('Bootstrap Multiselect "Specific Issues".', function() {
- it('#393.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 100; i++) {
- $select.append('<option value="' + i + '">1</option>');
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- includeSelectAllOption: true,
- selectAllValue: 0
- });
- expect($('#multiselect-container input[value="0"]').length).toBe(1);
- expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
- $('#multiselect').multiselect('selectAll');
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(true);
- $('#multiselect').multiselect('deselectAll');
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
- $('#multiselect-container input[value="0"]').click();
- expect($('#multiselect option:selected').length).toBe(99);
- expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(true);
- $('#multiselect-container input[value="0"]').click();
- expect($('#multiselect option:selected').length).toBe(0);
- expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- it('#405.', function() {
- var selection = document.getSelection();
- var range = document.createRange();
- var $selection = $('<span>Some text to select</span>');
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 5; i++) {
- $select.append('<option value="' + i + '">select option</option>');
- }
- $('body').append($selection).append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- });
- range.selectNodeContents($selection.get(0));
- selection.removeAllRanges();
- selection.addRange(range);
- if (document.getSelection().type === 'Range') {
- $('#multiselect-container').find('a:first label').trigger('click');
- expect($('#multiselect-container').find('input:first').prop('checked')).toBe(true);
- }
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- $selection.remove();
- });
- it('#679.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 11; i++) {
- var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
- for (var j = 1; j < 11; j++) {
- $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
- }
- $select.append($optgroup);
- }
- $('body').append($select);
- var fired = 0;
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- enableCollapsibleOptGroups: true,
- onChange: function(option, checked) {
- fired++;
- }
- });
- expect($('#multiselect option:selected').length).toBe(0);
- expect(fired).toBe(0);
- var i = 0;
- $('#multiselect-container li.multiselect-group').each(function() {
- $('label', $(this)).click();
- // Selected
- expect(fired).toBe(1);
- fired = 0;
- $('label', $(this)).click();
- // Deselected
- expect(fired).toBe(1);
- fired = 0;
- i++;
- });
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- it('#655/1 when clicking the options.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<optgroup label="Group 1"><option value="1-1">Option 1.1</option><option value="1-2">Option 1.2</option><option value="1-3">Option 1.3</option></optgroup>');
- $select.append('<optgroup label="Group 2"><option value="2-1">Option 2.1</option><option value="2-2">Option 2.2</option><option value="2-3">Option 2.3</option></optgroup>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true
- });
- $('#multiselect-container input[value="2-1"]').click();
- $('#multiselect-container input[value="2-2"]').click();
- $('#multiselect-container input[value="2-3"]').click();
- expect($($('#multiselect-container li.multiselect-group input')[0]).prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-1"]').prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-2"]').prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-3"]').prop('checked')).toBe(false);
- expect($($('#multiselect-container li.multiselect-group input')[1]).prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-1"]').prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-2"]').prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-3"]').prop('checked')).toBe(true);
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- it('#655/1 when clicking the optgroup.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<optgroup label="Group 1"><option value="1-1">Option 1.1</option><option value="1-2">Option 1.2</option><option value="1-3">Option 1.3</option></optgroup>');
- $select.append('<optgroup label="Group 2"><option value="2-1">Option 2.1</option><option value="2-2">Option 2.2</option><option value="2-3">Option 2.3</option></optgroup>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true
- });
- $($('#multiselect-container li.multiselect-group input')[1]).click();
- expect($($('#multiselect-container li.multiselect-group input')[0]).prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-1"]').prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-2"]').prop('checked')).toBe(false);
- expect($('#multiselect-container input[value="1-3"]').prop('checked')).toBe(false);
- expect($($('#multiselect-container li.multiselect-group input')[1]).prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-1"]').prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-2"]').prop('checked')).toBe(true);
- expect($('#multiselect-container input[value="2-3"]').prop('checked')).toBe(true);
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- it('#655/2 when clicking the options.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<optgroup label="Group 1"><option value="1-1">Option 1.1</option><option value="1-2">Option 1.2</option><option value="1-3">Option 1.3</option></optgroup>');
- $select.append('<optgroup label="Group 2"><option value="2-1">Option 2.1</option><option value="2-2">Option 2.2</option><option value="2-3">Option 2.3</option></optgroup>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- selectedClass: 'multiselect-custom-selected'
- });
- $('#multiselect-container input[value="2-1"]').click();
- $('#multiselect-container input[value="2-2"]').click();
- $('#multiselect-container input[value="2-3"]').click();
- expect($($('#multiselect-container li.multiselect-group')[0]).prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-1"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-2"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-3"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($($('#multiselect-container li.multiselect-group')[1]).prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-1"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-2"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-3"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- it('#655/2 when clicking the optgroup.', function() {
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<optgroup label="Group 1"><option value="1-1">Option 1.1</option><option value="1-2">Option 1.2</option><option value="1-3">Option 1.3</option></optgroup>');
- $select.append('<optgroup label="Group 2"><option value="2-1">Option 2.1</option><option value="2-2">Option 2.2</option><option value="2-3">Option 2.3</option></optgroup>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableClickableOptGroups: true,
- selectedClass: 'multiselect-custom-selected'
- });
- $($('#multiselect-container li.multiselect-group input')[1]).click();
- expect($($('#multiselect-container li.multiselect-group')[0]).prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-1"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-2"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="1-3"])').prop('class').split(' ')).not.toContain('multiselect-custom-selected');
- expect($($('#multiselect-container li.multiselect-group')[1]).prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-1"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-2"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- expect($('#multiselect-container li:has(input[value="2-3"])').prop('class').split(' ')).toContain('multiselect-custom-selected');
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- describe('#732.', function() {
- var triggeredOnSelectAll = false;
- var triggeredOnDeselectAll = false;
- beforeEach(function(done) {
- triggeredOnSelectAll = false;
- triggeredOnDeselectAll = false;
- var $select = $('<select id="multiselect" multiple="multiple"></select>');
- $select.append('<option value="value-1">Option 1</option><option value="value-2">Option 2</option><option value="value-1">Option 1</option>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- enableFiltering: true,
- includeSelectAllOption: true,
- onSelectAll: function() {
- triggeredOnSelectAll = true;
- },
- onDeselectAll: function() {
- triggeredOnDeselectAll = false;
- },
- onFiltering: function() {
- done();
- }
- });
- $('#multiselect-container li.multiselect-filter input').val('2').trigger('keydown');
- });
- it('Should not fire onSelectAll or onDeselectAll when filtering or clearing filter.', function() {
- expect(triggeredOnSelectAll).toBe(false);
- expect(triggeredOnDeselectAll).toBe(false);
- $('#multiselect-container li.multiselect-filter .multiselect-clear-filter').click();
- expect(triggeredOnSelectAll).toBe(false);
- expect(triggeredOnDeselectAll).toBe(false);
- });
- afterEach(function() {
- $('#multiselect').multiselect('destroy');
- $('#multiselect').remove();
- });
- });
- it('#759.', function() {
- var $select = $('<select data-placeholder="Test" id="multiselect" multiple="multiple"></select>');
- $select.append('<option value="value-1">Option 1</option><option value="value-2">Option 2</option><option value="value-3">Option 3</option>');
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>'
- });
- expect($('#multiselect-container .multiselect-selected-text').text()).toBe('Test');
- $('#multiselect').multiselect('destroy');
- $('#multiselect').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();
- $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();
- var multiSpy = spyOn(ko.bindingHandlers.multiselect, 'init').and.callThrough();
- ko.applyBindings(viewModel, $testArea[0]);
- // knockout bindings were called
- expect(optionSpy.calls.count()).toEqual(1);
- expect(multiSpy.calls.count()).toEqual(1);
- // 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);
- // 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);
- // 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');
- });
- });
- describe('Bootstrap Multiselect "Reset".', function() {
- var $select;
- beforeEach(function() {
- $select = $('<select id="multiselect" multiple="multiple"></select>');
- for (var i = 1; i < 100; i++) {
- $select.append('<option value="' + i + '">1</option>');
- }
- $('body').append($select);
- $select.multiselect({
- buttonContainer: '<div id="multiselect-container"></div>',
- includeResetOption: true
- });
- });
- it('Should not add an additional option to the select.', function() {
- expect($select.find('option').length).toBe(99);
- });
- it('Should add reset button.', function() {
- expect($('#multiselect-container').find('li.multiselect-reset a').text()).toBe('Reset');
- });
- it('Should reset if button clicked.', function() {
- $select.multiselect('selectAll', true, false);
- expect($select.find('option:selected').length).toBe(99);
- $('#multiselect-container').find('li.multiselect-reset a').click();
- expect($select.find('option:selected').length).toBe(0);
- });
- afterEach(function() {
- $select.multiselect('destroy');
- $select.remove();
- });
- });
|