-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbegin-with.spec.js
102 lines (86 loc) · 2.8 KB
/
begin-with.spec.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { expect } from 'chai';
import validator from '../../lib';
describe('BeginWith validator', () => {
const objectRules = {
pet: [
{name: 'beginWith', params: [['dog', 'cat', 'fish']]}
]
};
it('should success for single item', () => {
const acceptedInputs = [
'dog',
'doge',
'dog kintamani',
'dog shiba inu',
'cat woman',
'fish erman'
];
acceptedInputs.forEach(acceptedInput => {
const result = validator.validate(objectRules, {pet: acceptedInput});
const err = result.messages;
expect(result.success).to.equal(true);
expect(err).to.not.have.property('pet');
});
});
it('should success for array item', () => {
const acceptedInputs = [
[],
['dog', 'catherine'],
['dog', 'doge', 'dog kintamani', 'dog shiba inu', 'cat woman', 'fish arowana']
];
acceptedInputs.forEach(acceptedInput => {
const result = validator.validate(objectRules, {pet: acceptedInput});
const err = result.messages;
expect(result.success).to.equal(true);
expect(err).to.not.have.property('pet');
});
});
it('should fail for single item not beginning with string in list', () => {
const rejectedInputs = [
'',
'd',
'do',
'dug',
'dug up',
'cow',
'fisx',
null,
undefined
];
rejectedInputs.forEach(rejectedInput => {
const result = validator.validate(objectRules, {pet: rejectedInput});
const err = result.messages;
expect(result.success).to.equal(false);
expect(err).to.have.property('pet');
expect(err.pet['beginWith:$1']).to.equal('Pet must begin with one of dog,cat,fish.');
});
});
it('should fail for array item which contains item not beginning with the string in list', () => {
const rejectedInputs = [
['doge', 'dug'],
['dog kintamani', 'd'],
['dog kintamani', 'do']
];
rejectedInputs.forEach(rejectedInput => {
const result = validator.validate(objectRules, {pet: rejectedInput});
const err = result.messages;
expect(result.success).to.equal(false);
expect(err).to.have.property('pet');
expect(err.pet['beginWith:$1']).to.equal('Pet must begin with one of dog,cat,fish.');
});
});
it('should fail for non string or non array of strings', () => {
const rejectedInputs = [
['doge', 'cat', {wild: 'object'}],
['dog', 'catherine', 123],
{wild: 'object'}
];
rejectedInputs.forEach(rejectedInput => {
const result = validator.validate(objectRules, {pet: rejectedInput});
const err = result.messages;
expect(result.success).to.equal(false);
expect(err).to.have.property('pet');
expect(err.pet['beginWith:$1']).to.equal('Pet must begin with one of dog,cat,fish.');
});
});
});