inputData-tests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. module('Data adapters - <input> compatibility');
  2. var $ = require('jquery');
  3. var Options = require('select2/options');
  4. var Utils = require('select2/utils');
  5. var ArrayData = require('select2/data/array');
  6. var InputData = require('select2/compat/inputData');
  7. var InputAdapter = Utils.Decorate(ArrayData, InputData);
  8. test('test that options can be selected', function (assert) {
  9. var options = new Options({
  10. data: [
  11. {
  12. id: 'test',
  13. text: 'Test'
  14. }
  15. ]
  16. });
  17. var $element = $('<input />');
  18. var adapter = new InputAdapter($element, options);
  19. adapter.select({
  20. id: 'test'
  21. });
  22. assert.equal(
  23. $element.val(),
  24. 'test',
  25. 'The id of the item should be the value'
  26. );
  27. });
  28. test('unselect the single selected option clears the value', function (assert) {
  29. var options = new Options({
  30. data: [
  31. {
  32. id: 'test',
  33. text: 'Test',
  34. selected: true
  35. }
  36. ]
  37. });
  38. var $element = $('<input />');
  39. var adapter = new InputAdapter($element, options);
  40. adapter.unselect({
  41. id: 'test'
  42. });
  43. assert.equal(
  44. $element.val(),
  45. '',
  46. 'The id should no longer be in the value'
  47. );
  48. });
  49. test('options can be unselected individually', function (assert) {
  50. var options = new Options({
  51. data: [
  52. {
  53. id: 'test',
  54. text: 'Test'
  55. },
  56. {
  57. id: 'test2',
  58. text: 'Test2'
  59. },
  60. {
  61. id: 'test3',
  62. text: 'Test3'
  63. }
  64. ]
  65. });
  66. var $element = $('<input />');
  67. $element.val('test,test2,test3');
  68. var adapter = new InputAdapter($element, options);
  69. adapter.unselect({
  70. id: 'test2'
  71. });
  72. assert.equal(
  73. $element.val(),
  74. 'test,test3',
  75. 'The value should contain all the still selected options'
  76. );
  77. });
  78. test('default values can be set', function (assert) {
  79. assert.expect(4);
  80. var options = new Options({
  81. data: [
  82. {
  83. id: 'test',
  84. text: 'Test'
  85. }
  86. ]
  87. });
  88. var $element = $('<input value="test" />');
  89. var adapter = new InputAdapter($element, options);
  90. adapter.current(function (data) {
  91. assert.equal(
  92. data.length,
  93. 1,
  94. 'There should only be a single selected option'
  95. );
  96. var item = data[0];
  97. assert.equal(item.id, 'test');
  98. assert.equal(item.text, 'Test');
  99. });
  100. assert.equal(
  101. $element.val(),
  102. 'test',
  103. 'The value should not have been altered'
  104. );
  105. });
  106. test('no default value', function (assert) {
  107. assert.expect(2);
  108. var options = new Options({
  109. data: [
  110. {
  111. id: 'test',
  112. text: 'Test'
  113. }
  114. ]
  115. });
  116. var $element = $('<input />');
  117. var adapter = new InputAdapter($element, options);
  118. adapter.current(function (data) {
  119. assert.equal(
  120. data.length,
  121. 0,
  122. 'There should be no selected options'
  123. );
  124. });
  125. assert.equal(
  126. $element.val(),
  127. '',
  128. 'The value should not have been altered'
  129. );
  130. });