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.

maximumSelectionLength-tests.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. module('Data adapters - Maximum selection length');
  2. var SelectData = require('select2/data/select');
  3. var MaximumSelectionLength = require('select2/data/maximumSelectionLength');
  4. var $ = require('jquery');
  5. var Options = require('select2/options');
  6. var Utils = require('select2/utils');
  7. var MaximumSelectionData = Utils.Decorate(SelectData, MaximumSelectionLength);
  8. test('0 never displays the notice', function (assert) {
  9. assert.expect(3);
  10. var $select = $('#qunit-fixture .multiple');
  11. var zeroOptions = new Options({
  12. maximumSelectionLength: 0
  13. });
  14. var container = new MockContainer();
  15. var data = new MaximumSelectionData($select, zeroOptions);
  16. data.bind(container, null);
  17. data.on('results:message', function () {
  18. assert.ok(false, 'The message should not be displayed');
  19. });
  20. data.query({
  21. term: ''
  22. }, function () {
  23. assert.ok(true, 'The results should be queried');
  24. });
  25. $select.val(['One']);
  26. data.query({
  27. term: ''
  28. }, function () {
  29. assert.ok(true, 'The results should be queried');
  30. });
  31. $select.val(['One', 'Two']);
  32. data.query({
  33. term: ''
  34. }, function () {
  35. assert.ok(true, 'The results should be queried');
  36. });
  37. });
  38. test('< 0 never displays the notice', function (assert) {
  39. assert.expect(3);
  40. var $select = $('#qunit-fixture .multiple');
  41. var negativeOptions = new Options({
  42. maximumSelectionLength: -1
  43. });
  44. var container = new MockContainer();
  45. var data = new MaximumSelectionData($select, negativeOptions);
  46. data.bind(container, null);
  47. data.on('results:message', function () {
  48. assert.ok(false, 'The message should not be displayed');
  49. });
  50. data.query({
  51. term: ''
  52. }, function () {
  53. assert.ok(true, 'The results should be queried');
  54. });
  55. $select.val(['One']);
  56. data.query({
  57. term: ''
  58. }, function () {
  59. assert.ok(true, 'The results should be queried');
  60. });
  61. $select.val(['One', 'Two']);
  62. data.query({
  63. term: ''
  64. }, function () {
  65. assert.ok(true, 'The results should be queried');
  66. });
  67. });
  68. test('triggers when >= 1 selection' , function (assert) {
  69. assert.expect(2);
  70. var $select = $('#qunit-fixture .multiple');
  71. var maxOfOneOptions = new Options({
  72. maximumSelectionLength: 1
  73. });
  74. var container = new MockContainer();
  75. var data = new MaximumSelectionData($select, maxOfOneOptions);
  76. data.bind(container, null);
  77. data.on('results:message', function () {
  78. assert.ok(true, 'The message should be displayed');
  79. });
  80. $select.val(['One']);
  81. data.query({
  82. term: ''
  83. }, function () {
  84. assert.ok(false, 'The results should not be queried');
  85. });
  86. $select.val(['One', 'Two']);
  87. data.query({
  88. term: ''
  89. }, function () {
  90. assert.ok(false, 'The results should not be queried');
  91. });
  92. });
  93. test('triggers after selection' , function (assert) {
  94. assert.expect(1);
  95. var $select = $('#qunit-fixture .multiple');
  96. var maxOfOneOptions = new Options({
  97. maximumSelectionLength: 1
  98. });
  99. var container = new MockContainer();
  100. var data = new MaximumSelectionData($select, maxOfOneOptions);
  101. data.bind(container, null);
  102. data.on('results:message', function () {
  103. assert.ok(true, 'The message should be displayed');
  104. });
  105. $select.val(['One']);
  106. container.trigger('select', {
  107. data: {}
  108. });
  109. });