Skip to content

Commit db0c72b

Browse files
authored
feat: Console error additional info (#35)
* feat: Additional console of error * clean up
1 parent 93e65b3 commit db0c72b

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/utils/validateUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
ValidateOptions,
77
ValidateMessages,
88
RuleObject,
9-
Rule,
109
StoreValue,
1110
} from '../interface';
1211
import { setValues } from './valueUtil';
@@ -102,6 +101,7 @@ async function validateRule(
102101
: message),
103102
);
104103
} else {
104+
console.error(errObj);
105105
result = [(messages.default as (() => string))()];
106106
}
107107
}

tests/validate.test.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,60 @@ describe('Form.Validate', () => {
6868
});
6969
});
7070

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+
},
8385
},
84-
},
85-
]}
86-
/>
87-
</Form>,
88-
);
86+
]}
87+
/>
88+
</Form>,
89+
);
8990

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+
});
9399

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+
});
97125
});
98126

99127
it('fail validate if throw', async () => {

0 commit comments

Comments
 (0)