minimumInputLength-tests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. module('Data adapters - Minimum input length');
  2. var MinimumInputLength = require('select2/data/minimumInputLength');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var Utils = require('select2/utils');
  6. function StubData () {
  7. this.called = false;
  8. }
  9. StubData.prototype.query = function (params, callback) {
  10. this.called = true;
  11. };
  12. var MinimumData = Utils.Decorate(StubData, MinimumInputLength);
  13. test('0 never displays the notice', function (assert) {
  14. var zeroOptions = new Options({
  15. minimumInputLength: 0
  16. });
  17. var data = new MinimumData(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 MinimumData(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. minimumInputLength: -1
  34. });
  35. var data = new MinimumData(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 MinimumData(null, negativeOptions);
  44. data.query({
  45. term: 'test'
  46. });
  47. assert.ok(data.called);
  48. });
  49. test('triggers when input is not long enough', function (assert) {
  50. var options = new Options({
  51. minimumInputLength: 10
  52. });
  53. var data = new MinimumData(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);
  61. });
  62. test('does not trigger when equal', function (assert) {
  63. var options = new Options({
  64. minimumInputLength: 10
  65. });
  66. var data = new MinimumData(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 greater', function (assert) {
  76. var options = new Options({
  77. minimumInputLength: 10
  78. });
  79. var data = new MinimumData(null, options);
  80. data.trigger = function () {
  81. assert.ok(false, 'The event should not be triggered.');
  82. };
  83. data.query({
  84. term: '12345678901'
  85. });
  86. assert.ok(data.called);
  87. });
  88. test('works with null term', function (assert) {
  89. var options = new Options({
  90. minimumInputLength: 1
  91. });
  92. var data = new MinimumData(null, options);
  93. data.trigger = function () {
  94. assert.ok(true, 'The event should be triggered');
  95. };
  96. data.query({});
  97. assert.ok(!data.called);
  98. });