Skip to content

Commit

Permalink
patch(vest): add useAllIncomplete hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent b5ae658 commit 5a78179
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
6 changes: 6 additions & 0 deletions packages/vest/src/core/state/stateHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 5 additions & 5 deletions packages/vest/src/core/suite/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,13 +59,13 @@ export default function create<T extends (...args: any[]) => 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);
Expand Down
6 changes: 2 additions & 4 deletions packages/vest/src/core/suite/hasRemainingTests.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
16 changes: 4 additions & 12 deletions packages/vest/src/produce/isValid.ts
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -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;
Expand Down

0 comments on commit 5a78179

Please sign in to comment.