select-tests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. module('Data adapters - Select');
  2. var SelectData = require('select2/data/select');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var options = new Options({});
  6. test('current gets default for single', function (assert) {
  7. var $select = $('#qunit-fixture .single');
  8. var data = new SelectData($select, options);
  9. data.current(function (val) {
  10. assert.deepEqual(
  11. val,
  12. [{
  13. id: 'default',
  14. text: 'Default',
  15. disabled: false
  16. }],
  17. 'The first option should be selected by default (by the browser).'
  18. );
  19. });
  20. });
  21. test('current gets default for multiple', function (assert) {
  22. var $select = $('#qunit-fixture .multiple');
  23. var data = new SelectData($select, options);
  24. data.current(function (val) {
  25. assert.deepEqual(
  26. val,
  27. [],
  28. 'Multiple selects have no default selection.'
  29. );
  30. });
  31. });
  32. test('current gets options with explicit value', function (assert) {
  33. var $select = $('#qunit-fixture .single');
  34. var data = new SelectData($select, options);
  35. $select.val('1');
  36. data.current(function (val) {
  37. assert.deepEqual(
  38. val,
  39. [{
  40. id: '1',
  41. text: 'One',
  42. disabled: false
  43. }],
  44. 'The text and id should match the value and text for the option tag.'
  45. );
  46. });
  47. });
  48. test('current gets options with implicit value', function (assert) {
  49. var $select = $('#qunit-fixture .single');
  50. var data = new SelectData($select, options);
  51. $select.val('2');
  52. data.current(function (val) {
  53. assert.deepEqual(
  54. val,
  55. [{
  56. id: '2',
  57. text: '2',
  58. disabled: false
  59. }],
  60. 'The text and id should match the text within the option tag.'
  61. );
  62. });
  63. });
  64. test('select works for single', function (assert) {
  65. var $select = $('#qunit-fixture .single');
  66. var data = new SelectData($select, options);
  67. assert.equal($select.val(), 'default');
  68. data.select({
  69. id: '1',
  70. text: 'One'
  71. });
  72. assert.equal($select.val(), '1');
  73. });
  74. test('multiple sets the value', function (assert) {
  75. var $select = $('#qunit-fixture .multiple');
  76. var data = new SelectData($select, options);
  77. assert.equal($select.val(), null);
  78. data.select({
  79. id: 'default',
  80. text: 'Default'
  81. });
  82. assert.deepEqual($select.val(), ['default']);
  83. });
  84. test('multiple adds to the old value', function (assert) {
  85. var $select = $('#qunit-fixture .multiple');
  86. var data = new SelectData($select, options);
  87. $select.val(['2']);
  88. assert.deepEqual($select.val(), ['2']);
  89. data.select({
  90. id: 'default',
  91. text: 'Default'
  92. });
  93. assert.deepEqual($select.val(), ['default', '2']);
  94. });