Skip to content

Commit

Permalink
patch(vest): move creation functions to event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent fe83e88 commit ddfa9fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
41 changes: 20 additions & 21 deletions packages/vest/src/core/suite/create.ts
Expand Up @@ -8,13 +8,9 @@ import { IsolateTypes } from 'IsolateTypes';
import createStateRef from 'createStateRef';
import context from 'ctx';
import { isolate } from 'isolate';
import matchingFieldName from 'matchingFieldName';
import omitOptionalTests from 'omitOptionalTests';
import { IVestResult, produceFullResult } from 'produce';
import { produceDraft, TDraftResult } from 'produceDraft';
import removeTestFromState from 'removeTestFromState';
import { useTestsFlat } from 'stateHooks';
import { initBus } from 'vestBus';
import { initBus, Events } from 'vestBus';

// eslint-disable-next-line max-lines-per-function
export default function create<T extends (...args: any[]) => void>(
Expand All @@ -30,9 +26,13 @@ export default function create<T extends (...args: any[]) => void>(
throwError('vest.create: Expected callback to be a function.');
}

// Event bus initialization
const bus = initBus();

// State initialization
const state = createState();

// State reference - this holds the actual state values
const stateRef = createStateRef(state, { suiteId: genId() });

interface IVestSuite {
Expand All @@ -43,35 +43,34 @@ export default function create<T extends (...args: any[]) => void>(
remove: (fieldName: string) => void;
}

// Create base context reference. All hooks will derive their data from this
const ctxRef = { stateRef, bus };

const suite: IVestSuite = assign(
context.bind({ stateRef, bus }, (...args: unknown[]) => {
// Bind the suite body to the context
context.bind(ctxRef, (...args: unknown[]) => {
// Reset the state. Migrates current test objects to `prev` array.
state.reset();

// Run the consumer's callback
// Create a top level isolate
isolate({ type: IsolateTypes.SUITE }, () => {
// Run the consumer's callback
suiteCallback(...args);
});

// Remove tests that are optional and should be omitted
omitOptionalTests();
// Report the suite is done registering tests
// Async tests may still be running
bus.emit(Events.SUITE_COMPLETED);

// Return the result
return produceFullResult();
}),
{
get: context.bind({ stateRef }, produceDraft),
remove: context.bind({ stateRef }, name => {
const testObjects = useTestsFlat();

// We're mutating the array in `cancel`, so we have to first copy it.
testObjects.forEach(testObject => {
if (matchingFieldName(testObject, name)) {
testObject.cancel();
removeTestFromState(testObject);
}
});
}),
get: context.bind(ctxRef, produceDraft),
reset: state.reset,
remove: context.bind(ctxRef, (fieldName: string) => {
bus.emit(Events.REMOVE_FIELD, fieldName);
}),
}
);

Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/lib/registerTest.ts
Expand Up @@ -9,7 +9,7 @@ import { useBus, Events } from 'vestBus';
* Registers test, if async - adds to pending array
*/
export default function registerTest(testObject: VestTest): void {
const { emit } = useBus();
const bus = useBus();

// Run test callback.
// If a promise is returned, set as async and
Expand All @@ -24,7 +24,7 @@ export default function registerTest(testObject: VestTest): void {
testObject.setPending();
runAsyncTest(testObject);
} else {
emit(Events.TEST_COMPLETED, testObject);
bus.emit(Events.TEST_COMPLETED, testObject);
}
} catch (e) {
throwError(
Expand Down
27 changes: 27 additions & 0 deletions packages/vest/src/core/vestBus.ts
Expand Up @@ -3,11 +3,17 @@ import throwError from 'throwError';

import VestTest from 'VestTest';
import ctx from 'ctx';
import matchingFieldName from 'matchingFieldName';
import omitOptionalTests from 'omitOptionalTests';
import removeTestFromState from 'removeTestFromState';
import { runFieldCallbacks, runDoneCallbacks } from 'runCallbacks';
import { useTestsFlat } from 'stateHooks';

export function initBus() {
const bus = createBus();

// Report a the completion of a test. There may be other tests with the same
// name that are still running, or not yet started.
bus.on(Events.TEST_COMPLETED, (testObject: VestTest) => {
if (testObject.isCanceled()) {
return;
Expand All @@ -19,6 +25,25 @@ export function initBus() {
runDoneCallbacks();
});

// Report that the suite completed its synchronous test run.
// Async operations may still be running.
bus.on(Events.SUITE_COMPLETED, () => {
// Remove tests that are optional and need to be omitted
omitOptionalTests();
});

// Removes a certain field from the state.
bus.on(Events.REMOVE_FIELD, (fieldName: string) => {
const testObjects = useTestsFlat();

testObjects.forEach(testObject => {
if (matchingFieldName(testObject, fieldName)) {
testObject.cancel();
removeTestFromState(testObject);
}
});
});

return bus;
}

Expand All @@ -34,4 +59,6 @@ export function useBus() {

export enum Events {
TEST_COMPLETED = 'test_completed',
REMOVE_FIELD = 'remove_field',
SUITE_COMPLETED = 'suite_completed',
}

0 comments on commit ddfa9fd

Please sign in to comment.