-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.TypeTests.js
86 lines (73 loc) · 2.98 KB
/
class.TypeTests.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
const randomString = require('randomstring');
const TestParams = require('./class.TestParams.js');
const Messages = require('./class.Messages.js');
const { concatObjects, VALID_OBJECT_ID } = require('./utils');
const { executeFieldMultiTest, executeMultiTest, executeTest } = require('./common');
class TypeTests extends TestParams {
constructor(params) {
super(params);
this.messages = new Messages({
gte: (comparision) => `Must be greater than or equal to ${comparision}`,
gt: (comparision) => `Must be greater than ${comparision}`,
safeInteger: () => 'Is not a safe integer',
objectId: () => 'Id has invalid format',
boolean: () => 'Invalid boolean',
});
}
safeInteger(field, { status = 400, expect = this.messages.get('safeInteger', field), send = {}} = {}) {
describe(`safeInteger test`, () => {
it('valid', async () => executeFieldMultiTest(...this._paramsArray, field, {
status: 200,
sendValues: [3, 3.0],
defaultSend: send,
}));
it('invalid', async () => executeFieldMultiTest(...this._paramsArray, field, {
status: 400,
sendValues: ['3.5', 3.1],
defaultSend: send,
expect,
}));
});
}
gt(field, comparision, { status = 400, expect = this.messages.get('gt', field, comparision), send = {} } = {}) {
it(`greater than ${comparision}`, () => executeMultiTest(...this._paramsArray, [
{ status: 200, send: concatObjects(send, { [field]: comparision + 1}) },
{ status: 400, expect, send: concatObjects(send, { [field]: comparision }) },
{ status: 400, expect, send: concatObjects(send, { [field]: comparision - 1 }) },
]))
}
gte(field, comparision, { status = 400, expect = this.messages.get('gte', field, comparision), send = {} } = {}) {
it(`greater than or equal to ${comparision}`, () => executeMultiTest(...this._paramsArray, [
{ status: 200, send: concatObjects(send, { [field]: comparision }) },
{ status: 200, send: concatObjects(send, { [field]: comparision + 1 }) },
{ status, expect, send: concatObjects(send, { [field]: comparision - 1 }) },
],
));
}
boolean(field, { status = 400, send = {}, expect = this.messages.get('boolean', field) } = {}) {
describe('boolean test', () => {
it('valid', () => executeFieldMultiTest(...this._paramsArray, field, {
status: 200,
sendValues: [true, 'true', false, 'false'],
}));
it('invalid', () => executeFieldMultiTest(...this._paramsArray, field, {
status, expect,
sendValues: [randomString.generate(3), randomString.generate(6)],
}));
});
}
objectId(field, { status = 400, send = {}, onlyInvalid = false, expect = this.messages.get('objectId', field) } = {}) {
describe('objectId test', () => {
it('invalid', () => executeTest(...this._paramsArray, {
status: 400, expect,
send: { ...send, [field]: randomString.generate(20) },
}));
if (onlyInvalid) return;
it('valid', () => executeTest(...this._paramsArray, {
status: 200,
send: { ...send, [field]: VALID_OBJECT_ID },
}));
});
}
}
module.exports = TypeTests;