Home Reference Source Test

test/Rule/int.test.js

  1. const { Rule } = require('../../src');
  2.  
  3. test('type test not int is false', () => {
  4. const intRule = new Rule({
  5. type: 'int',
  6. }, null);
  7.  
  8. expect(intRule.test([])).toBe(false);
  9. });
  10.  
  11. test('type test', () => {
  12. const intRule = new Rule({
  13. type: 'int',
  14. }, null);
  15.  
  16. expect(intRule.test(2)).toBe(true);
  17. });
  18.  
  19. test('max is true if < 100', () => {
  20. const intRule = new Rule({
  21. type: 'int',
  22. max: 100,
  23. }, null);
  24.  
  25. expect(intRule.test(99)).toBe(true);
  26. });
  27.  
  28. test('max is false if > 100', () => {
  29. const intRule = new Rule({
  30. type: 'int',
  31. max: 100,
  32. }, null);
  33.  
  34. expect(intRule.test(101)).toBe(false);
  35. });
  36.  
  37. test('equal returns true if 100', () => {
  38. const intRule = new Rule({
  39. type: 'int',
  40. equal: 100,
  41. }, null);
  42.  
  43. expect(intRule.test(100)).toBe(true);
  44. });
  45.  
  46.  
  47. test('custom rulle should return true', () => {
  48. const intRule = new Rule({
  49. type: 'int',
  50. custom: val => val % 2 === 0,
  51. }, null);
  52.  
  53. expect(intRule.test(4)).toBe(true);
  54. });
  55.  
  56. test('custom rulle should return false', () => {
  57. const intRule = new Rule({
  58. type: 'int',
  59. custom: val => val % 2 === 0,
  60. }, null);
  61.  
  62. expect(intRule.test(3)).toBe(false);
  63. });
  64.  
  65. test('mixng rulles returns true', () => {
  66. const intRule = new Rule({
  67. type: 'int',
  68. max: 50,
  69. min: 5,
  70. custom: val => val % 2 === 0,
  71. }, null);
  72.  
  73. expect(intRule.test(6)).toBe(true);
  74. });
  75.  
  76. test('mixng rulles returns false', () => {
  77. const intRule = new Rule({
  78. type: 'int',
  79. max: 50,
  80. min: 5,
  81. custom: val => val % 2 === 0,
  82. }, null);
  83.  
  84. expect(intRule.test(1)).toBe(false);
  85. });
  86.  
  87. test('throws error when test doesn\'t exist', () => {
  88. expect(() => {
  89. const intRule = new Rule({
  90. type: 'int',
  91. test: false,
  92. }, null);
  93. intRule.test(3);
  94. }).toThrow();
  95. });
  96.  
  97. test('returns true if optional set to true and patram undefined', () => {
  98. const intRule = new Rule({
  99. type: 'int',
  100. optional: true,
  101. }, null);
  102. expect(intRule.test()).toBe(true);
  103. });
  104.  
  105. test('returns false if optional set to true and patram dont match', () => {
  106. const intRule = new Rule({
  107. type: 'int',
  108. optional: true,
  109. }, null);
  110. expect(intRule.test('hello')).toBe(false);
  111. });
  112.  
  113. test('returns true if optional set to true and patram match', () => {
  114. const intRule = new Rule({
  115. type: 'int',
  116. optional: true,
  117. }, null);
  118. expect(intRule.test(150)).toBe(true);
  119. });
  120.  
  121. test('returns true if optional set to false and patram match', () => {
  122. const intRule = new Rule({
  123. type: 'int',
  124. optional: false,
  125. }, null);
  126. expect(intRule.test(150)).toBe(true);
  127. });
  128.  
  129. test('returns false if optional set to false and patram dont match', () => {
  130. const intRule = new Rule({
  131. type: 'int',
  132. optional: false,
  133. }, null);
  134. expect(intRule.test('150')).toBe(false);
  135. });