select2-methods.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module('select2(data)');
  2. var $ = require('jquery');
  3. var Select2 = require('select2/core');
  4. var Options = require('select2/options');
  5. test('single default selection returned', function (assert) {
  6. var $select = $(
  7. '<select>' +
  8. '<option>One</option>' +
  9. '<option>Two</option>' +
  10. '<option value="3" selected>Three</option>' +
  11. '</select>'
  12. );
  13. var options = new Options({});
  14. var select = new Select2($select, options);
  15. var items = select.data();
  16. assert.equal(
  17. items.length,
  18. 1,
  19. 'The one selected item should be returned'
  20. );
  21. var first = items[0];
  22. assert.equal(
  23. first.id,
  24. '3',
  25. 'The first option was correct'
  26. );
  27. assert.equal(
  28. first.text,
  29. 'Three',
  30. 'The first option was correct'
  31. );
  32. });
  33. test('multiple default selections returned', function (assert) {
  34. var $select = $(
  35. '<select multiple>' +
  36. '<option selected>One</option>' +
  37. '<option>Two</option>' +
  38. '<option value="3" selected>Three</option>' +
  39. '</select>'
  40. );
  41. var options = new Options({});
  42. var select = new Select2($select, options);
  43. var items = select.data();
  44. assert.equal(
  45. items.length,
  46. 2,
  47. 'The two selected items should be returned'
  48. );
  49. var first = items[0];
  50. assert.equal(
  51. first.id,
  52. 'One',
  53. 'The first option was correct'
  54. );
  55. var second = items[1];
  56. assert.equal(
  57. second.id,
  58. '3',
  59. 'The option value should be pulled correctly'
  60. );
  61. });
  62. module('select2(val)');
  63. test('single value matches jquery value', function (assert) {
  64. var $select = $(
  65. '<select>' +
  66. '<option>One</option>' +
  67. '<option>Two</option>' +
  68. '<option value="3" selected>Three</option>' +
  69. '</select>'
  70. );
  71. var options = new Options({});
  72. var select = new Select2($select, options);
  73. var value = select.val();
  74. assert.equal(
  75. value,
  76. '3',
  77. 'The value should match the option tag attribute'
  78. );
  79. assert.equal(
  80. value,
  81. $select.val(),
  82. 'The value should match the jquery value'
  83. );
  84. });
  85. test('multiple value matches the jquery value', function (assert) {
  86. var $select = $(
  87. '<select multiple>' +
  88. '<option selected>One</option>' +
  89. '<option>Two</option>' +
  90. '<option value="3" selected>Three</option>' +
  91. '</select>'
  92. );
  93. var options = new Options({});
  94. var select = new Select2($select, options);
  95. var value = select.val();
  96. assert.equal(
  97. value.length,
  98. 2,
  99. 'Two options should be selected'
  100. );
  101. assert.deepEqual(
  102. value,
  103. ['One', '3'],
  104. 'The values should match the option tag attribute'
  105. );
  106. assert.deepEqual(
  107. value,
  108. $select.val(),
  109. 'The values should match the jquery values'
  110. );
  111. });