From 5a78179dbd162456d2bc400cda1959d87bd27f9f Mon Sep 17 00:00:00 2001 From: ealush Date: Wed, 1 Sep 2021 22:34:18 -0600 Subject: [PATCH] patch(vest): add useAllIncomplete hook --- packages/vest/src/core/state/stateHooks.ts | 6 ++++++ packages/vest/src/core/suite/create.ts | 10 +++++----- .../vest/src/core/suite/hasRemainingTests.ts | 6 ++---- packages/vest/src/produce/isValid.ts | 16 ++++------------ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/vest/src/core/state/stateHooks.ts b/packages/vest/src/core/state/stateHooks.ts index 9f0c59dbb..502dbe664 100644 --- a/packages/vest/src/core/state/stateHooks.ts +++ b/packages/vest/src/core/state/stateHooks.ts @@ -88,3 +88,9 @@ export function isOptionalField(fieldName: string): boolean { return !!optionalFields[fieldName]; } + +export function useAllIncomplete(): VestTest[] { + const [pending] = usePending(); + const [lagging] = useLagging(); + return pending.concat(lagging); +} diff --git a/packages/vest/src/core/suite/create.ts b/packages/vest/src/core/suite/create.ts index 336e66fa5..e4dc6453e 100644 --- a/packages/vest/src/core/suite/create.ts +++ b/packages/vest/src/core/suite/create.ts @@ -10,10 +10,10 @@ import context from 'ctx'; import { IVestResult, produceFullResult } from 'produce'; import { produceDraft, TDraftResult } from 'produceDraft'; import { - usePending, - useLagging, + useAllIncomplete, useTestObjects, usePrevTestObjects, + useLagging, } from 'stateHooks'; // eslint-disable-next-line max-lines-per-function @@ -59,13 +59,13 @@ export default function create void>( const [prevTestObjects] = useTestObjects(); const [, setPrevTestObjects] = usePrevTestObjects(); - const [pending] = usePending(); - const [prevLagging, setLagging] = useLagging(); + const [, setLagging] = useLagging(); + const allIncomplete = useAllIncomplete(); state.reset(); setPrevTestObjects(() => prevTestObjects); // Move all the active pending tests to the lagging array - setLagging(pending.concat(prevLagging)); + setLagging(allIncomplete); // Run the consumer's callback suiteCallback(...args); diff --git a/packages/vest/src/core/suite/hasRemainingTests.ts b/packages/vest/src/core/suite/hasRemainingTests.ts index a25f7bf28..c89715f4f 100644 --- a/packages/vest/src/core/suite/hasRemainingTests.ts +++ b/packages/vest/src/core/suite/hasRemainingTests.ts @@ -1,14 +1,12 @@ import { isEmpty, isNotEmpty } from 'isEmpty'; -import { usePending, useLagging } from 'stateHooks'; +import { useAllIncomplete } from 'stateHooks'; /** * Checks if a given tests, or the suite as a whole still have remaining tests. */ function hasRemainingTests(fieldName?: string): boolean { - const [pending] = usePending(); - const [lagging] = useLagging(); - const allIncomplete = pending.concat(lagging); + const allIncomplete = useAllIncomplete(); if (isEmpty(allIncomplete)) { return false; } diff --git a/packages/vest/src/produce/isValid.ts b/packages/vest/src/produce/isValid.ts index 68da58ba4..c67f8da11 100644 --- a/packages/vest/src/produce/isValid.ts +++ b/packages/vest/src/produce/isValid.ts @@ -1,12 +1,7 @@ import { isNotEmpty, isEmpty } from 'isEmpty'; import type { TDraftResult } from 'produceDraft'; -import { - useTestObjects, - isOptionalField, - usePending, - useLagging, -} from 'stateHooks'; +import { useTestObjects, isOptionalField, useAllIncomplete } from 'stateHooks'; export function isValid(result: TDraftResult): boolean { if (result.hasErrors()) { @@ -19,14 +14,11 @@ export function isValid(result: TDraftResult): boolean { return false; } - const [pending] = usePending(); - const [lagging] = useLagging(); - if ( isNotEmpty( - pending - .concat(lagging) - .filter(testObject => !isOptionalField(testObject.fieldName)) + useAllIncomplete().filter( + testObject => !isOptionalField(testObject.fieldName) + ) ) ) { return false;