Skip to content

Commit

Permalink
fix: edge case when calling done after delay (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 14, 2020
1 parent 1a86ee1 commit c2beec9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/vest/src/core/produce/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,30 @@ runSpec(vest => {
});
});
});

describe('When delayed', () => {
const validate = vest.create('delayed_done', () => {
testDummy(vest).failing('field_1', 'error_1:a');
testDummy(vest).failingAsync('field_2', 'error_1:b');
testDummy(vest).failingAsync('field_3', 'error_1:c', { time: 100 });
});

afterEach(() => {
resetState();
});

it('Should call done callback immediately when delayed', () => {
const res = validate();
return new Promise(done => {
setTimeout(() => {
const doneCb = jest.fn();
res.done(doneCb);
expect(doneCb).toHaveBeenCalled();
done();
}, 500);
});
});
});
});
});
describe('method: getErrorsByGroup', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/vest/src/core/test/lib/pending/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import removeElementFromArray from '../../../../lib/removeElementFromArray';
import getSuiteState from '../../../state/getSuiteState';
import patch from '../../../state/patch';
import { setCanceled } from '../canceled';
Expand Down Expand Up @@ -46,7 +47,7 @@ export const setPending = (suiteId, testObject) => {
export const removePending = testObject => {
patch(testObject.suiteId, state => ({
...state,
pending: state.pending.filter(tO => tO !== testObject),
lagging: state.lagging.filter(tO => tO !== testObject),
pending: removeElementFromArray(state.pending, testObject),
lagging: removeElementFromArray(state.lagging, testObject),
}));
};
17 changes: 17 additions & 0 deletions packages/vest/src/lib/removeElementFromArray/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Removes first found element from array
* WARNING: Mutates array
*
* @param {any[]} array
* @param {any} element
*/
const removeElementFromArray = (array, element) => {
const index = array.findIndex(item => item === element);
if (index !== -1) {
array.splice(index, 1);
}

return array;
};

export default removeElementFromArray;
32 changes: 32 additions & 0 deletions packages/vest/src/lib/removeElementFromArray/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import _ from 'lodash';
import removeElementFromArray from '.';

describe('Tests removeElementFromArray', () => {
describe('When found', () => {
it('Should return the same array', () => {
const arr = [1, 2, 3];

expect(removeElementFromArray(arr, 3)).toBe(arr);
});

it('Should remove found element', () => {
const foundElement = {};

const arr = [1, 2, foundElement];
expect(removeElementFromArray(arr, foundElement)).toEqual([1, 2]);
});
});

describe('When not found', () => {
it('Should keep array unchanged', () => {
const arr = [1, 2, {}, null];

expect(removeElementFromArray(_.cloneDeep(arr), 777)).toEqual(arr);
});

it('Should return the same array', () => {
const arr = [1, 2, {}, null];
expect(removeElementFromArray(arr, 777)).toBe(arr);
});
});
});

0 comments on commit c2beec9

Please sign in to comment.