Skip to content

Commit

Permalink
patch: remove canceled tests from canceled state (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 23, 2020
1 parent c4e827c commit b9ebf1e
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 985 deletions.
42 changes: 21 additions & 21 deletions packages/vest/dist/vest.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

996 changes: 100 additions & 896 deletions packages/vest/src/core/state/reset/__snapshots__/spec.js.snap

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/vest/src/core/state/reset/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setSuites } from '..';
import throwError from '../../../lib/throwError';
import { setCanceled } from '../../test/lib/canceled';
import getSuiteState from '../getSuiteState';
import setCanceled from '../setCanceled';

/**
* Cleans up a suite from state.
Expand All @@ -13,7 +13,6 @@ const reset = suiteId => {
}

const suite = getSuiteState(suiteId);

if (!suite) {
return;
}
Expand Down
11 changes: 5 additions & 6 deletions packages/vest/src/core/state/reset/spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getState, getSuites } from '..';
import resetState from '../../../../testUtils/resetState';
import runRegisterSuite from '../../../../testUtils/runRegisterSuite';
import runSpec from '../../../../testUtils/runSpec';
import vest from '../../../index';
Expand All @@ -7,9 +8,8 @@ import getSuiteState from '../getSuiteState';
import { SYMBOL_CANCELED } from '../symbols';
import reset from '.';

const genSuiteId = ((n = 0) => () => `suite_${n++}`)();
let state,
suiteId = genSuiteId();
const suiteId = 'suite_id';
let state;

const spec = _vest => {
let _reset;
Expand Down Expand Up @@ -41,10 +41,9 @@ const spec = _vest => {
});
};

beforeEach(() => {
suiteId = genSuiteId();
beforeAll(() => {
resetState();
});

describe('When invoked without suiteId', () => {
it('Should throw', () => {
expect(() => reset()).toThrow('`vest.reset` must be called with suiteId');
Expand Down
22 changes: 0 additions & 22 deletions packages/vest/src/core/state/setCanceled/index.js

This file was deleted.

27 changes: 0 additions & 27 deletions packages/vest/src/core/state/setCanceled/spec.js

This file was deleted.

31 changes: 31 additions & 0 deletions packages/vest/src/core/test/lib/canceled/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { setState } from '../../../state';
import { SYMBOL_CANCELED } from '../../../state/symbols';

/**
* Adds a VestTest to the canceled state.
* @param {VestTest[]} testObjects
*/
export const setCanceled = (...testObjects) => {
if (!testObjects || !testObjects.length) {
return;
}

setState(state => ({
...state,
[SYMBOL_CANCELED]: testObjects.reduce(
(ids, testObjects) => Object.assign(ids, { [testObjects.id]: true }),
state[SYMBOL_CANCELED]
),
}));
};

/**
* Removes a test from canceled state.
* @param {VestTest} testObject
*/
export const removeCanceled = testObject => {
setState(state => {
delete state[SYMBOL_CANCELED][testObject.id];
return state;
});
};
45 changes: 45 additions & 0 deletions packages/vest/src/core/test/lib/canceled/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import resetState from '../../../../../testUtils/resetState';
import { getState } from '../../../state';
import { SYMBOL_CANCELED } from '../../../state/symbols';
import VestTest from '../VestTest';
import { setCanceled, removeCanceled } from '.';

const genTests = () =>
Array.from(
{ length: 5 },
(_, i) => new VestTest('suite_id', `field_${i}`, 'msg', jest.fn())
);

let tests;

describe('module: canceled', () => {
beforeEach(() => {
resetState();
tests = genTests();
});

describe('setCanceled', () => {
it('Should add all passed ids to canceled object', () => {
const ids = tests.map(({ id }) => id);
expect(Object.keys(getState(SYMBOL_CANCELED))).not.toEqual(
expect.arrayContaining(ids)
);
setCanceled(...tests);
expect(Object.keys(getState(SYMBOL_CANCELED))).toEqual(
expect.arrayContaining(ids)
);
});
});

describe('removeCanceled', () => {
beforeEach(() => {
setCanceled(...tests);
});

it('Should remove canceled test', () => {
expect(getState(SYMBOL_CANCELED)).toHaveProperty(tests[0].id);
removeCanceled(tests[0]);
expect(getState(SYMBOL_CANCELED)).not.toHaveProperty(tests[0].id);
});
});
});
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/lib/pending/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getSuiteState from '../../../state/getSuiteState';
import patch from '../../../state/patch';
import setCanceled from '../../../state/setCanceled';
import { setCanceled } from '../canceled';

/**
* Sets a test as pending in the state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Object {
VestTest {
"failed": false,
"fieldName": "field_1",
"id": "9",
"id": "11",
"isWarning": false,
"statement": "some statement string",
"suiteId": "suiteId_1",
Expand Down
23 changes: 18 additions & 5 deletions packages/vest/src/core/test/runAsyncTest/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { OPERATION_MODE_STATELESS } from '../../../constants';
import singleton from '../../../lib/singleton';
import { getState } from '../../state';
import { getState, getSuites } from '../../state';
import cleanupCompletedSuite from '../../state/cleanupCompletedSuite';
import getSuiteState from '../../state/getSuiteState';
import hasRemainingTests from '../../state/hasRemainingTests';
import { SYMBOL_CANCELED } from '../../state/symbols';
import { removeCanceled } from '../lib/canceled';
import { removePending } from '../lib/pending';
import runTest from '../lib/runTest';

/**
* Runs done callback when async tests are finished running.
* @param {String} suiteId
Expand Down Expand Up @@ -36,9 +36,23 @@ const runAsyncTest = testObject => {
const { testFn, statement, fieldName, id, suiteId } = testObject;
const { operationMode } = singleton.useContext();
const done = cb => {
// If current test instance is canceled
const isCanceled = getState(SYMBOL_CANCELED)[id];

if (isCanceled) {
removeCanceled(testObject);
}

if (getState(SYMBOL_CANCELED)[id]) {
// This is for cases in which the suite state was already reset
if (!getSuites()[suiteId]) {
return;
}

removePending(testObject);

// We're returning here and not in the first `isCanceled` check
// because we need to remove pending regardless - as long as the\
// suite is present.
if (isCanceled) {
return;
}

Expand All @@ -50,7 +64,6 @@ const runAsyncTest = testObject => {
cb();
}

removePending(testObject);
runDoneCallbacks(suiteId, fieldName);

if (operationMode === OPERATION_MODE_STATELESS) {
Expand Down
34 changes: 31 additions & 3 deletions packages/vest/src/core/test/runAsyncTest/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import resetState from '../../../../testUtils/resetState';
import { OPERATION_MODE_STATEFUL } from '../../../constants';
import runWithContext from '../../../lib/runWithContext';
import Context from '../../Context';
import { setState } from '../../state';
import { setState, getState } from '../../state';
import getSuiteState from '../../state/getSuiteState';
import patch from '../../state/patch';
import registerSuite from '../../state/registerSuite';
Expand Down Expand Up @@ -84,11 +84,39 @@ describe.each([CASE_PASSING, CASE_FAILING])('runAsyncTest: %s', testCase => {
state = _.cloneDeep(getSuiteState(suiteId));
});

it('Should keep state unchanged', () =>
it('Should remove test from pending array', () => {
expect(getSuiteState(suiteId).pending).toEqual(
expect.arrayContaining([testObject])
);
runRunAsyncTest(testObject);
return new Promise(done => {
setTimeout(() => {
expect(getSuiteState(suiteId).pending).toEqual(
expect.not.arrayContaining([testObject])
);
done();
});
});
});

it('Should remove test from canceled state', () => {
expect(getState(SYMBOL_CANCELED)).toHaveProperty(testObject.id);
runRunAsyncTest(testObject);
return new Promise(done => {
setTimeout(() => {
expect(getState(SYMBOL_CANCELED)).not.toHaveProperty(testObject.id);
done();
});
});
});

it('Should keep rest of the state unchanged', () =>
new Promise(done => {
runRunAsyncTest(testObject);
setTimeout(() => {
expect(getSuiteState(suiteId)).toEqual(state);
expect(_.omit(getSuiteState(suiteId), 'pending')).toEqual(
_.omit(state, 'pending')
);
done();
});
}));
Expand Down

0 comments on commit b9ebf1e

Please sign in to comment.