maximumSelectionLength-tests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. module('Data adapters - Maximum selection length');
  2. var MaximumSelectionLength = require('select2/data/maximumSelectionLength');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var Utils = require('select2/utils');
  6. function MaximumSelectionStub () {
  7. this.called = false;
  8. this.currentData = [];
  9. }
  10. MaximumSelectionStub.prototype.current = function (callback) {
  11. callback(this.currentData);
  12. };
  13. MaximumSelectionStub.prototype.val = function (val) {
  14. this.currentData.push(val);
  15. };
  16. MaximumSelectionStub.prototype.query = function (params, callback) {
  17. this.called = true;
  18. };
  19. var MaximumSelectionData = Utils.Decorate(
  20. MaximumSelectionStub,
  21. MaximumSelectionLength
  22. );
  23. test('0 never displays the notice', function (assert) {
  24. var zeroOptions = new Options({
  25. maximumSelectionLength: 0
  26. });
  27. var data = new MaximumSelectionData(null, zeroOptions);
  28. data.trigger = function () {
  29. assert.ok(false, 'No events should be triggered');
  30. };
  31. data.query({
  32. term: ''
  33. });
  34. assert.ok(data.called);
  35. data = new MaximumSelectionData(null, zeroOptions);
  36. data.trigger = function () {
  37. assert.ok(false, 'No events should be triggered');
  38. };
  39. data.val('1');
  40. data.query({
  41. term: ''
  42. });
  43. assert.ok(data.called);
  44. data = new MaximumSelectionData(null, zeroOptions);
  45. data.trigger = function () {
  46. assert.ok(false, 'No events should be triggered');
  47. };
  48. data.val('1');
  49. data.val('2');
  50. data.query({
  51. term: ''
  52. });
  53. assert.ok(data.called);
  54. });
  55. test('< 0 never displays the notice', function (assert) {
  56. var negativeOptions = new Options({
  57. maximumSelectionLength: -1
  58. });
  59. var data = new MaximumSelectionData(null, negativeOptions);
  60. data.trigger = function () {
  61. assert.ok(false, 'No events should be triggered');
  62. };
  63. data.query({
  64. term: ''
  65. });
  66. assert.ok(data.called);
  67. data = new MaximumSelectionData(null, negativeOptions);
  68. data.trigger = function () {
  69. assert.ok(false, 'No events should be triggered');
  70. };
  71. data.val('1');
  72. data.query({
  73. term: ''
  74. });
  75. assert.ok(data.called);
  76. data = new MaximumSelectionData(null, negativeOptions);
  77. data.trigger = function () {
  78. assert.ok(false, 'No events should be triggered');
  79. };
  80. data.val('1');
  81. data.val('2');
  82. data.query({
  83. term: ''
  84. });
  85. assert.ok(data.called);
  86. });
  87. test('triggers when >= 1 selection' , function (assert) {
  88. var maxOfOneOptions = new Options({
  89. maximumSelectionLength: 1
  90. });
  91. var data = new MaximumSelectionData(null, maxOfOneOptions);
  92. data.trigger = function () {
  93. assert.ok(false, 'No events should be triggered');
  94. };
  95. data.query({
  96. term: ''
  97. });
  98. assert.ok(data.called);
  99. data = new MaximumSelectionData(null, maxOfOneOptions);
  100. data.trigger = function () {
  101. assert.ok(true, 'The event should be triggered.');
  102. };
  103. data.val('1');
  104. data.query({
  105. term: ''
  106. });
  107. assert.ok(!data.called);
  108. });
  109. test('triggers when >= 2 selections' , function (assert) {
  110. var maxOfTwoOptions = new Options({
  111. maximumSelectionLength: 2
  112. });
  113. var data = new MaximumSelectionData(null, maxOfTwoOptions);
  114. data.trigger = function () {
  115. assert.ok(false, 'No events should be triggered');
  116. };
  117. data.query({
  118. term: ''
  119. });
  120. assert.ok(data.called);
  121. data = new MaximumSelectionData(null, maxOfTwoOptions);
  122. data.trigger = function () {
  123. assert.ok(false, 'No events should be triggered');
  124. };
  125. data.val('1');
  126. data.query({
  127. term: ''
  128. });
  129. assert.ok(data.called);
  130. data = new MaximumSelectionData(null, maxOfTwoOptions);
  131. data.trigger = function () {
  132. assert.ok(true, 'The event should be triggered.');
  133. };
  134. data.val('1');
  135. data.val('2');
  136. data.query({
  137. term: ''
  138. });
  139. assert.ok(!data.called);
  140. });