forked from oussamahamdaoui/forgJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.test.js
40 lines (32 loc) · 871 Bytes
/
rule.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { Rule } = require('../../src');
test('adding a custom rule', () => {
Rule.addCustom('customInteger', {
min: (val, min) => val - min > 0,
max: (val, max) => val - max < 0,
equal: (val, equal) => val === equal,
type: val => Number.isInteger(val) && val > 0 && val < 100,
});
const customInteger = new Rule({
type: 'customInteger',
min: 10,
}, null);
expect(customInteger.test(11)).toBe(true);
expect(customInteger.test(200)).toBe(false);
});
test('throwing when type not exist', () => {
expect(() => {
const customInteger = new Rule({
type: 'something',
min: 10,
}, null);
customInteger.test(11);
}).toThrow();
});
test('throwing when type is undefined', () => {
expect(() => {
const customInteger = new Rule({
min: 10,
}, null);
customInteger.test(11);
}).toThrow();
});