array-tests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.deepEqual(
  26. val,
  27. [],
  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.deepEqual(
  37. val,
  38. [],
  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.deepEqual(
  49. val,
  50. [{
  51. id: '3',
  52. text: 'Three',
  53. disabled: false
  54. }],
  55. 'The text and id should match the value and text for the option tag.'
  56. );
  57. });
  58. });
  59. test('current works with selected data', function (assert) {
  60. var $select = $('#qunit-fixture .single');
  61. var data = new ArrayData($select, options);
  62. data.select({
  63. id: '2',
  64. text: '2'
  65. });
  66. data.current(function (val) {
  67. assert.deepEqual(
  68. val,
  69. [{
  70. id: '2',
  71. text: '2'
  72. }],
  73. 'The text and id should match the selected array data.'
  74. );
  75. });
  76. });
  77. test('select works for single', function (assert) {
  78. var $select = $('#qunit-fixture .single');
  79. var data = new ArrayData($select, options);
  80. assert.equal($select.val(), null);
  81. data.select({
  82. id: '1',
  83. text: 'One'
  84. });
  85. assert.equal($select.val(), '1');
  86. });
  87. test('multiple sets the value', function (assert) {
  88. var $select = $('#qunit-fixture .multiple');
  89. var data = new ArrayData($select, options);
  90. assert.equal($select.val(), null);
  91. data.select({
  92. id: 'default',
  93. text: 'Default'
  94. });
  95. assert.deepEqual($select.val(), ['default']);
  96. });
  97. test('multiple adds to the old value', function (assert) {
  98. var $select = $('#qunit-fixture .multiple');
  99. var data = new ArrayData($select, options);
  100. $select.val(['3']);
  101. assert.deepEqual($select.val(), ['3']);
  102. data.select({
  103. id: 'default',
  104. text: 'Default'
  105. });
  106. assert.deepEqual($select.val(), ['3', 'default']);
  107. });