-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.js
114 lines (104 loc) · 2.99 KB
/
index.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
113
114
import copy from '../../lib/copy';
import hasRemainingTests from '../state/hasRemainingTests';
import patch from '../state/patch';
import {
SEVERITY_GROUP_ERROR,
SEVERITY_GROUP_WARN,
} from '../test/lib/VestTest/constants';
import genTestsSummary, { countFailures } from './genTestsSummary';
import get from './get';
import getByGroup from './getByGroup';
import has from './has';
import hasByGroup from './hasByGroup';
/**
* Registers done callbacks.
* @param {string} suiteId
* @param {string} [fieldName]
* @param {Function} doneCallback
* @register {Object} Vest output object.
*/
const done = (state, ...args) => {
const { length, [length - 1]: callback, [length - 2]: fieldName } = args;
const output = produce(state);
// If we do not have any tests for current field
const shouldSkipRegistration = fieldName && !output.tests[fieldName];
if (typeof callback !== 'function' || shouldSkipRegistration) {
return output;
}
const cb = () => callback(produce(state, { draft: true }));
const isFinishedTest = fieldName && !hasRemainingTests(state, fieldName);
const isSuiteFinished = !hasRemainingTests(state);
const shouldRunCallback = isFinishedTest || isSuiteFinished;
if (shouldRunCallback) {
cb();
return output;
}
patch(state.suiteId, state => {
if (fieldName) {
state.fieldCallbacks[fieldName] = [].concat(
...(state.fieldCallbacks[fieldName] || []),
cb
);
} else {
state.doneCallbacks.push(cb);
}
return state;
});
return output;
};
/**
* @returns {Object} with only public properties.
*/
const extract = ({ groups, tests, name }) => ({
groups,
tests,
name,
});
/**
* @param {string} suiteId
* @param {Object} Options
* @returns Vest output object.
*/
const produce = (state, { draft } = {}) =>
state
|> copy
|> genTestsSummary
|> extract
|> countFailures
|> (transformedState =>
Object.defineProperties(
transformedState,
[
['hasErrors', has.bind(null, state, SEVERITY_GROUP_ERROR)],
['hasWarnings', has.bind(null, state, SEVERITY_GROUP_WARN)],
['getErrors', get.bind(null, state, SEVERITY_GROUP_ERROR)],
['getWarnings', get.bind(null, state, SEVERITY_GROUP_WARN)],
[
'hasErrorsByGroup',
hasByGroup.bind(null, state, SEVERITY_GROUP_ERROR),
],
[
'hasWarningsByGroup',
hasByGroup.bind(null, state, SEVERITY_GROUP_WARN),
],
[
'getErrorsByGroup',
getByGroup.bind(null, state, SEVERITY_GROUP_ERROR),
],
[
'getWarningsByGroup',
getByGroup.bind(null, state, SEVERITY_GROUP_WARN),
],
]
.concat(draft ? [] : [['done', done.bind(null, state)]])
.reduce((properties, [name, value]) => {
properties[name] = {
configurable: true,
enumerable: true,
value,
writeable: true,
};
return properties;
}, {})
));
export default produce;