select-tests.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. module('Data adapters - Select');
  2. var SelectData = require('select2/data/select');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var options = new Options({});
  6. test('current gets default for single', function (assert) {
  7. var $select = $('#qunit-fixture .single');
  8. var data = new SelectData($select, options);
  9. data.current(function (val) {
  10. assert.deepEqual(
  11. val,
  12. [{
  13. id: 'default',
  14. text: 'Default'
  15. }],
  16. 'The first option should be selected by default (by the browser).'
  17. );
  18. });
  19. });
  20. test('current gets default for multiple', function (assert) {
  21. var $select = $('#qunit-fixture .multiple');
  22. var data = new SelectData($select, options);
  23. data.current(function (val) {
  24. assert.deepEqual(
  25. val,
  26. [],
  27. 'Multiple selects have no default selection.'
  28. );
  29. });
  30. });
  31. test('current gets options with explicit value', function (assert) {
  32. var $select = $('#qunit-fixture .single');
  33. var data = new SelectData($select, options);
  34. $select.val('1');
  35. data.current(function (val) {
  36. assert.deepEqual(
  37. val,
  38. [{
  39. id: '1',
  40. text: 'One'
  41. }],
  42. 'The text and id should match the value and text for the option tag.'
  43. );
  44. });
  45. });
  46. test('current gets options with implicit value', function (assert) {
  47. var $select = $('#qunit-fixture .single');
  48. var data = new SelectData($select, options);
  49. $select.val('2');
  50. data.current(function (val) {
  51. assert.deepEqual(
  52. val,
  53. [{
  54. id: '2',
  55. text: '2'
  56. }],
  57. 'The text and id should match the text within the option tag.'
  58. );
  59. });
  60. });
  61. test('select works for single', function (assert) {
  62. var $select = $('#qunit-fixture .single');
  63. var data = new SelectData($select, options);
  64. assert.equal($select.val(), 'default');
  65. data.select({
  66. id: '1',
  67. text: 'One'
  68. });
  69. assert.equal($select.val(), '1');
  70. });
  71. test('multiple sets the value', function (assert) {
  72. var $select = $('#qunit-fixture .multiple');
  73. var data = new SelectData($select, options);
  74. assert.equal($select.val(), null);
  75. data.select({
  76. id: 'default',
  77. text: 'Default'
  78. });
  79. assert.deepEqual($select.val(), ['default']);
  80. });
  81. test('multiple adds to the old value', function (assert) {
  82. var $select = $('#qunit-fixture .multiple');
  83. var data = new SelectData($select, options);
  84. $select.val(['2']);
  85. assert.deepEqual($select.val(), ['2']);
  86. data.select({
  87. id: 'default',
  88. text: 'Default'
  89. });
  90. assert.deepEqual($select.val(), ['default', '2']);
  91. });