Skip to content

Commit

Permalink
feat(vest): isValid with field name
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 5a78179 commit 2553748
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
68 changes: 68 additions & 0 deletions packages/vest/src/produce/__tests__/isValid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,72 @@ describe('isValid', () => {
).toBe(false);
});
});

describe('When fieldname is specified', () => {
it('Should return false when field did not run yet', () => {
expect(
create(() => {
skip('field_1');
test('field_1', () => true);
})().isValid()
).toBe(false);
});

it("Should return false when some of the field's tests ran", () => {
expect(
create(() => {
test('field_1', () => {
return true;
});
skipWhen(true, () => {
test('field_1', () => {
return true;
});
});
})().isValid()
).toBe(false);
});

it('Should return false when the field has errors', () => {
expect(
create(() => {
test('field_1', () => {
return false;
});
})().isValid()
).toBe(false);
});

it('Should return true when all the tests are passing', () => {
expect(
create(() => {
test('field_1', () => {
return true;
});
})().isValid()
).toBe(true);
});

it('Should return true when the field only has warnings', () => {
expect(
create(() => {
test('field_1', () => {
warn();
return false;
});
})().isValid()
).toBe(true);
});

it('Should return true if field is optional and did not run', () => {
expect(
create(() => {
optional('field_1');
skipWhen(true, () => {
test('field_1', () => false);
});
})().isValid()
).toBe(true);
});
});
});
22 changes: 15 additions & 7 deletions packages/vest/src/produce/isValid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isNotEmpty, isEmpty } from 'isEmpty';

import nonMatchingFieldName from 'nonMatchingFieldName';
import type { TDraftResult } from 'produceDraft';
import { useTestObjects, isOptionalField, useAllIncomplete } from 'stateHooks';

export function isValid(result: TDraftResult): boolean {
if (result.hasErrors()) {
export function isValid(result: TDraftResult, fieldName?: string): boolean {
if (result.hasErrors(fieldName)) {
return false;
}

Expand All @@ -16,21 +17,28 @@ export function isValid(result: TDraftResult): boolean {

if (
isNotEmpty(
useAllIncomplete().filter(
testObject => !isOptionalField(testObject.fieldName)
)
useAllIncomplete().filter(testObject => {
if (nonMatchingFieldName(testObject, fieldName)) {
return false;
}
return !isOptionalField(testObject.fieldName);
})
)
) {
return false;
}

return hasMissingTests();
return hasMissingTests(fieldName);
}

function hasMissingTests(): boolean {
function hasMissingTests(fieldName?: string): boolean {
const [testObjects] = useTestObjects();

return testObjects.every(testObject => {
if (nonMatchingFieldName(testObject, fieldName)) {
return true;
}

if (isOptionalField(testObject.fieldName)) {
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/vest/src/produce/produceDraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function produceDraft(): TDraftResult {
hasErrorsByGroup: ctx.bind(ctxRef, hasErrorsByGroup),
hasWarnings: ctx.bind(ctxRef, hasWarnings),
hasWarningsByGroup: ctx.bind(ctxRef, hasWarningsByGroup),
isValid: ctx.bind(ctxRef, () => isValid(produceDraft())),
isValid: ctx.bind(ctxRef, (fieldName?: string) =>
isValid(produceDraft(), fieldName)
),
});
})
);
Expand All @@ -41,7 +43,7 @@ export type TDraftResult = ReturnType<typeof genTestsSummary> & {
* Determined if there are no errors, and if no
* required fields are skipped.
*/
isValid: () => boolean;
isValid: (fieldName?: string) => boolean;
hasErrors: typeof hasErrors;
hasWarnings: typeof hasWarnings;
getErrors: typeof getErrors;
Expand Down

0 comments on commit 2553748

Please sign in to comment.