Skip to content

Commit

Permalink
patch(vest): simplify useProduceSuiteSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 8, 2024
1 parent 38ef2f7 commit 0238d82
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 93 deletions.
12 changes: 6 additions & 6 deletions packages/vest/src/suiteResult/SuiteResultTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Maybe } from 'vest-utils';
import { Maybe, Nullable } from 'vest-utils';

import { Severity } from 'Severity';
import { SummaryFailure } from 'SummaryFailure';
Expand All @@ -14,13 +14,13 @@ export class SummaryBase {

export class SuiteSummary<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
> extends SummaryBase {
public [Severity.ERRORS]: SummaryFailure<F, G>[] = [];
public [Severity.WARNINGS]: SummaryFailure<F, G>[] = [];
public groups: Groups<G, F> = {} as Groups<G, F>;
public tests: Tests<F> = {} as Tests<F>;
public valid = false;
public valid: Nullable<boolean> = null;
}

export type TestsContainer<F extends TFieldName, G extends TGroupName> =
Expand All @@ -38,7 +38,7 @@ export type Tests<F extends TFieldName> = Record<F, SingleTestSummary>;
export type SingleTestSummary = SummaryBase & {
errors: string[];
warnings: string[];
valid: boolean;
valid: Nullable<boolean>;
pendingCount: number;
};

Expand All @@ -48,12 +48,12 @@ export type FailureMessages = Record<string, string[]>;

export type SuiteResult<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
> = SuiteSummary<F, G> & SuiteSelectors<F, G> & { suiteName: SuiteName };

export type SuiteRunResult<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
> = SuiteResult<F, G> & {
done: Done<F, G>;
};
Expand Down
38 changes: 19 additions & 19 deletions packages/vest/src/suiteResult/selectors/suiteSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { gatherFailures } from 'collectFailures';
import matchingFieldName from 'matchingFieldName';

export function bindSuiteSelectors<F extends TFieldName, G extends TGroupName>(
get: <F extends string, G extends string>() => SuiteResult<F, G>
get: <F extends string, G extends string>() => SuiteResult<F, G>,
): SuiteSelectors<F, G> {
return {
getError: (...args: Parameters<SuiteSelectors<F, G>['getError']>) =>
Expand Down Expand Up @@ -57,7 +57,7 @@ export function bindSuiteSelectors<F extends TFieldName, G extends TGroupName>(

// eslint-disable-next-line max-lines-per-function, max-statements
export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>
summary: SuiteSummary<F, G>,
): SuiteSelectors<F, G> {
const selectors = {
getError,
Expand All @@ -81,7 +81,7 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
// Booleans

function isValid(fieldName?: F): boolean {
return fieldName ? Boolean(summary.tests[fieldName]?.valid) : summary.valid;
return Boolean(fieldName ? summary.tests[fieldName]?.valid : summary.valid);
}

function isValidByGroup(groupName: G, fieldName?: F): boolean {
Expand Down Expand Up @@ -117,25 +117,25 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(

function hasWarningsByGroup<G extends TGroupName>(
groupName: G,
fieldName?: F
fieldName?: F,
): boolean {
return hasFailuresByGroup(
summary,
SeverityCount.WARN_COUNT,
groupName,
fieldName
fieldName,
);
}

function hasErrorsByGroup<G extends TGroupName>(
groupName: G,
fieldName?: F
fieldName?: F,
): boolean {
return hasFailuresByGroup(
summary,
SeverityCount.ERROR_COUNT,
groupName,
fieldName
fieldName,
);
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export function suiteSelectors<F extends TFieldName, G extends TGroupName>(
function getWarningsByGroup(groupName: G, fieldName: F): string[];
function getWarningsByGroup(
groupName: G,
fieldName?: F
fieldName?: F,
): GetFailuresResponse {
return getFailuresByGroup(summary, Severity.WARNINGS, groupName, fieldName);
}
Expand Down Expand Up @@ -220,17 +220,17 @@ export interface SuiteSelectors<F extends TFieldName, G extends TGroupName> {
// With a fieldName, it will only gather failures for that field
function getFailures<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>,
severityKey: Severity
severityKey: Severity,
): FailureMessages;
function getFailures<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>,
severityKey: Severity,
fieldName?: TFieldName
fieldName?: TFieldName,
): string[];
function getFailures<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>,
severityKey: Severity,
fieldName?: TFieldName
fieldName?: TFieldName,
): GetFailuresResponse {
return gatherFailures(summary.tests, severityKey, fieldName);
}
Expand All @@ -241,14 +241,14 @@ function getFailuresByGroup(
summary: SuiteSummary<TFieldName, TGroupName>,
severityKey: Severity,
groupName: TGroupName,
fieldName?: TFieldName
fieldName?: TFieldName,
): GetFailuresResponse {
return gatherFailures(summary.groups[groupName], severityKey, fieldName);
}
// Checks if a field is valid within a container object - can be within a group or top level
function isFieldValid(
testContainer: TestsContainer<TFieldName, TGroupName>,
fieldName: TFieldName
fieldName: TFieldName,
): boolean {
return !!testContainer[fieldName]?.valid;
}
Expand All @@ -259,7 +259,7 @@ function hasFailuresByGroup(
summary: SuiteSummary<TFieldName, TGroupName>,
severityCount: SeverityCount,
groupName: TGroupName,
fieldName?: TFieldName
fieldName?: TFieldName,
): boolean {
const group = summary.groups[groupName];

Expand All @@ -285,7 +285,7 @@ function hasFailuresByGroup(
function hasFailures(
summary: SuiteSummary<TFieldName, TGroupName>,
countKey: SeverityCount,
fieldName?: TFieldName
fieldName?: TFieldName,
): boolean {
const failureCount = fieldName
? summary.tests[fieldName]?.[countKey]
Expand All @@ -296,17 +296,17 @@ function hasFailures(

function getFailure<F extends TFieldName, G extends TGroupName>(
severity: Severity,
summary: SuiteSummary<F, G>
summary: SuiteSummary<F, G>,
): Maybe<SummaryFailure<F, G>>;
function getFailure<F extends TFieldName, G extends TGroupName>(
severity: Severity,
summary: SuiteSummary<F, G>,
fieldName: F
fieldName: F,
): Maybe<string>;
function getFailure<F extends TFieldName, G extends TGroupName>(
severity: Severity,
summary: SuiteSummary<F, G>,
fieldName?: F
fieldName?: F,
): Maybe<SummaryFailure<F, G> | string> {
const summaryKey = summary[severity];

Expand All @@ -316,6 +316,6 @@ function getFailure<F extends TFieldName, G extends TGroupName>(

return summaryKey.find(
(summaryFailure: SummaryFailure<TFieldName, TGroupName>) =>
matchingFieldName(summaryFailure, fieldName)
matchingFieldName(summaryFailure, fieldName),
)?.message;
}
113 changes: 45 additions & 68 deletions packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,64 @@ export function useProduceSuiteSummary<
F extends TFieldName,
G extends TGroupName,
>(): SuiteSummary<F, G> {
const summary: SuiteSummary<F, G> = new SuiteSummary();

TestWalker.walkTests<F, G>(testObject => {
summary.tests = useAppendToTest(summary.tests, testObject);
// @vx-allow use-use (TODO: fix this. the error is in the lint rule)
const summary = TestWalker.reduceTests<
SuiteSummary<F, G>,
TIsolateTest<F, G>
>((summary, testObject) => {
const fieldName = VestTest.getData<F>(testObject).fieldName;
summary.tests[fieldName] = useAppendToTest(summary.tests, testObject);
summary.groups = useAppendToGroup(summary.groups, testObject);
summary.errors = appendFailures(
Severity.ERRORS,
summary.errors,
testObject,
);
summary.warnings = appendFailures(
Severity.WARNINGS,
summary.warnings,
testObject,
);
});

summary.valid = useShouldAddValidProperty();
if (VestTest.isOmitted(testObject)) {
return summary;
}
if (summary.tests[fieldName].valid === false) {
summary.valid = false;
}
return addSummaryStats(testObject, summary);
}, new SuiteSummary());

return countOverallStates(summary);
summary.valid = summary.valid === false ? false : useShouldAddValidProperty();

return summary;
}

function appendFailures<F extends TFieldName, G extends TGroupName>(
key: Severity,
failures: SummaryFailure<F, G>[],
function addSummaryStats<F extends TFieldName, G extends TGroupName>(
testObject: TIsolateTest<F, G>,
): SummaryFailure<F, G>[] {
if (VestTest.isOmitted(testObject)) {
return failures;
summary: SuiteSummary<F, G>,
): SuiteSummary<F, G> {
if (VestTest.isWarning(testObject)) {
summary.warnCount++;
summary.warnings.push(SummaryFailure.fromTestObject(testObject));
} else if (VestTest.isFailing(testObject)) {
summary.errorCount++;
summary.errors.push(SummaryFailure.fromTestObject(testObject));
}

const shouldAppend =
key === Severity.WARNINGS
? VestTest.isWarning(testObject)
: VestTest.isFailing(testObject);
if (VestTest.isPending(testObject)) {
summary.pendingCount++;
}

if (shouldAppend) {
return failures.concat(SummaryFailure.fromTestObject(testObject));
if (!VestTest.isNonActionable(testObject)) {
summary.testCount++;
}
return failures;

return summary;
}

function useAppendToTest<F extends TFieldName>(
tests: Tests<F>,
testObject: TIsolateTest<F>,
): Tests<F> {
): SingleTestSummary {
const fieldName = VestTest.getData<F>(testObject).fieldName;

const newTests = {
...tests,
};

newTests[fieldName] = appendTestObject(newTests[fieldName], testObject);
const test = appendTestObject(tests[fieldName], testObject);
// If `valid` is false to begin with, keep it that way. Otherwise, assess.
newTests[fieldName].valid =
newTests[fieldName].valid === false
? false
: useShouldAddValidProperty(fieldName);
test.valid =
test.valid === false ? false : useShouldAddValidProperty(fieldName);

return newTests;
return test;
}

/**
Expand All @@ -98,37 +96,16 @@ function useAppendToGroup(
return groups;
}

const newGroups = {
...groups,
};

newGroups[groupName] = newGroups[groupName] || {};
newGroups[groupName][fieldName] = appendTestObject(
newGroups[groupName][fieldName],
testObject,
);
groups[groupName] = groups[groupName] || {};
const group = groups[groupName];
group[fieldName] = appendTestObject(group[fieldName], testObject);

newGroups[groupName][fieldName].valid =
newGroups[groupName][fieldName].valid === false
group[fieldName].valid =
group[fieldName].valid === false
? false
: useShouldAddValidPropertyInGroup(groupName, fieldName);

return newGroups;
}

/**
* Counts the failed tests and adds global counters
*/
function countOverallStates<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>,
): SuiteSummary<F, G> {
for (const test in summary.tests) {
summary.errorCount += summary.tests[test].errorCount;
summary.warnCount += summary.tests[test].warnCount;
summary.testCount += summary.tests[test].testCount;
summary.pendingCount += summary.tests[test].pendingCount;
}
return summary;
return groups;
}

/**
Expand Down

0 comments on commit 0238d82

Please sign in to comment.