Skip to content

Commit

Permalink
added: runWithContext interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Apr 24, 2020
1 parent 0ed9e3b commit 3c17e85
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 87 deletions.
41 changes: 21 additions & 20 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.

1 change: 1 addition & 0 deletions packages/vest/src/__snapshots__/spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Object {
"draft": [Function],
"enforce": [Function],
"only": [Function],
"runWithContext": [Function],
"skip": [Function],
"test": [Function],
"validate": [Function],
Expand Down
11 changes: 8 additions & 3 deletions packages/vest/src/core/Context/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { singleton } from "../../lib";
import singleton from "../../lib/singleton";

/**
* Creates a new context object, and assigns it as a static property on Vest's singleton.
* @param {Object} parent Parent context.
*/
function Context(parent) {
singleton.use().ctx = this;
Object.assign(this, parent);
if (singleton.use().ctx) {
Object.assign(singleton.use().ctx, this);
} else {
singleton.use().ctx = this;
}

Object.assign(singleton.use().ctx, parent);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/vest/src/core/Context/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe("Context", () => {
instance = new Context(parent);
});

afterEach(() => {
Context.clear();
});

it("Should assign all parent properties onto ctx instance", () => {
Object.keys(parent).forEach((key) => {
expect(instance[key]).toBe(parent[key]);
Expand All @@ -32,4 +36,31 @@ describe("Context", () => {
expect(singleton.useContext()).toBe(null);
});
});

describe("Nested context", () => {
let parent1, parent2;

beforeEach(() => {
parent1 = {
prop1: "parent_1_prop_1_value",
prop2: "parent_1_prop_2_value",
};

parent2 = {
prop1: "parent_2_prop_1_value",
prop3: "parent_2_prop_3_value",
};

new Context(parent1);
new Context(parent2);
});

it("Should assign new parent onto existing parent", () => {
expect(singleton.useContext()).toMatchObject({
prop1: "parent_2_prop_1_value",
prop2: "parent_1_prop_2_value",
prop3: "parent_2_prop_3_value",
});
});
});
});
39 changes: 21 additions & 18 deletions packages/vest/src/core/validate/__snapshots__/spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test validate suite wrapper Context creation Should create a new context object with initialized suite data 1`] = `
Object {
"result": Object {
"addToSkipped": [Function],
"markAsDone": [Function],
"markFailure": [Function],
"markTestRun": [Function],
"output": Object {
"errorCount": 0,
"name": "formName",
"skipped": Array [],
"testCount": 0,
"tested": Array [],
"tests": Object {},
"warnCount": 0,
exports[`Test validate suite wrapper Context creation Should call \`runWithContext\` with tests as the argument 1`] = `
Array [
Object {
"result": Object {
"addToSkipped": [Function],
"markAsDone": [Function],
"markFailure": [Function],
"markTestRun": [Function],
"output": Object {
"errorCount": 0,
"name": "formName",
"skipped": Array [],
"testCount": 0,
"tested": Array [],
"tests": Object {},
"warnCount": 0,
},
"pending": Array [],
"setPending": [Function],
},
"pending": Array [],
"setPending": [Function],
},
}
[Function],
]
`;
14 changes: 5 additions & 9 deletions packages/vest/src/core/validate/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { throwError } from "../../lib";
import Context from "../Context";
import { throwError, runWithContext } from "../../lib";
import suiteResult from "../suiteResult";
import { runAsync } from "../test";
import { SUITE_INIT_ERROR } from "./constants";
Expand Down Expand Up @@ -27,13 +26,10 @@ const validate = (name, tests) => {

const result = suiteResult(name);

new Context({ result });

tests();

Context.clear();

[...result.pending].forEach(runAsync);
runWithContext({ result }, () => {
tests();
[...result.pending].forEach(runAsync);
});

return result.output;
};
Expand Down
17 changes: 6 additions & 11 deletions packages/vest/src/core/validate/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ describe("Test validate suite wrapper", () => {
});

describe("Context creation", () => {
let mockContext, validate, name;
let mockRunWithContext, validate, name;

beforeEach(() => {
name = "formName";
mockContext = jest.fn();
mockContext.clear = jest.fn();
mockRunWithContext = jest.fn();
jest.resetModules();
jest.mock("../Context/", () => ({
jest.mock("../../lib/runWithContext", () => ({
__esModule: true,
default: mockContext,
default: mockRunWithContext,
}));
validate = require(".");
validate(name, noop);
Expand All @@ -67,12 +66,8 @@ describe("Test validate suite wrapper", () => {
jest.resetAllMocks();
});

it("Should create a new context object with initialized suite data", () => {
expect(mockContext.mock.calls[0][0]).toMatchSnapshot();
});

it("Should clear created context", () => {
expect(mockContext.clear).toHaveBeenCalled();
it("Should call `runWithContext` with tests as the argument", () => {
expect(mockRunWithContext.mock.calls[0]).toMatchSnapshot();
});
});
});
11 changes: 6 additions & 5 deletions packages/vest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { VERSION } from "./constants";
import test from "./core/test";
import validate from "./core/validate";
import { draft, only, skip, warn } from "./hooks";
import { singleton } from "./lib";
import { singleton, runWithContext } from "./lib";

export default singleton.register({
Enforce: enforce.Enforce,
VERSION,
enforce,
draft,
test,
any,
validate,
draft,
enforce,
only,
runWithContext,
skip,
test,
validate,
warn,
});
1 change: 1 addition & 0 deletions packages/vest/src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as globalObject } from "./globalObject";
export { default as runWithContext } from "./runWithContext";
export { default as singleton } from "./singleton";
export { default as throwError } from "./throwError";
16 changes: 16 additions & 0 deletions packages/vest/src/lib/runWithContext/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Context from "../../core/Context";

/**
* Initializes a Vest context and runs a callback function.
* @param {Object} parent Parent context.
* @param {Function} fn Function to run after creating the context.
* @returns {*} callback funcion output.
*/
const runWithContext = (parent, fn) => {
new Context(parent);
const res = fn();
Context.clear();
return res;
};

export default runWithContext;
55 changes: 55 additions & 0 deletions packages/vest/src/lib/runWithContext/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import faker from "faker";

describe("runWithContext", () => {
let parent, fn, mockContext, result, runWithContext, singleton;

beforeEach(() => {
singleton = require("../singleton");

result = faker.random.word();
parent = { [faker.random.word()]: faker.lorem.word() };
fn = jest.fn(() => result);
singleton = require("../singleton");
runWithContext = require(".");
});

afterEach(() => {
jest.resetAllMocks();
});

it("Should call callback function after creating the context", () => {
expect(singleton.useContext()).toBeFalsy();
const fn = jest.fn(() => {
expect(singleton.useContext()).toMatchObject(parent);
});

runWithContext(parent, fn);
expect(fn).toHaveBeenCalledTimes(1);
});

it("Should return with the callback function's return value", () => {
expect(runWithContext(parent, fn)).toBe(result);
});

describe("Calls to context", () => {
beforeEach(() => {
jest.resetModules();
mockContext = jest.fn();
mockContext.clear = jest.fn();
jest.mock("../../core/Context/", () => ({
__esModule: true,
default: mockContext,
}));
singleton = require("../singleton");
runWithContext = require(".");
runWithContext(parent, fn);
});
it("Should create a new context with the parent object", () => {
expect(mockContext).toHaveBeenCalledWith(parent);
});

it("Should clear the created context", () => {
expect(mockContext.clear).toHaveBeenCalledTimes(1);
});
});
});
40 changes: 20 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,9 @@ camelcase@^5.0.0, camelcase@^5.3.1:
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==

caniuse-lite@^1.0.30001038:
version "1.0.30001041"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001041.tgz#c2ea138dafc6fe03877921ddcddd4a02a14daf76"
integrity sha512-fqDtRCApddNrQuBxBS7kEiSGdBsgO4wiVw4G/IClfqzfhW45MbTumfN4cuUJGTM0YGFNn97DCXPJ683PS6zwvA==
version "1.0.30001042"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001042.tgz#c91ec21ec2d270bd76dbc2ce261260c292b8c93c"
integrity sha512-igMQ4dlqnf4tWv0xjaaE02op9AJ2oQzXKjWf4EuAHFN694Uo9/EfPVIPJcmn2WkU9RqozCxx5e2KPcVClHDbDw==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -2077,9 +2077,9 @@ ecc-jsbn@~0.1.1:
safer-buffer "^2.1.0"

electron-to-chromium@^1.3.390:
version "1.3.405"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.405.tgz#b84fcb157edb26eae6c36d93d416cb51caa399bd"
integrity sha512-D+xkP+hAQY/790DzImC8bI8QJLaArNG4b74bYvkhkK/fli51JmNyUYxwKLSl/8VPGkkXEqKCupSDD05/E5P72w==
version "1.3.410"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.410.tgz#00e0ec61c22933daa8b4de172c03932678783adc"
integrity sha512-DbCBdwtARI0l3e3m6ZIxVaTNahb6dSsmGjuag/twiVcWuM4MSpL5IfsJsJSyqLqxosE/m0CXlZaBmxegQW/dAg==

emoji-regex@^7.0.1:
version "7.0.3"
Expand Down Expand Up @@ -2281,11 +2281,11 @@ esprima@^4.0.0, esprima@^4.0.1:
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

esquery@^1.0.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.2.1.tgz#105239e215c5aa480369c7794d74b8b5914c19d4"
integrity sha512-/IcAXa9GWOX9BUIb/Tz2QrrAWFWzWGrFIeLeMRwtiuwg9qTFhSYemsi9DixwrFFqVbhBZ47vGcxEnu5mbPqbig==
version "1.3.1"
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.3.1.tgz#b78b5828aa8e214e29fb74c4d5b752e1c033da57"
integrity sha512-olpvt9QG0vniUBZspVRN6lwB7hOZoTRtT+jzR+tS4ffYx2mzbw+z0XCOk44aaLYKApNX5nMm+E+P6o25ip/DHQ==
dependencies:
estraverse "^5.0.0"
estraverse "^5.1.0"

esrecurse@^4.1.0:
version "4.2.1"
Expand All @@ -2299,10 +2299,10 @@ estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0:
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d"
integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==

estraverse@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.0.0.tgz#ac81750b482c11cca26e4b07e83ed8f75fbcdc22"
integrity sha512-j3acdrMzqrxmJTNj5dbr1YbjacrYgAxVMeF0gK16E3j494mOe7xygM/ZLIguEQ0ETwAg2hlJCtHRGav+y0Ny5A==
estraverse@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.1.0.tgz#374309d39fd935ae500e7b92e8a6b4c720e59642"
integrity sha512-FyohXK+R0vE+y1nHLoBM7ZTyqRpqAlhdZHCWIWEviFLiGB8b04H6bQs8G+XTthacvT8VuwvteiP7RJSxMs8UEw==

estree-walker@2.0.1:
version "2.0.1"
Expand Down Expand Up @@ -4816,9 +4816,9 @@ resolve@1.1.7:
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

resolve@^1.10.0, resolve@^1.11.0, resolve@^1.11.1, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.15.1, resolve@^1.3.2:
version "1.15.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8"
integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w==
version "1.16.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.16.0.tgz#063dc704fa3413e13ac1d0d1756a7cbfe95dd1a7"
integrity sha512-LarL/PIKJvc09k1jaeT4kQb/8/7P+qV4qSnN2K80AES+OHdfZELAKVOBjxsvtToT/uLOfFbvYvKfZmV8cee7nA==
dependencies:
path-parse "^1.0.6"

Expand Down Expand Up @@ -5871,9 +5871,9 @@ yargs-parser@13.1.2, yargs-parser@^13.1.2:
decamelize "^1.2.0"

yargs-parser@^18.1.1:
version "18.1.2"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.2.tgz#2f482bea2136dbde0861683abea7756d30b504f1"
integrity sha512-hlIPNR3IzC1YuL1c2UwwDKpXlNFBqD1Fswwh1khz5+d8Cq/8yc/Mn0i+rQXduu8hcrFKvO7Eryk+09NecTQAAQ==
version "18.1.3"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
integrity sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==
dependencies:
camelcase "^5.0.0"
decamelize "^1.2.0"
Expand Down

0 comments on commit 3c17e85

Please sign in to comment.