array-tests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 0,
  28. 'There should be no default selection.'
  29. );
  30. });
  31. });
  32. test('current gets default for multiple', function (assert) {
  33. var $select = $('#qunit-fixture .multiple');
  34. var data = new ArrayData($select, options);
  35. data.current(function (val) {
  36. assert.equal(
  37. val.length,
  38. 0,
  39. 'There should be no default selection.'
  40. );
  41. });
  42. });
  43. test('current works with existing selections', function (assert) {
  44. var $select = $('#qunit-fixture .multiple');
  45. var data = new ArrayData($select, options);
  46. $select.val(['3']);
  47. data.current(function (val) {
  48. assert.equal(
  49. val.length,
  50. 1,
  51. 'There should only be one existing selection.'
  52. );
  53. var option = val[0];
  54. assert.equal(
  55. option.id,
  56. '3',
  57. 'The id should be equal to the value of the option tag.'
  58. );
  59. assert.equal(
  60. option.text,
  61. 'Three',
  62. 'The text should be equal to the text of the option tag.'
  63. );
  64. });
  65. });
  66. test('current works with selected data', function (assert) {
  67. var $select = $('#qunit-fixture .single');
  68. var data = new ArrayData($select, options);
  69. data.select({
  70. id: '2',
  71. text: '2'
  72. });
  73. data.current(function (val) {
  74. assert.equal(
  75. val.length,
  76. 1,
  77. 'There should only be one option selected.'
  78. );
  79. var option = val[0];
  80. assert.equal(
  81. option.id,
  82. '2',
  83. 'The id should match the original id from the array.'
  84. );
  85. assert.equal(
  86. option.text,
  87. '2',
  88. 'The text should match the original text from the array.'
  89. );
  90. });
  91. });
  92. test('select works for single', function (assert) {
  93. var $select = $('#qunit-fixture .single');
  94. var data = new ArrayData($select, options);
  95. assert.equal($select.val(), null);
  96. data.select({
  97. id: '1',
  98. text: 'One'
  99. });
  100. assert.equal($select.val(), '1');
  101. });
  102. test('multiple sets the value', function (assert) {
  103. var $select = $('#qunit-fixture .multiple');
  104. var data = new ArrayData($select, options);
  105. assert.equal($select.val(), null);
  106. data.select({
  107. id: 'default',
  108. text: 'Default'
  109. });
  110. assert.deepEqual($select.val(), ['default']);
  111. });
  112. test('multiple adds to the old value', function (assert) {
  113. var $select = $('#qunit-fixture .multiple');
  114. var data = new ArrayData($select, options);
  115. $select.val(['3']);
  116. assert.deepEqual($select.val(), ['3']);
  117. data.select({
  118. id: 'default',
  119. text: 'Default'
  120. });
  121. assert.deepEqual($select.val(), ['3', 'default']);
  122. });