No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inputData-tests.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var container = new MockContainer();
  20. adapter.bind(container, $('<div></div>'));
  21. adapter.select({
  22. id: 'test'
  23. });
  24. assert.equal(
  25. $element.val(),
  26. 'test',
  27. 'The id of the item should be the value'
  28. );
  29. });
  30. test('unselect the single selected option clears the value', function (assert) {
  31. var options = new Options({
  32. data: [
  33. {
  34. id: 'test',
  35. text: 'Test',
  36. selected: true
  37. }
  38. ]
  39. });
  40. var $element = $('<input />');
  41. var adapter = new InputAdapter($element, options);
  42. var container = new MockContainer();
  43. adapter.bind(container, $('<div></div>'));
  44. adapter.unselect({
  45. id: 'test'
  46. });
  47. assert.equal(
  48. $element.val(),
  49. '',
  50. 'The id should no longer be in the value'
  51. );
  52. });
  53. test('options can be unselected individually', function (assert) {
  54. var options = new Options({
  55. data: [
  56. {
  57. id: 'test',
  58. text: 'Test'
  59. },
  60. {
  61. id: 'test2',
  62. text: 'Test2'
  63. },
  64. {
  65. id: 'test3',
  66. text: 'Test3'
  67. }
  68. ]
  69. });
  70. var $element = $('<input />');
  71. $element.val('test,test2,test3');
  72. var adapter = new InputAdapter($element, options);
  73. var container = new MockContainer();
  74. adapter.bind(container, $('<div></div>'));
  75. adapter.unselect({
  76. id: 'test2'
  77. });
  78. assert.equal(
  79. $element.val(),
  80. 'test,test3',
  81. 'The value should contain all the still selected options'
  82. );
  83. });
  84. test('default values can be set', function (assert) {
  85. assert.expect(4);
  86. var options = new Options({
  87. data: [
  88. {
  89. id: 'test',
  90. text: 'Test'
  91. }
  92. ]
  93. });
  94. var $element = $('<input value="test" />');
  95. var adapter = new InputAdapter($element, options);
  96. var container = new MockContainer();
  97. adapter.bind(container, $('<div></div>'));
  98. adapter.current(function (data) {
  99. assert.equal(
  100. data.length,
  101. 1,
  102. 'There should only be a single selected option'
  103. );
  104. var item = data[0];
  105. assert.equal(item.id, 'test');
  106. assert.equal(item.text, 'Test');
  107. });
  108. assert.equal(
  109. $element.val(),
  110. 'test',
  111. 'The value should not have been altered'
  112. );
  113. });
  114. test('no default value', function (assert) {
  115. assert.expect(2);
  116. var options = new Options({
  117. data: [
  118. {
  119. id: 'test',
  120. text: 'Test'
  121. }
  122. ]
  123. });
  124. var $element = $('<input />');
  125. var adapter = new InputAdapter($element, options);
  126. var container = new MockContainer();
  127. adapter.bind(container, $('<div></div>'));
  128. adapter.current(function (data) {
  129. assert.equal(
  130. data.length,
  131. 0,
  132. 'There should be no selected options'
  133. );
  134. });
  135. assert.equal(
  136. $element.val(),
  137. '',
  138. 'The value should not have been altered'
  139. );
  140. });