Skip to content

Commit

Permalink
tests(vest): isValid
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Apr 1, 2022
1 parent 8a20841 commit b20ef64
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('isValid', () => {
it('Should return false when a required test has errors', () => {
expect(suite('field_1').isValid()).toBe(false);
});

it('Should return false when the queried field is not optional and has errors', () => {
expect(suite('field_2').isValid('field_2')).toBe(false);
});

it('Should return true when the queried field is optional and has errors', () => {
expect(suite('field_1').isValid('field_1')).toBe(true);
});
});

describe('When there are warnings in the suite', () => {
Expand All @@ -48,6 +56,7 @@ describe('isValid', () => {
});
it('Should return true when a required test has warnings', () => {
expect(suite().isValid()).toBe(true);
expect(suite().isValid('field_1')).toBe(true);
});

describe('When some of the tests for the required field are warnings', () => {
Expand Down Expand Up @@ -125,13 +134,15 @@ describe('isValid', () => {
it('Should return true', () => {
suite();
expect(suite.get().isValid()).toBe(true);
expect(suite.get().isValid('field_1')).toBe(true);
});
});
describe('When test is passing', () => {
it('Should return true', async () => {
suite();
await wait(300);
expect(suite.get().isValid()).toBe(true);
expect(suite.get().isValid('field_1')).toBe(true);
});
});
});
Expand All @@ -158,6 +169,13 @@ describe('isValid', () => {
await wait(300);
expect(suite.get().isValid()).toBe(true);
});

it('Should return false as long as the test is pending when querying a specific field', async () => {
suite();
expect(suite.get().isValid('field_1')).toBe(false);
await wait(300);
expect(suite.get().isValid('field_1')).toBe(true);
});
});

describe('When the suite has async non-optional tests', () => {
Expand All @@ -181,6 +199,7 @@ describe('isValid', () => {
const result = suite();

expect(result.isValid()).toBe(false);
expect(result.isValid('field_1')).toBe(false);
});
});

Expand All @@ -189,6 +208,8 @@ describe('isValid', () => {
return new Promise<void>(done => {
suite().done(result => {
expect(result.isValid()).toBe(true);
expect(result.isValid('field_1')).toBe(true);
expect(result.isValid('field_2')).toBe(true);
done();
});
});
Expand Down Expand Up @@ -226,6 +247,9 @@ describe('isValid', () => {
});
it('Should return true', () => {
expect(suite().isValid()).toBe(true);
expect(suite().isValid('field_1')).toBe(true);
expect(suite().isValid('field_2')).toBe(true);
expect(suite().isValid('field_3')).toBe(true);
});
});

Expand Down Expand Up @@ -319,4 +343,14 @@ describe('isValid', () => {
).toBe(true);
});
});

describe('When querying a non existing field', () => {
it('Should return false', () => {
expect(
create(() => {
test('field_1', () => true);
})().isValid('field_2')
).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { useSummary } from 'genTestsSummary';
export function isValid(fieldName?: string): boolean {
const summary = useSummary();

return Boolean(fieldName ? summary.tests?.[fieldName]?.valid : summary.valid);
return fieldName ? Boolean(summary.tests[fieldName]?.valid) : summary.valid;
}
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/__tests__/memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('test.memo', () => {
});

describe('async', () => {
it('Should immediately previous result on re-run', async () => {
it('Should immediately return previous result on re-run', async () => {
{
const suite = promisify(
vest.create(() => {
Expand Down

0 comments on commit b20ef64

Please sign in to comment.