deprecated-tests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. module('Options - Deprecated - initSelection');
  2. var $ = require('jquery');
  3. var Options = require('select2/options');
  4. test('converted into dataAdapter.current', function (assert) {
  5. assert.expect(5);
  6. var $test = $('<select></select>');
  7. var called = false;
  8. var options = new Options({
  9. initSelection: function ($element, callback) {
  10. called = true;
  11. callback([{
  12. id: '1',
  13. text: '2'
  14. }]);
  15. }
  16. }, $test);
  17. assert.ok(!called, 'initSelection should not have been called');
  18. var DataAdapter = options.get('dataAdapter');
  19. var data = new DataAdapter($test, options);
  20. data.current(function (data) {
  21. assert.equal(
  22. data.length,
  23. 1,
  24. 'There should have only been one object selected'
  25. );
  26. var item = data[0];
  27. assert.equal(
  28. item.id,
  29. '1',
  30. 'The id should have been set by initSelection'
  31. );
  32. assert.equal(
  33. item.text,
  34. '2',
  35. 'The text should have been set by initSelection'
  36. );
  37. });
  38. assert.ok(called, 'initSelection should have been called');
  39. });
  40. test('single option converted to array automatically', function (assert) {
  41. assert.expect(2);
  42. var $test = $('<select></select>');
  43. var called = false;
  44. var options = new Options({
  45. initSelection: function ($element, callback) {
  46. called = true;
  47. callback({
  48. id: '1',
  49. text: '2'
  50. });
  51. }
  52. }, $test);
  53. var DataAdapter = options.get('dataAdapter');
  54. var data = new DataAdapter($test, options);
  55. data.current(function (data) {
  56. assert.ok(
  57. $.isArray(data),
  58. 'The data should have been converted to an array'
  59. );
  60. });
  61. assert.ok(called, 'initSelection should have been called');
  62. });
  63. test('only called once', function (assert) {
  64. assert.expect(8);
  65. var $test = $('<select><option value="3" selected>4</option></select>');
  66. var called = 0;
  67. var options = new Options({
  68. initSelection: function ($element, callback) {
  69. called++;
  70. callback([{
  71. id: '1',
  72. text: '2'
  73. }]);
  74. }
  75. }, $test);
  76. var DataAdapter = options.get('dataAdapter');
  77. var data = new DataAdapter($test, options);
  78. data.current(function (data) {
  79. assert.equal(
  80. data.length,
  81. 1,
  82. 'There should have only been a single option'
  83. );
  84. var item = data[0];
  85. assert.equal(
  86. item.id,
  87. '1',
  88. 'The id should match the one given by initSelection'
  89. );
  90. assert.equal(
  91. item.text,
  92. '2',
  93. 'The text should match the one given by initSelection'
  94. );
  95. });
  96. assert.equal(
  97. called,
  98. 1,
  99. 'initSelection should have been called'
  100. );
  101. data.current(function (data) {
  102. assert.equal(
  103. data.length,
  104. 1,
  105. 'There should have only been a single option'
  106. );
  107. var item = data[0];
  108. assert.equal(
  109. item.id,
  110. '3',
  111. 'The id should match the value given in the DOM'
  112. );
  113. assert.equal(
  114. item.text,
  115. '4',
  116. 'The text should match the text given in the DOM'
  117. );
  118. });
  119. assert.equal(
  120. called,
  121. 1,
  122. 'initSelection should have only been called once'
  123. );
  124. });
  125. module('Options - Deprecated - query');
  126. test('converted into dataAdapter.query automatically', function (assert) {
  127. assert.expect(6);
  128. var $test = $('<select></select>');
  129. var called = false;
  130. var options = new Options({
  131. query: function (params) {
  132. called = true;
  133. params.callback({
  134. results: [
  135. {
  136. id: 'test',
  137. text: params.term
  138. }
  139. ]
  140. });
  141. }
  142. }, $test);
  143. assert.ok(!called, 'The query option should not have been called');
  144. var DataAdapter = options.get('dataAdapter');
  145. var data = new DataAdapter($test, options);
  146. data.query({
  147. term: 'term'
  148. }, function (data) {
  149. assert.ok(
  150. 'results' in data,
  151. 'It should have included the results key'
  152. );
  153. assert.equal(
  154. data.results.length,
  155. 1,
  156. 'There should have only been a single result returned'
  157. );
  158. var item = data.results[0];
  159. assert.equal(
  160. item.id,
  161. 'test',
  162. 'The id should have been returned from the query function'
  163. );
  164. assert.equal(
  165. item.text,
  166. 'term',
  167. 'The text should have matched the term that was passed in'
  168. );
  169. });
  170. assert.ok(called, 'The query function should have been called');
  171. });
  172. module('Options - deprecated - data-ajax-url');
  173. test('converted ajax-url to ajax--url automatically', function (assert) {
  174. var $test = $('<select data-ajax-url="test://url"></select>');
  175. var options = new Options({}, $test);
  176. assert.ok(
  177. options.get('ajax'),
  178. 'The `ajax` key was automatically created'
  179. );
  180. assert.equal(
  181. options.get('ajax').url,
  182. 'test://url',
  183. 'The `url` property for the `ajax` option was filled in correctly'
  184. );
  185. });
  186. test('converted select2-tags to data/tags automatically', function (assert) {
  187. var $test = $('<select data-select2-tags="original data"></select>');
  188. var options = new Options({}, $test);
  189. assert.ok(
  190. options.get('tags'),
  191. 'The `tags` key is automatically set to true'
  192. );
  193. assert.equal(
  194. options.get('data'),
  195. 'original data',
  196. 'The `data` key is created with the original data'
  197. );
  198. });