array-tests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. module('Data adapters - Array');
  2. var ArrayData = require('select2/data/array');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var options = new Options({
  6. data: [
  7. {
  8. id: 'default',
  9. text: 'Default'
  10. },
  11. {
  12. id: '1',
  13. text: 'One'
  14. },
  15. {
  16. id: '2',
  17. text: '2'
  18. }
  19. ]
  20. });
  21. test('current gets default for single', function (assert) {
  22. var $select = $('#qunit-fixture .single');
  23. var data = new ArrayData($select, options);
  24. data.current(function (val) {
  25. assert.equal(
  26. val.length,
  27. 1,
  28. 'There should always be a selected item for array data.'
  29. );
  30. var item = val[0];
  31. assert.equal(
  32. item.id,
  33. 'default',
  34. 'The first item should be selected'
  35. );
  36. });
  37. });
  38. test('current gets default for multiple', function (assert) {
  39. var $select = $('#qunit-fixture .multiple');
  40. var data = new ArrayData($select, options);
  41. data.current(function (val) {
  42. assert.equal(
  43. val.length,
  44. 0,
  45. 'There should be no default selection.'
  46. );
  47. });
  48. });
  49. test('current works with existing selections', function (assert) {
  50. var $select = $('#qunit-fixture .multiple');
  51. var data = new ArrayData($select, options);
  52. $select.val(['3']);
  53. data.current(function (val) {
  54. assert.equal(
  55. val.length,
  56. 1,
  57. 'There should only be one existing selection.'
  58. );
  59. var option = val[0];
  60. assert.equal(
  61. option.id,
  62. '3',
  63. 'The id should be equal to the value of the option tag.'
  64. );
  65. assert.equal(
  66. option.text,
  67. 'Three',
  68. 'The text should be equal to the text of the option tag.'
  69. );
  70. });
  71. });
  72. test('current works with selected data', function (assert) {
  73. var $select = $('#qunit-fixture .single');
  74. var data = new ArrayData($select, options);
  75. data.select({
  76. id: '2',
  77. text: '2'
  78. });
  79. data.current(function (val) {
  80. assert.equal(
  81. val.length,
  82. 1,
  83. 'There should only be one option selected.'
  84. );
  85. var option = val[0];
  86. assert.equal(
  87. option.id,
  88. '2',
  89. 'The id should match the original id from the array.'
  90. );
  91. assert.equal(
  92. option.text,
  93. '2',
  94. 'The text should match the original text from the array.'
  95. );
  96. });
  97. });
  98. test('select works for single', function (assert) {
  99. var $select = $('#qunit-fixture .single');
  100. var data = new ArrayData($select, options);
  101. assert.equal(
  102. $select.val(),
  103. 'default',
  104. 'There should already be a selection'
  105. );
  106. data.select({
  107. id: '1',
  108. text: 'One'
  109. });
  110. assert.equal(
  111. $select.val(),
  112. '1',
  113. 'The selected value should be the same as the selected id'
  114. );
  115. });
  116. test('multiple sets the value', function (assert) {
  117. var $select = $('#qunit-fixture .multiple');
  118. var data = new ArrayData($select, options);
  119. assert.equal($select.val(), null);
  120. data.select({
  121. id: 'default',
  122. text: 'Default'
  123. });
  124. assert.deepEqual($select.val(), ['default']);
  125. });
  126. test('multiple adds to the old value', function (assert) {
  127. var $select = $('#qunit-fixture .multiple');
  128. var data = new ArrayData($select, options);
  129. $select.val(['3']);
  130. assert.deepEqual($select.val(), ['3']);
  131. data.select({
  132. id: 'default',
  133. text: 'Default'
  134. });
  135. assert.deepEqual($select.val(), ['3', 'default']);
  136. });
  137. test('option tags are automatically generated', function (assert) {
  138. var $select = $('#qunit-fixture .single');
  139. var data = new ArrayData($select, options);
  140. assert.equal(
  141. $select.find('option').length,
  142. 3,
  143. 'An <option> element should be created for each object'
  144. );
  145. });