Skip to content

Commit

Permalink
[Minor] mark field as async
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 26, 2018
1 parent be39cc8 commit f12d3df
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 32 deletions.
34 changes: 28 additions & 6 deletions dist/passable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/passable.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/passable.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/passable.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion documentation/getting_started/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
| `validationErrors[field-name]` | `Object[]` | All error strings for this field |
| `validationWarnings` | `Object[]` | Actual errors per each field |
| `validationWarnings[field-name]` | `Object[]` | All warning strings for this field |
| `async` | `Object{}` | Contains async tests and their current completion status |
| `skipped` | `Array` | All skipped fields (empty, unless the `specific` option is used) |
| `getErrors` | `Function` | Getter function which allows accessing the errors array of one or all fields |
| `getWarnings` | `Function` | Getter function which allows accessing the warnings array of one or all fields |
| `getWarnings` | `Function` | Getter function which allows accessing the warnings array of one or all fields |

### `testsPerformed` field structure
| Name | Type | Description |
Expand Down
3 changes: 2 additions & 1 deletion src/Passable.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ class Passable {
[...this.pending].forEach((test) => {
if (test instanceof Promise) {

this.res.markAsync();
this.res.markAsync(test.fieldName);

const done: Function = () => {
this.res.markAsDone(test.fieldName);
this.clearPendingTest(test);
};

Expand Down
25 changes: 20 additions & 5 deletions src/result_object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResultObject {
* @return {Object} Current instance
*/
constructor(name: string) {
this.async = false;
this.async = null;
this.name = name;
this.hasValidationErrors = false;
this.hasValidationWarnings = false;
Expand Down Expand Up @@ -139,11 +139,26 @@ class ResultObject {
}

/**
* Marks current suite as async to be used by `done`
* Marks a field as async
* @param {string} fieldName the name of the field marked as async
* @return {Object} Current instance
*/
markAsync() {
this.async = true;
markAsync(fieldName: string) {
this.async = this.async || {};
this.async[fieldName] = { done: false };
return this;
}

/**
* Marks an async field as done
* @param {string} fieldName the name of the field marked as done
* @return {Object} Current instance
*/
markAsDone(fieldName: string) {
if (this.async && this.async[fieldName]) {
this.async[fieldName] = { done: true };
}

return this;
}

Expand Down Expand Up @@ -181,7 +196,7 @@ class ResultObject {
return [];
}

async: boolean;
async: AsyncObject;
name: string;
hasValidationErrors: boolean;
hasValidationWarnings: boolean;
Expand Down
10 changes: 5 additions & 5 deletions src/result_object/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('class: PassableResponse', () => {
it('Should return correct initial object from constructor', () => {
expect(new ResultObject('FormName')).to.deep.equal({
name: 'FormName',
async: false,
async: null,
hasValidationErrors: false,
hasValidationWarnings: false,
failCount: 0,
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('class: PassableResponse', () => {

expect(fail).to.deep.equal({
name: 'FormName',
async: false,
async: null,
testCount: 0,
failCount: 1,
warnCount: 0,
Expand All @@ -261,7 +261,7 @@ describe('class: PassableResponse', () => {
const warn = testObject.fail('f1', 'should warn', WARN);
expect(warn).to.deep.equal({
name: 'FormName',
async: false,
async: null,
testCount: 0,
failCount: 0,
warnCount: 1,
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('class: PassableResponse', () => {
testObject.bumpTestWarning('f1', 'should warn');

expect(testObject).to.deep.equal({
async: false,
async: null,
name: 'FormName',
testCount: 0,
failCount: 0,
Expand All @@ -325,7 +325,7 @@ describe('class: PassableResponse', () => {

expect(testObject).to.deep.equal({
name: 'FormName',
async: false,
async: null,
testCount: 0,
failCount: 1,
warnCount: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/spec/passable.api.severity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const warnPass = passable('WarnPass', (test) => {

const warnPassExpected = {
name: 'WarnPass',
async: false,
async: null,
skipped: [],
hasValidationErrors: false,
hasValidationWarnings: true,
Expand All @@ -49,7 +49,7 @@ const warnPassExpected = {
},
warnFailExpected = {
name: 'WarnFail',
async: false,
async: null,
skipped: [],
hasValidationErrors: true,
hasValidationWarnings: true,
Expand All @@ -66,7 +66,7 @@ const warnPassExpected = {
},
failExpected = {
name: 'Fail',
async: false,
async: null,
skipped: [],
hasValidationErrors: true,
hasValidationWarnings: false,
Expand Down
21 changes: 17 additions & 4 deletions src/spec/passable.test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,31 @@ describe("Tests Passable's `test` functionality", () => {

describe('async test behavior', () => {
describe('failing', () => {
let name;
beforeEach(() => {
name = lorem.word();
instance = new Passable('formname', (test) => {
test(lorem.word(), lorem.sentence(), new Promise((resolve, reject) => setImmediate(reject)));
test(name, lorem.sentence(), new Promise((resolve, reject) => setImmediate(reject)));
});
});

it('Should immediately register test', () => {
expect(instance.res.testCount).to.equal(1);
});

it('Should mark response object as `async`', () => {
expect(instance.res.async).to.equal(true);
it('Should set field as async (done: false) upon init', () => {
expect(instance.res.async).to.deep.equal({
[name]: { done: false }
});
});

it('Should set async field as done when completed', (done) => {
setTimeout(() => {
expect(instance.res.async).to.deep.equal({
[name]: { done: true }
});
done();
}, 100);
});

it('Should only marke test as failing after rejection', (done) => {
Expand Down Expand Up @@ -165,7 +178,7 @@ describe("Tests Passable's `test` functionality", () => {
test(name, lorem.sentence(), () => { throw new Error(); });
test(lorem.word(), lorem.sentence(), noop);
});
expect(instance.res.async).to.equal(false);
expect(instance.res.async).to.equal(null);
});

it('should mark a test as failed for `false`', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/spec/usecase/usecase_a.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default (passable) => {
});

const expect = {
async: false,
async: null,
name: 'case_a',
hasValidationErrors: true,
hasValidationWarnings: true,
Expand Down
2 changes: 1 addition & 1 deletion src/spec/usecase/usecase_b.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default (passable) => {
}, ['field_1', 'field_4']);

const expect = {
async: false,
async: null,
name: 'case_b',
hasValidationErrors: false,
hasValidationWarnings: true,
Expand Down
2 changes: 1 addition & 1 deletion src/spec/usecase/usecase_c.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default (passable) => {
});

const expect = {
async: false,
async: null,
name: 'case_c',
hasValidationErrors: true,
hasValidationWarnings: false,
Expand Down
2 changes: 1 addition & 1 deletion src/spec/usecase/usecase_d.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default (passable, enforce) => {
});

const expect = {
async: false,
async: null,
name: 'case_d',
hasValidationErrors: true,
hasValidationWarnings: false,
Expand Down
6 changes: 6 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ declare type ErrorAndWarningObject = {
[name: string]: Array<string>
}

declare type AsyncObject = {
[name: string]: {
done: boolean
}
} | null;

// Test
declare type TestsWrapper = (test: TestProvider) => void;
declare type TestProvider = (fieldName: string, statemenpt: string, test: PassableTest, severity: Severity) => void;
Expand Down

0 comments on commit f12d3df

Please sign in to comment.