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.

maximumInputLength-tests.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. module('Data adapters - Maximum input length');
  2. var MaximumInputLength = require('select2/data/maximumInputLength');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var Utils = require('select2/utils');
  6. function MaximumInputStub () {
  7. this.called = false;
  8. }
  9. MaximumInputStub.prototype.query = function (params, callback) {
  10. this.called = true;
  11. };
  12. var MaximumInputData = Utils.Decorate(MaximumInputStub, MaximumInputLength);
  13. test('0 never displays the notice', function (assert) {
  14. var zeroOptions = new Options({
  15. maximumInputLength: 0
  16. });
  17. var data = new MaximumInputData(null, zeroOptions);
  18. data.trigger = function () {
  19. assert.ok(false, 'No events should be triggered');
  20. };
  21. data.query({
  22. term: ''
  23. });
  24. assert.ok(data.called);
  25. data = new MaximumInputData(null, zeroOptions);
  26. data.query({
  27. term: 'test'
  28. });
  29. assert.ok(data.called);
  30. });
  31. test('< 0 never displays the notice', function (assert) {
  32. var negativeOptions = new Options({
  33. maximumInputLength: -1
  34. });
  35. var data = new MaximumInputData(null, negativeOptions);
  36. data.trigger = function () {
  37. assert.ok(false, 'No events should be triggered');
  38. };
  39. data.query({
  40. term: ''
  41. });
  42. assert.ok(data.called);
  43. data = new MaximumInputData(null, negativeOptions);
  44. data.query({
  45. term: 'test'
  46. });
  47. assert.ok(data.called);
  48. });
  49. test('triggers when input is too long', function (assert) {
  50. var options = new Options({
  51. maximumInputLength: 1
  52. });
  53. var data = new MaximumInputData(null, options);
  54. data.trigger = function () {
  55. assert.ok(true, 'The event should be triggered.');
  56. };
  57. data.query({
  58. term: 'no'
  59. });
  60. assert.ok(!data.called, 'The adapter should not be called');
  61. });
  62. test('does not trigger when equal', function (assert) {
  63. var options = new Options({
  64. maximumInputLength: 10
  65. });
  66. var data = new MaximumInputData(null, options);
  67. data.trigger = function () {
  68. assert.ok(false, 'The event should not be triggered.');
  69. };
  70. data.query({
  71. term: '1234567890'
  72. });
  73. assert.ok(data.called);
  74. });
  75. test('does not trigger when less', function (assert) {
  76. var options = new Options({
  77. maximumInputLength: 10
  78. });
  79. var data = new MaximumInputData(null, options);
  80. data.trigger = function () {
  81. assert.ok(false, 'The event should not be triggered.');
  82. };
  83. data.query({
  84. term: '123'
  85. });
  86. assert.ok(data.called);
  87. });
  88. test('works with null term', function (assert) {
  89. var options = new Options({
  90. maximumInputLength: 1
  91. });
  92. var data = new MaximumInputData(null, options);
  93. data.trigger = function () {
  94. assert.ok(false, 'The event should not be triggered');
  95. };
  96. data.query({});
  97. assert.ok(data.called);
  98. });