Skip to content

Commit

Permalink
patch(vest): invalid accounts for warning tests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 4563b8d commit b5ae658
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
66 changes: 53 additions & 13 deletions packages/vest/src/produce/__tests__/isValid.test.ts
Expand Up @@ -49,6 +49,39 @@ describe('isValid', () => {
it('Should return true when a required test has warnings', () => {
expect(suite().isValid()).toBe(true);
});

describe('When some of the tests for the required field are warnings', () => {
beforeEach(() => {
suite = create(() => {
test('field_1', () => {
warn();
return false;
});
test('field_1', () => true);
});
});
it('Should return true when a required test has warnings', () => {
expect(suite().isValid()).toBe(true);
});
});

describe('when a warning test in a required field is skipped', () => {
beforeEach(() => {
suite = create(() => {
test('field_1', () => true);

skipWhen(true, () => {
test('field_1', () => {
warn();
return false;
});
});
});
});
it('Should return false even when the skipped field is warning', () => {
expect(suite().isValid()).toBe(false);
});
});
});

describe('When a non optional field is skipped', () => {
Expand Down Expand Up @@ -89,9 +122,9 @@ describe('isValid', () => {
});

describe('When test is pending', () => {
it('Should return false', () => {
it('Should return true', () => {
suite();
expect(suite.get().isValid()).toBe(false);
expect(suite.get().isValid()).toBe(true);
});
});
describe('When test is passing', () => {
Expand Down Expand Up @@ -119,8 +152,11 @@ describe('isValid', () => {
});
});

it('Should return true', () => {
expect(suite().isValid()).toBe(true);
it('Should return false before as long as the test is pending', async () => {
const res = suite();
expect(res.isValid()).toBe(false);
await wait(300);
expect(res.isValid()).toBe(true);
});
});

Expand All @@ -141,7 +177,7 @@ describe('isValid', () => {
});

describe('When test is pending', () => {
it('Should return `false`', () => {
it('Should return `false` for a required field', () => {
const result = suite();

expect(result.isValid()).toBe(false);
Expand Down Expand Up @@ -189,18 +225,22 @@ describe('isValid', () => {
});
});
it('Should return true', () => {
expect(suite('field_1').isValid()).toBe(true);
expect(suite().isValid()).toBe(true);
});
});

describe('When a required field has some passing tests', () => {
expect(
create(() => {
test('field_1', () => true);
skipWhen(true, () => {
it('Should return false', () => {
expect(
create(() => {
test('field_1', () => true);
});
})().isValid()
).toBe(false);
skipWhen(true, () => {
test('field_1', () => {
return true;
});
});
})().isValid()
).toBe(false);
});
});
});
32 changes: 11 additions & 21 deletions packages/vest/src/produce/isValid.ts
Expand Up @@ -24,35 +24,25 @@ export function isValid(result: TDraftResult): boolean {

if (
isNotEmpty(
pending.concat(lagging).filter(testObject => !testObject.isWarning)
pending
.concat(lagging)
.filter(testObject => !isOptionalField(testObject.fieldName))
)
) {
return false;
}

return noMissingRequiredTestRuns(result);
return hasMissingTests();
}

function noMissingRequiredTestRuns(result: TDraftResult): boolean {
const testObjectsPerField = countTestObjectsPerField();
function hasMissingTests(): boolean {
const [testObjects] = useTestObjects();

for (const test in result.tests) {
if (
!isOptionalField(test) &&
result.tests[test].testCount !== testObjectsPerField[test]
) {
return false;
return testObjects.every(testObject => {
if (isOptionalField(testObject.fieldName)) {
return true;
}
}

return true;
}

function countTestObjectsPerField(): Record<string, number> {
const [testObjects] = useTestObjects();

return testObjects.reduce((counters, testObject) => {
counters[testObject.fieldName] = (counters[testObject.fieldName] || 0) + 1;
return counters;
}, {});
return !testObject.skipped;
});
}

0 comments on commit b5ae658

Please sign in to comment.