Skip to content

Commit

Permalink
feat: 重构代码&&添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
ckvv committed Sep 29, 2021
1 parent f6d9642 commit 46818a4
Show file tree
Hide file tree
Showing 10 changed files with 1,203 additions and 594 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Default `convertType` is `number`.
If type is `string`, there has four addition rules:

- `allowEmpty`(alias to `empty`) - allow empty string, default to false. If `rule.required` set to false, `allowEmpty` will be set to `true` by default.
- `format` - A `RegExp` to check string's format.
- `regexp` - A `RegExp` to check string's regexp.
- `max` - The maximum length of the string.
- `min` - The minimum length of the string.
- `trim` - Trim the string before check, default is `false`.
Expand Down Expand Up @@ -113,7 +113,7 @@ var rule = {
age: {
type: 'int',
min: 0,
msg: '需要是一个大于0的整数',
message: '需要是一个大于0的整数',
convert: false,
},
gender: ['male', 'female', 'unknown'],
Expand Down
149 changes: 149 additions & 0 deletions __test__/checkers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-env jest */
import { DEF_CHECKERS } from '../src/checkers';

describe(__filename, () => {
test('int', async () => {
const { int: checkInt } = DEF_CHECKERS;
expect(checkInt({
min: 8
}, 3)).toEqual('should bigger than 8');
expect(checkInt({
min: 8,
max: 20
}, 23)).toEqual('should smaller than 20');
expect(checkInt({
min: 8,
max: 20
}, undefined)).toEqual('should be a integer');
expect(checkInt({
min: 8,
max: 20
}, 1.1)).toEqual('should be a integer');
expect(checkInt({
min: 8,
max: 20
}, 9.0)).toEqual(null);
});

test('number', async () => {
const { number: checkNumber } = DEF_CHECKERS;
expect(checkNumber({
min: 8
}, 3)).toEqual('should bigger than 8');
expect(checkNumber({
min: 8,
max: 20
}, 23)).toEqual('should smaller than 20');

expect(checkNumber({
min: 8,
max: 20
}, 'test')).toEqual('should be a number');
expect(checkNumber({
min: 8,
max: 20
}, 10)).toEqual(null);
});

test('string', async () => {
const { string: checkString } = DEF_CHECKERS;
expect(checkString({
min: 8
}, 3)).toEqual('should be a string');
expect(checkString({
min: 8,
max: 20
}, 'he')).toEqual('length should bigger than 8');
expect(checkString({
min: 8,
max: 20
}, 'hello world')).toEqual(null);
expect(checkString({
min: 8,
max: 20
}, 'length should bigger than 8')).toEqual('length should smaller than 20');
expect(checkString({
min: 8,
max: 20,
regexp: /\d+/
}, '0123456789')).toEqual(null);
expect(checkString({
min: 8,
max: 20,
regexp: /\d+/
}, 'hello world')).toEqual('should match /\\d+/');
});

test('boolean', async () => {
const { boolean: checkBoolean } = DEF_CHECKERS;
expect(checkBoolean({}, 0)).toEqual('should be a boolean');
expect(checkBoolean({}, true)).toEqual(null);
expect(checkBoolean({}, false)).toEqual(null);
});

test('enum', async () => {
const { enum: checkEnum } = DEF_CHECKERS;
expect(checkEnum({
enum: [0, true, 'hello']
}, 0)).toEqual(null);
expect(checkEnum({
enum: [0, true, 'hello']
}, false)).toEqual('should be one of 0, true, hello');
});

test('custom', async () => {
const { custom: checkCustom } = DEF_CHECKERS;
expect(checkCustom({
times: 3,
custom: (rule:any, value:any) => {
const { times } = rule;
return value % times === 0 ? null : `not an integer multiple of ${times}`;
}
}, 0)).toEqual(null);
expect(checkCustom({
times: 3,
custom: (rule:any, value:any) => {
const { times } = rule;
return value % times === 0 ? null : `not an integer multiple of ${times}`;
}
}, 2)).toEqual('not an integer multiple of 3');
});

test('array', async () => {
const { array: checkArray } = DEF_CHECKERS;
expect(checkArray({
itemType: 'string',
itemRule: {
min: 8,
max: 20
}
}, ['hello', 'hello world'])).toEqual([{ code: 'invalid', field: '[0]', message: 'length should bigger than 8' }]);
expect(checkArray({
itemType: 'string',
itemRule: {
min: 2,
max: 20
}
}, ['hello', 'hello world'])).toEqual(null);

expect(checkArray({
itemChecker: (rule:any, value:any) => {
const { times } = rule;
return value % times === 0 ? null : `not an integer multiple of ${times}`;
},
itemRule: {
times: 3
}
}, [3, 6, 7])).toEqual([{ code: 'invalid', field: '[2]', message: 'not an integer multiple of 3' }]);

expect(checkArray({
itemChecker: (rule:any, value:any) => {
const { times } = rule;
return value % times === 0 ? null : `not an integer multiple of ${times}`;
},
itemRule: {
times: 3
}
}, [3, 6, 9])).toEqual(null);
});
});
Loading

0 comments on commit 46818a4

Please sign in to comment.