-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
test.js
112 lines (97 loc) · 2.79 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import VestTest from 'VestTest';
import addTestToState from 'addTestToState';
import context from 'ctx';
import { isExcluded } from 'exclusive';
import isFunction from 'isFunction';
import isPromise from 'isPromise';
import isStringValue from 'isStringValue';
import { isUndefined } from 'isUndefined';
import { setPending } from 'pending';
import runAsyncTest from 'runAsyncTest';
import bindTestEach from 'test.each';
import bindTestMemo from 'test.memo';
import throwError from 'throwError';
import withArgs from 'withArgs';
/**
* Runs sync tests - or extracts promise.
* @param {VestTest} testObject VestTest instance.
* @returns {*} Result from test callback.
*/
const sync = testObject =>
context.run({ currentTest: testObject }, () => {
let result;
try {
result = testObject.testFn();
} catch (e) {
if (isUndefined(testObject.statement) && isStringValue(e)) {
testObject.statement = e;
}
result = false;
}
if (result === false) {
testObject.fail();
}
return result;
});
/**
* Registers test, if async - adds to pending array
* @param {VestTest} testObject A VestTest Instance.
*/
const register = testObject => {
addTestToState(testObject);
if (isExcluded(testObject)) {
testObject.markSkipped();
return;
}
// Run test callback.
// If a promise is returned, set as async and
// Move to pending list.
const result = sync(testObject);
try {
// try catch for safe property access
// in case object is an enforce chain
if (isPromise(result)) {
testObject.asyncTest = result;
setPending(testObject);
runAsyncTest(testObject);
}
} catch {
if (__DEV__) {
throwError(
`Your test function ${testObject.fieldName} returned ${JSON.stringify(
result
)}. Only "false" or a Promise are supported. Return values may cause unexpected behavior.`
);
}
}
};
/**
* Test function used by consumer to provide their own validations.
* @param {String} fieldName Name of the field to test.
* @param {String} [statement] The message returned in case of a failure.
* @param {function} testFn The actual test callback.
* @return {VestTest} A VestTest instance.
*
* **IMPORTANT**
* Changes to this function need to reflect in test.memo as well
*/
function test(fieldName, args) {
const [testFn, statement] = args.reverse();
const { groupName } = context.use();
const testObject = new VestTest({
fieldName,
group: groupName,
statement,
testFn,
});
if (!isFunction(testFn)) {
return;
}
register(testObject);
return testObject;
}
const exportedTest = withArgs(test);
bindTestEach(exportedTest);
bindTestMemo(exportedTest);
/* eslint-disable jest/no-export */
export default exportedTest;