-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: consolidate state into a single source of truth
- Loading branch information
Showing
43 changed files
with
1,520 additions
and
997 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 29 additions & 11 deletions
40
packages/vest/src/core/produce/collectFailureMessages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import { SEVERITY_GROUP_WARN } from '../../test/lib/VestTest/constants'; | ||
|
||
/** | ||
* @param {string} suiteId Current suite id. | ||
* @param {String} suiteId Current suite id. | ||
* @param {'warn'|'error'} severity Filter by severity. | ||
* @param {string} [group] Group to collect messages from. | ||
* @param {Object} options | ||
* @param {String} [options.group] Group name for error lookup. | ||
* @param {String} [options.fieldName] Field name for error lookup. | ||
* @returns all messages for given criteria. | ||
*/ | ||
const collectFailureMessages = (state, severity, group) => { | ||
const collector = {}; | ||
const collectFailureMessages = (state, severity, { group, fieldName } = {}) => | ||
state.testObjects.reduce((collector, testObject) => { | ||
if (group && testObject.groupName !== group) { | ||
return collector; | ||
} | ||
|
||
if (fieldName && testObject.fieldName !== fieldName) { | ||
return collector; | ||
} | ||
|
||
const collectFrom = group ? state.groups[group] : state.tests; | ||
if (!testObject.failed) { | ||
return collector; | ||
} | ||
|
||
for (const fieldName in collectFrom) { | ||
if (collectFrom?.[fieldName] && collectFrom?.[fieldName][severity]) { | ||
collector[fieldName] = collectFrom[fieldName][severity]; | ||
if ( | ||
(severity !== SEVERITY_GROUP_WARN && testObject.isWarning) || | ||
(severity === SEVERITY_GROUP_WARN && !testObject.isWarning) | ||
) { | ||
return collector; | ||
} | ||
} | ||
|
||
return collector; | ||
}; | ||
collector[testObject.fieldName] = ( | ||
collector[testObject.fieldName] || [] | ||
).concat(testObject.statement); | ||
|
||
return collector; | ||
}, {}); | ||
|
||
export default collectFailureMessages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
SEVERITY_COUNT_WARN, | ||
SEVERITY_COUNT_ERROR, | ||
SEVERITY_GROUP_WARN, | ||
SEVERITY_GROUP_ERROR, | ||
TEST_COUNT, | ||
} from '../../test/lib/VestTest/constants'; | ||
|
||
const genTestObject = (stateKey, testObject) => { | ||
const { fieldName, isWarning, failed, statement } = testObject; | ||
|
||
stateKey[fieldName] = stateKey[fieldName] || { | ||
[SEVERITY_COUNT_WARN]: 0, | ||
[SEVERITY_COUNT_ERROR]: 0, | ||
[TEST_COUNT]: 0, | ||
}; | ||
|
||
const testKey = stateKey[fieldName]; | ||
|
||
stateKey[fieldName][TEST_COUNT]++; | ||
|
||
if (failed) { | ||
if (isWarning) { | ||
testKey[SEVERITY_COUNT_WARN]++; | ||
if (statement) { | ||
testKey[SEVERITY_GROUP_WARN] = ( | ||
testKey[SEVERITY_GROUP_WARN] || [] | ||
).concat(statement); | ||
} | ||
} else { | ||
testKey[SEVERITY_COUNT_ERROR]++; | ||
if (statement) { | ||
testKey[SEVERITY_GROUP_ERROR] = ( | ||
testKey[SEVERITY_GROUP_ERROR] || [] | ||
).concat(statement); | ||
} | ||
} | ||
} | ||
|
||
return testKey; | ||
}; | ||
|
||
/** | ||
* Counts the failed tests and adds global counters | ||
* @param {Object} state | ||
*/ | ||
export const countFailures = state => { | ||
state[SEVERITY_COUNT_ERROR] = 0; | ||
state[SEVERITY_COUNT_WARN] = 0; | ||
for (const test in state.tests) { | ||
state[SEVERITY_COUNT_ERROR] += state.tests[test][SEVERITY_COUNT_ERROR]; | ||
state[SEVERITY_COUNT_WARN] += state.tests[test][SEVERITY_COUNT_WARN]; | ||
} | ||
return state; | ||
}; | ||
|
||
/** | ||
* Reads the testObjects list and gets full validation result from it. | ||
* @param {Object} state | ||
*/ | ||
const genTestsSummary = state => { | ||
state.tests = {}; | ||
state.groups = {}; | ||
|
||
state.testObjects.forEach(testObject => { | ||
const { fieldName, groupName } = testObject; | ||
|
||
state.tests[fieldName] = genTestObject(state.tests, testObject); | ||
|
||
if (groupName) { | ||
state.groups[groupName] = state.groups[groupName] || {}; | ||
state.groups[groupName][fieldName] = genTestObject( | ||
state.groups[groupName], | ||
testObject | ||
); | ||
} | ||
}); | ||
|
||
return state; | ||
}; | ||
|
||
export default genTestsSummary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
import { SEVERITY_GROUP_WARN } from '../../test/lib/VestTest/constants'; | ||
|
||
/** | ||
* Determines whether a certain test profile has failures. | ||
* @param {string} suiteId | ||
* @param {'errorCount'|'warnCount'} severityKey lookup severity | ||
* @param {'warnings'|'errors'} severityKey lookup severity | ||
* @param {string} [fieldName] | ||
* @returns {Boolean} whether a suite or field have errors or warnings. | ||
* @returns {Boolean} | ||
*/ | ||
const has = (state, severityKey, fieldName) => { | ||
if (!fieldName) { | ||
return Boolean(state?.[severityKey]); | ||
export const hasLogic = (testObject, severityKey, fieldName) => { | ||
if (!testObject.failed) { | ||
return false; | ||
} | ||
|
||
if (fieldName && fieldName !== testObject.fieldName) { | ||
return false; | ||
} | ||
return Boolean(state?.tests?.[fieldName]?.[severityKey]); | ||
|
||
if ( | ||
(severityKey === SEVERITY_GROUP_WARN && !testObject.isWarning) || | ||
(severityKey !== SEVERITY_GROUP_WARN && testObject.isWarning) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* @param {string} suiteId | ||
* @param {'warnings'|'errors'} severityKey lookup severity | ||
* @param {string} [fieldName] | ||
* @returns {Boolean} whether a suite or field have errors or warnings. | ||
*/ | ||
const has = (state, severityKey, fieldName) => | ||
state.testObjects.some(testObject => | ||
hasLogic(testObject, severityKey, fieldName) | ||
); | ||
|
||
export default has; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.