Skip to content

Commit

Permalink
use context.bind
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 17, 2020
1 parent b080130 commit 699b293
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 30 deletions.
6 changes: 0 additions & 6 deletions packages/vest/src/core/context/index.js
Expand Up @@ -15,10 +15,4 @@ const context = createContext((ctxRef, parentContext) =>
})
);

export function bindContext(ctxRef, fn, ...args) {
return function (...runTimeArgs) {
return context.run(ctxRef, () => fn(...args, ...runTimeArgs));
};
}

export default context;
24 changes: 12 additions & 12 deletions packages/vest/src/core/produce/index.js
@@ -1,5 +1,5 @@
import createCache from '../../lib/cache';
import context, { bindContext } from '../context';
import context from '../context';
import hasRemainingTests from '../suite/hasRemainingTests';
import {
SEVERITY_GROUP_ERROR,
Expand Down Expand Up @@ -34,7 +34,7 @@ const done = (...args) => {
return output;
}

const cb = bindContext({ stateRef }, () =>
const cb = context.bind({ stateRef }, () =>
callback(produce({ draft: true }))
);

Expand Down Expand Up @@ -73,32 +73,32 @@ const produce = ({ draft } = {}) => {
const [testObjects] = useTestObjects();
return cache(
[testObjects, draft],
bindContext({ stateRef }, () =>
context.bind({ stateRef }, () =>
Object.defineProperties(
countFailures(genTestsSummary()),
[
['hasErrors', bindContext({ stateRef }, has, SEVERITY_GROUP_ERROR)],
['hasWarnings', bindContext({ stateRef }, has, SEVERITY_GROUP_WARN)],
['getErrors', bindContext({ stateRef }, get, SEVERITY_GROUP_ERROR)],
['getWarnings', bindContext({ stateRef }, get, SEVERITY_GROUP_WARN)],
['hasErrors', context.bind({ stateRef }, has, SEVERITY_GROUP_ERROR)],
['hasWarnings', context.bind({ stateRef }, has, SEVERITY_GROUP_WARN)],
['getErrors', context.bind({ stateRef }, get, SEVERITY_GROUP_ERROR)],
['getWarnings', context.bind({ stateRef }, get, SEVERITY_GROUP_WARN)],
[
'hasErrorsByGroup',
bindContext({ stateRef }, hasByGroup, SEVERITY_GROUP_ERROR),
context.bind({ stateRef }, hasByGroup, SEVERITY_GROUP_ERROR),
],
[
'hasWarningsByGroup',
bindContext({ stateRef }, hasByGroup, SEVERITY_GROUP_WARN),
context.bind({ stateRef }, hasByGroup, SEVERITY_GROUP_WARN),
],
[
'getErrorsByGroup',
bindContext({ stateRef }, getByGroup, SEVERITY_GROUP_ERROR),
context.bind({ stateRef }, getByGroup, SEVERITY_GROUP_ERROR),
],
[
'getWarningsByGroup',
bindContext({ stateRef }, getByGroup, SEVERITY_GROUP_WARN),
context.bind({ stateRef }, getByGroup, SEVERITY_GROUP_WARN),
],
]
.concat(draft ? [] : [['done', bindContext({ stateRef }, done)]])
.concat(draft ? [] : [['done', context.bind({ stateRef }, done)]])
.reduce((properties, [name, value]) => {
properties[name] = {
configurable: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/produce/spec.js
Expand Up @@ -3,7 +3,7 @@ import vest from '../..';
import collector from '../../../../../shared/testUtils/collector';
import testDummy from '../../../testUtils/testDummy';
import group from '../../hooks/group';
import context, { bindContext } from '../context';
import context from '../context';
import hasRemainingTests from '../suite/hasRemainingTests';
import {
SEVERITY_COUNT_ERROR,
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('module: produce', () => {
beforeEach(() => {
collect = collector();
runCreateSuite('suite_name');
runProduce = bindContext({ stateRef }, produce);
runProduce = context.bind({ stateRef }, produce);
context.run({ stateRef }, () => {
const [testObjectsState] = useTestObjects();
testKeys = [
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/suite/create/index.js
@@ -1,6 +1,6 @@
import genId from '../../../lib/id';
import throwError from '../../../lib/throwError';
import { bindContext } from '../../context';
import context from '../../context';
import produce from '../../produce';
import useTestCallbacks from '../../produce/useTestCallbacks';
import state from '../../state';
Expand Down Expand Up @@ -31,7 +31,7 @@ const createSuite = (...args) => {
});

return Object.defineProperties(
bindContext({ stateRef }, (...args) => {
context.bind({ stateRef }, (...args) => {
stateRef.unshift();

tests.apply(null, args);
Expand All @@ -41,7 +41,7 @@ const createSuite = (...args) => {
}),
{
get: {
value: bindContext({ stateRef }, produce, { draft: true }),
value: context.bind({ stateRef }, produce, { draft: true }),
},
name: { value: 'validate' },
reset: { value: stateRef.reset },
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/test/runAsyncTest/index.js
@@ -1,4 +1,4 @@
import context, { bindContext } from '../../context';
import context from '../../context';
import useTestCallbacks from '../../produce/useTestCallbacks';
import hasRemainingTests from '../../suite/hasRemainingTests';
import { removePending } from '../lib/pending';
Expand All @@ -11,7 +11,7 @@ import useTestObjects from '../useTestObjects';
const runAsyncTest = testObject => {
const { asyncTest, statement } = testObject;
const { stateRef } = context.use();
const done = bindContext({ stateRef }, () => {
const done = context.bind({ stateRef }, () => {
removePending(testObject);

// This is for cases in which the suite state was already reset
Expand All @@ -22,7 +22,7 @@ const runAsyncTest = testObject => {
// Perform required done callback calls and cleanups after the test is finished
runDoneCallbacks(testObject.fieldName);
});
const fail = bindContext({ stateRef }, rejectionMessage => {
const fail = context.bind({ stateRef }, rejectionMessage => {
testObject.statement =
typeof rejectionMessage === 'string' ? rejectionMessage : statement;
testObject.fail();
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/runAsyncTest/spec.js
@@ -1,6 +1,6 @@
import _ from 'lodash';
import runCreateRef from '../../../../testUtils/runCreateRef';
import context, { bindContext } from '../../context';
import context from '../../context';
import useTestCallbacks from '../../produce/useTestCallbacks';
import VestTest from '../lib/VestTest';
import { setPending } from '../lib/pending';
Expand Down Expand Up @@ -68,7 +68,7 @@ describe.each([CASE_PASSING /*, CASE_FAILING*/])(
new Promise(done => {
runRunAsyncTest(testObject);
setTimeout(
bindContext({ stateRef }, () => {
context.bind({ stateRef }, () => {
const [pendingState] = usePending();
expect(pendingState.pending).not.toContain(testObject);
done();
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/hooks/group/index.js
@@ -1,5 +1,5 @@
import { throwError } from '../../../../n4s/src/lib';
import { bindContext } from '../../core/context';
import context from '../../core/context';

/**
* Runs a group callback.
Expand All @@ -20,7 +20,7 @@ const group = (groupName, tests) => {
}

// Running with the context applied
bindContext({ groupName }, tests)();
context.bind({ groupName }, tests)();
};

export default group;

0 comments on commit 699b293

Please sign in to comment.