array-tests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.deepEqual(
  75. val,
  76. [{
  77. id: '2',
  78. text: '2'
  79. }],
  80. 'The text and id should match the selected array data.'
  81. );
  82. });
  83. });
  84. test('select works for single', function (assert) {
  85. var $select = $('#qunit-fixture .single');
  86. var data = new ArrayData($select, options);
  87. assert.equal($select.val(), null);
  88. data.select({
  89. id: '1',
  90. text: 'One'
  91. });
  92. assert.equal($select.val(), '1');
  93. });
  94. test('multiple sets the value', function (assert) {
  95. var $select = $('#qunit-fixture .multiple');
  96. var data = new ArrayData($select, options);
  97. assert.equal($select.val(), null);
  98. data.select({
  99. id: 'default',
  100. text: 'Default'
  101. });
  102. assert.deepEqual($select.val(), ['default']);
  103. });
  104. test('multiple adds to the old value', function (assert) {
  105. var $select = $('#qunit-fixture .multiple');
  106. var data = new ArrayData($select, options);
  107. $select.val(['3']);
  108. assert.deepEqual($select.val(), ['3']);
  109. data.select({
  110. id: 'default',
  111. text: 'Default'
  112. });
  113. assert.deepEqual($select.val(), ['3', 'default']);
  114. });