min-length-validator.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {minLengthValidator} from './min-length-validator.js';
  4. describe('minLengthValidator', function () {
  5. it('returns true if the "options" argument is false', function () {
  6. const res = minLengthValidator('test', false, {});
  7. expect(res).to.be.true;
  8. });
  9. it('returns true for undefined and null values', function () {
  10. const res1 = minLengthValidator(undefined, 10, {});
  11. const res2 = minLengthValidator(null, 10, {});
  12. expect(res1).to.be.true;
  13. expect(res2).to.be.true;
  14. });
  15. it('requires the "value" argument as a String or an Array', function () {
  16. const throwable = v => () =>
  17. minLengthValidator(v, 0, {
  18. validatorName: 'myValidator',
  19. });
  20. const error = v =>
  21. format(
  22. 'The property validator "myValidator" requires a String ' +
  23. 'or an Array value, but %s given.',
  24. v,
  25. );
  26. expect(throwable(10)).to.throw(error('10'));
  27. expect(throwable(0)).to.throw(error('0'));
  28. expect(throwable(true)).to.throw(error('true'));
  29. expect(throwable(false)).to.throw(error('false'));
  30. expect(throwable({})).to.throw(error('Object'));
  31. expect(throwable(() => undefined)).to.throw(error('Function'));
  32. throwable('str')();
  33. throwable('')();
  34. throwable([1, 2, 3])();
  35. throwable([])();
  36. throwable(undefined)();
  37. throwable(null)();
  38. });
  39. it('requires the "options" argument to be a number', function () {
  40. const throwable = v => () =>
  41. minLengthValidator('test', v, {
  42. validatorName: 'myValidator',
  43. });
  44. const error = v =>
  45. format(
  46. 'The validator "myValidator" requires the "options" argument ' +
  47. 'as a Number, but %s given.',
  48. v,
  49. );
  50. expect(throwable('str')).to.throw(error('"str"'));
  51. expect(throwable('')).to.throw(error('""'));
  52. expect(throwable(true)).to.throw(error('true'));
  53. expect(throwable(undefined)).to.throw(error('undefined'));
  54. expect(throwable(null)).to.throw(error('null'));
  55. expect(throwable({})).to.throw(error('Object'));
  56. expect(throwable([])).to.throw(error('Array'));
  57. expect(throwable(() => undefined)).to.throw(error('Function'));
  58. throwable(10)();
  59. throwable(0)();
  60. throwable(false)();
  61. });
  62. describe('a string value', function () {
  63. it('returns false if the value length is lower than the min length option', function () {
  64. const res = minLengthValidator('12', 3, {});
  65. expect(res).to.be.false;
  66. });
  67. it('returns true if the value length is equal to the min length option', function () {
  68. const res = minLengthValidator('123', 3, {});
  69. expect(res).to.be.true;
  70. });
  71. it('returns true if the value length is greater than the min length option', function () {
  72. const res = minLengthValidator('1234', 3, {});
  73. expect(res).to.be.true;
  74. });
  75. });
  76. describe('an array value', function () {
  77. it('returns false if the value length is lower than the min length option', function () {
  78. const res = minLengthValidator([1, 2], 3, {});
  79. expect(res).to.be.false;
  80. });
  81. it('returns true if the value length is equal to the min length option', function () {
  82. const res = minLengthValidator([1, 2, 3], 3, {});
  83. expect(res).to.be.true;
  84. });
  85. it('returns true if the value length is greater than the min length option', function () {
  86. const res = minLengthValidator([1, 2, 3, 4], 3, {});
  87. expect(res).to.be.true;
  88. });
  89. });
  90. });