Skip to content

Commit

Permalink
test: remove runSpec module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 17, 2020
1 parent 9edb4a1 commit 1474825
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 438 deletions.
134 changes: 61 additions & 73 deletions packages/vest/src/core/test/spec.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,77 @@
import faker from 'faker';
import runSpec from '../../../testUtils/runSpec';
import vest, { create, test, enforce } from '../..';

runSpec(vest => {
let testObject;
let testObject;

const { create, test, enforce } = vest;
describe("Test Vest's `test` function", () => {
describe('test callbacks', () => {
describe('Warn hook', () => {
it('Should be marked as warning when the warn hook gets called', () => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
vest.warn();
}
);
})();
expect(testObject.isWarning).toBe(true);
});
describe("Test Vest's `test` function", () => {
describe('test callbacks', () => {
describe('Warn hook', () => {
it('Should be marked as warning when the warn hook gets called', () => {
create(faker.random.word(), () => {
testObject = test(faker.random.word(), faker.lorem.sentence(), () => {
vest.warn();
});
})();
expect(testObject.isWarning).toBe(true);
});
});

describe('Sync', () => {
it('Should be marked as failed after a thrown error', () => {
create(faker.random.word(), () => {
testObject = test(faker.random.word(), faker.lorem.sentence(), () => {
throw new Error();
});
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});

describe('Sync', () => {
it('Should be marked as failed after a thrown error', () => {
it('Should be marked as failed for an explicit false return', () => {
create(faker.random.word(), () => {
test(faker.random.word(), faker.lorem.sentence(), () => false);
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});
});

describe('async', () => {
it('Should be marked as failed when a returned promise rejects', () =>
new Promise(done => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
throw new Error();
}
() =>
new Promise((resolve, reject) => {
expect(testObject.failed).toBe(false);
setTimeout(reject, 300);
})
);
expect(testObject.failed).toBe(false);
setTimeout(() => {
expect(testObject.failed).toBe(true);
done();
}, 310);
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});

it('Should be marked as failed for an explicit false return', () => {
create(faker.random.word(), () => {
test(faker.random.word(), faker.lorem.sentence(), () => false);
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});
});

describe('async', () => {
it('Should be marked as failed when a returned promise rejects', () =>
new Promise(done => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() =>
new Promise((resolve, reject) => {
expect(testObject.failed).toBe(false);
setTimeout(reject, 300);
})
);
expect(testObject.failed).toBe(false);
setTimeout(() => {
expect(testObject.failed).toBe(true);
done();
}, 310);
})();
}));
});
}));
});
});

describe('error handling', () => {
describe('When enforce is returned (not thrown)', () => {
it('Should continue without failing', () =>
new Promise(done => {
create('form_with_enforce', () => {
try {
test('field_with_enforce', () =>
enforce('example').isNotEmpty());
done();
} catch {
/* */
}
})();
}));
});
describe('error handling', () => {
describe('When enforce is returned (not thrown)', () => {
it('Should continue without failing', () =>
new Promise(done => {
create('form_with_enforce', () => {
try {
test('field_with_enforce', () => enforce('example').isNotEmpty());
done();
} catch {
/* */
}
})();
}));
});
});
});
132 changes: 64 additions & 68 deletions packages/vest/src/hooks/draft/spec.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
import runSpec from '../../../testUtils/runSpec';
import vest, { test, create } from '../..';

runSpec(vest => {
const { test, create } = vest;

describe('Draft', () => {
it('Should be exposed as a function from vest', () => {
create(() => {
expect(typeof vest.draft).toBe('function');
})();
});
describe('Draft', () => {
it('Should be exposed as a function from vest', () => {
create(() => {
expect(typeof vest.draft).toBe('function');
})();
});

it('Should the same object when result is unchanged', () => {
create(() => {
const a = vest.draft();
const b = vest.draft();
expect(a).toBe(b);
})();
});
it('Should the same object when result is unchanged', () => {
create(() => {
const a = vest.draft();
const b = vest.draft();
expect(a).toBe(b);
})();
});

it('Should only contain has/get callbacks', () => {
create(() => {
expect(typeof vest.draft().hasErrors).toBe('function');
expect(typeof vest.draft().getErrors).toBe('function');
expect(typeof vest.draft().hasWarnings).toBe('function');
expect(typeof vest.draft().getWarnings).toBe('function');
expect(typeof vest.draft().done).not.toBe('function');
expect(typeof vest.draft().cancel).not.toBe('function');
})();
});
it('Should only contain has/get callbacks', () => {
create(() => {
expect(typeof vest.draft().hasErrors).toBe('function');
expect(typeof vest.draft().getErrors).toBe('function');
expect(typeof vest.draft().hasWarnings).toBe('function');
expect(typeof vest.draft().getWarnings).toBe('function');
expect(typeof vest.draft().done).not.toBe('function');
expect(typeof vest.draft().cancel).not.toBe('function');
})();
});

it('Should contain intermediate test result', () => {
// This test is so long because it tests `draft` throughout
// A suite's life cycle, both as an argument, and as an import
create(() => {
expect(vest.draft().errorCount).toBe(0);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(false);
expect(vest.draft().hasWarnings()).toBe(false);
expect(vest.draft().hasErrors('field1')).toBe(false);
test('field1', 'message', () => expect(1).toBe(2));
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasErrors('field1')).toBe(true);
expect(vest.draft().hasWarnings()).toBe(false);
test('field2', 'message', () => expect(2).toBe(2));
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(false);
expect(vest.draft().hasWarnings('field3')).toBe(false);
test('field3', 'message', () => {
vest.warn();
expect(2).toBe(1);
});
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(1);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(true);
expect(vest.draft().hasWarnings('field3')).toBe(true);
test('field4', 'message', () => {
vest.warn();
return Promise.resolve();
});
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(1);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(true);
expect(vest.draft().hasWarnings('field4')).toBe(false);
})();
});
it('Should contain intermediate test result', () => {
// This test is so long because it tests `draft` throughout
// A suite's life cycle, both as an argument, and as an import
create(() => {
expect(vest.draft().errorCount).toBe(0);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(false);
expect(vest.draft().hasWarnings()).toBe(false);
expect(vest.draft().hasErrors('field1')).toBe(false);
test('field1', 'message', () => expect(1).toBe(2));
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasErrors('field1')).toBe(true);
expect(vest.draft().hasWarnings()).toBe(false);
test('field2', 'message', () => expect(2).toBe(2));
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(false);
expect(vest.draft().hasWarnings('field3')).toBe(false);
test('field3', 'message', () => {
vest.warn();
expect(2).toBe(1);
});
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(1);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(true);
expect(vest.draft().hasWarnings('field3')).toBe(true);
test('field4', 'message', () => {
vest.warn();
return Promise.resolve();
});
expect(vest.draft().errorCount).toBe(1);
expect(vest.draft().warnCount).toBe(1);
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(true);
expect(vest.draft().hasWarnings('field4')).toBe(false);
})();
});
});

0 comments on commit 1474825

Please sign in to comment.