-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
58 lines (43 loc) · 1.29 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const makeValidator = require('./lib');
const {rules: {string}} = require('v6e');
const {SubmissionError} = require('redux-form');
const schema = {
username: string(),
};
const successful = {
username: 'hunter',
};
const error = {
illegal: 'hacking!!',
nested: {
illegal: 'hi',
fields: 'hi',
}
};
it('should not throw when when the validation succeeds', () => {
return makeValidator(schema, {strict: true})(successful);
});
it('should throw a SubmissionError when the validation fails', () => {
expect.assertions(2);
return makeValidator(schema, {strict: true})(error)
.catch(e => {
expect(e).toBeInstanceOf(SubmissionError);
expect(e.errors).toEqual({
username: 'Must be a string',
_error: [
'illegal: Illegal attribute.',
'nested: Illegal attribute.',
]
});
});
});
it('should allow injection of the SubmissionError class', () => {
expect.assertions(1);
class CustomSubmissionError {
}
return makeValidator(schema, {strict: true}, {SubmissionError: CustomSubmissionError})(error)
.catch(e => {
expect(e).toBeInstanceOf(CustomSubmissionError);
});
});