array-tests.js 2.6 KB

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