@@ -68,32 +68,60 @@ describe('Form.Validate', () => {
68
68
} ) ;
69
69
} ) ;
70
70
71
- it ( 'customize validator' , async ( ) => {
72
- const wrapper = mount (
73
- < Form >
74
- < InfoField
75
- name = "username"
76
- rules = { [
77
- {
78
- async validator ( _ , value ) {
79
- if ( value !== 'bamboo' ) {
80
- return Promise . reject ( new Error ( 'should be bamboo!' ) ) ;
81
- }
82
- return '' ;
71
+ describe ( 'customize validator' , ( ) => {
72
+ it ( 'work' , async ( ) => {
73
+ const wrapper = mount (
74
+ < Form >
75
+ < InfoField
76
+ name = "username"
77
+ rules = { [
78
+ {
79
+ async validator ( _ , value ) {
80
+ if ( value !== 'bamboo' ) {
81
+ return Promise . reject ( new Error ( 'should be bamboo!' ) ) ;
82
+ }
83
+ return '' ;
84
+ } ,
83
85
} ,
84
- } ,
85
- ] }
86
- />
87
- </ Form > ,
88
- ) ;
86
+ ] }
87
+ />
88
+ </ Form > ,
89
+ ) ;
89
90
90
- // Wrong value
91
- await changeValue ( wrapper , 'light' ) ;
92
- matchError ( wrapper , 'should be bamboo!' ) ;
91
+ // Wrong value
92
+ await changeValue ( wrapper , 'light' ) ;
93
+ matchError ( wrapper , 'should be bamboo!' ) ;
94
+
95
+ // Correct value
96
+ await changeValue ( wrapper , 'bamboo' ) ;
97
+ matchError ( wrapper , false ) ;
98
+ } ) ;
93
99
94
- // Correct value
95
- await changeValue ( wrapper , 'bamboo' ) ;
96
- matchError ( wrapper , false ) ;
100
+ it ( 'should error if throw in validate' , async ( ) => {
101
+ const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
102
+ const wrapper = mount (
103
+ < Form >
104
+ < InfoField
105
+ name = "username"
106
+ rules = { [
107
+ {
108
+ validator ( ) {
109
+ throw new Error ( 'without thinking' ) ;
110
+ } ,
111
+ } ,
112
+ ] }
113
+ />
114
+ </ Form > ,
115
+ ) ;
116
+
117
+ await changeValue ( wrapper , 'light' ) ;
118
+ matchError ( wrapper , "Validation error on field 'username'" ) ;
119
+
120
+ const consoleErr = String ( errorSpy . mock . calls [ 0 ] [ 0 ] ) ;
121
+ expect ( consoleErr ) . toBe ( 'Error: without thinking' ) ;
122
+
123
+ errorSpy . mockRestore ( ) ;
124
+ } ) ;
97
125
} ) ;
98
126
99
127
it ( 'fail validate if throw' , async ( ) => {
0 commit comments