Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Commit

Permalink
Revise import notation (#98)
Browse files Browse the repository at this point in the history
* Use CorePackage abstract class

* Rename middleware.test to transformers.test

* Use npm script for publishing in CI, export DSL manually

* Exports as default

* Updates tests to use top level exports when possible

* Update state test to use new abstract class
  • Loading branch information
idantene authored Jul 23, 2019
1 parent 3c43358 commit cdd46ee
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
- run:
name: Authenticate with npm
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
- run: npx lerna publish from-package --yes
- add_ssh_keys:
fingerprints:
- "34:32:ff:02:fe:1e:00:10:49:75:8e:d4:6c:2b:12:ca"
Expand All @@ -45,6 +44,7 @@ jobs:
TAG=v`cat packages/unmock-core/package.json | grep version | awk 'BEGIN { FS = "\"" } { print $4 }'`
git tag -a $TAG
git push origin $TAG
- run: npm run publish --yes

workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compile": "tsc --build tsconfig.build.json",
"compile:clean": "tsc --build tsconfig.build.json --clean",
"watch": "tsc --build tsconfig.build.json --watch",
"release": "npm run clean && npm install && lerna publish --exact --force-publish=unmock-core --include-merged-tags",
"release": "npm run clean && npm install && lerna publish from-package --exact --force-publish=unmock-core --include-merged-tags",
"postinstall": "lerna run prepare && npm run lint-ts && npm run compile",
"format-check": "prettier-check '**/*.{js,ts}'",
"format": "prettier '**/*.{js,ts}' --write",
Expand Down
1 change: 1 addition & 0 deletions packages/unmock-core/src/__tests__/dsl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe("Translates top level DSL to OAS", () => {
it("Throws on invalid $times (wrong type) with STRICT_MODE", () => {
DSL.STRICT_MODE = true;
const translated = () =>
// @ts-ignore // uses incorrect type on purpose
DSL.translateTopLevelToOAS({ $times: "a" }, responsesWithoutProperties);
expect(translated).toThrow(
"Can't set response $times with non-numeric value!",
Expand Down
5 changes: 1 addition & 4 deletions packages/unmock-core/src/__tests__/generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from "fs";
import path from "path";
import { IServiceDefLoader } from "..";
import { responseCreatorFactory } from "../generator";

// const slackYamlString: string = fs.readFileSync(slackAbsPath, "utf-8");
import { IServiceDefLoader, responseCreatorFactory } from "..";

const serviceDefLoader: IServiceDefLoader = {
load: () => Promise.all(serviceDefLoader.loadSync()),
Expand Down
2 changes: 1 addition & 1 deletion packages/unmock-core/src/__tests__/matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import jsYaml from "js-yaml";
import path from "path";
import { ISerializedRequest } from "../interfaces";
import { ISerializedRequest } from "..";
import { OASMatcher } from "../service/matcher";

const petStoreYamlString: string = fs.readFileSync(
Expand Down
2 changes: 1 addition & 1 deletion packages/unmock-core/src/__tests__/options.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnmockOptions } from "../options";
import { UnmockOptions } from "..";

describe("tests whitelist", () => {
test("test default whitelist", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/unmock-core/src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import { IServiceDef } from "../interfaces";
import { IServiceDef } from "..";
import { ServiceParser } from "../service/parser";

const absPath = path.join(__dirname, "__unmock__", "petstore", "spec.yaml");
Expand Down
36 changes: 24 additions & 12 deletions packages/unmock-core/src/__tests__/state.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { dsl } from "..";
import {
ExtendedHTTPMethod,
HTTPMethod,
IStateInputGenerator,
} from "../service/interfaces";
import { State } from "../service/state/state";
import defMiddleware, { textResponse } from "../service/state/transformers";

const fullSchema = {
openapi: "",
Expand Down Expand Up @@ -98,27 +98,33 @@ describe("Test state management", () => {
it("returns undefined when setting empty state", () => {
const state = new State();
expect(
updateState(state, "any", "**", defMiddleware(), "**"),
updateState(state, "any", "**", dsl.objResponse(), "**"),
).toBeUndefined();
});

it("throws when setting state for non-existing method with generic endpoint", () => {
const state = new State();
expect(() =>
updateState(state, "post", "**", defMiddleware(), "**"),
updateState(state, "post", "**", dsl.objResponse(), "**"),
).toThrow("Can't find any endpoints with method 'post'");
});

it("throws when setting state for non-existing method with specific, existing endpoint", () => {
const state = new State();
expect(() =>
updateState(state, "post", "/test/5", defMiddleware(), "/test/{test_id}"),
updateState(
state,
"post",
"/test/5",
dsl.objResponse(),
"/test/{test_id}",
),
).toThrow("Can't find response for 'post /test/5'");
});

it("saves state from any endpoint and get method as expected", () => {
const state = new State();
updateState(state, "get", "**", defMiddleware({ foo: { id: 5 } }), "**");
updateState(state, "get", "**", dsl.objResponse({ foo: { id: 5 } }), "**");
const stateResult = getState(state, "get", "/");
expect(stateResult).toEqual({
200: {
Expand All @@ -142,7 +148,13 @@ describe("Test state management", () => {

it("parses $times on specific endpoint as expected", () => {
const state = new State();
updateState(state, "get", "**", defMiddleware({ id: 5, $times: 2 }), "**");
updateState(
state,
"get",
"**",
dsl.objResponse({ id: 5, $times: 2 }),
"**",
);
const getRes = () => getState(state, "get", "/");
expect(getRes()).toEqual({
200: {
Expand Down Expand Up @@ -194,7 +206,7 @@ describe("Test state management", () => {

it("saves state from any endpoint and any method as expected", () => {
const state = new State();
updateState(state, "any", "**", defMiddleware({ id: 5 }), "**");
updateState(state, "any", "**", dsl.objResponse({ id: 5 }), "**");
const stateResult = getState(state, "get", "/");
expect(stateResult).toEqual({
200: {
Expand All @@ -214,19 +226,19 @@ describe("Test state management", () => {

it("spreads states from multiple paths correctly", () => {
const state = new State();
updateState(state, "any", "**", defMiddleware({ id: 5 }), "**");
updateState(state, "any", "**", dsl.objResponse({ id: 5 }), "**");
updateState(
state,
"any",
"/test/5",
defMiddleware({ id: 3 }),
dsl.objResponse({ id: 3 }),
"/test/{test_id}",
);
updateState(
state,
"any",
"/test/*",
defMiddleware({ id: -1 }),
dsl.objResponse({ id: -1 }),
"/test/{test_id}",
);
const stateResult = getState(state, "get", "/test/5");
Expand All @@ -248,7 +260,7 @@ describe("Test state management", () => {

it("saves nested state correctly", () => {
const state = new State();
updateState(state, "any", "**", defMiddleware({ foo: { id: 5 } }), "**");
updateState(state, "any", "**", dsl.objResponse({ foo: { id: 5 } }), "**");
const stateResult = getState(state, "get", "/");
expect(stateResult).toEqual({
200: {
Expand All @@ -272,7 +284,7 @@ describe("Test state management", () => {

it("Handles textual state correctly", () => {
const state = new State();
updateState(state, "any", "**", textResponse("foobar"), "**");
updateState(state, "any", "**", dsl.textResponse("foobar"), "**");
const stateResult = getState(state, "get", "/");
expect(stateResult).toEqual({
200: { "text/plain": { const: "foobar", type: "string" } },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformers } from "../index";
import { dsl } from "..";
import { Schema } from "../service/interfaces";

const schema: Schema = {
Expand Down Expand Up @@ -34,46 +34,46 @@ const schema: Schema = {

describe("Test text provider", () => {
it("returns empty object for undefined state", () => {
const p = transformers.textResponse();
const p = dsl.textResponse();
expect(p.isEmpty).toBeTruthy();
expect(p.top).toEqual({});
// @ts-ignore // deliberately checking with empty input
expect(p.gen()).toEqual({});
});

it("returns empty object for empty state", () => {
const p = transformers.textResponse("");
const p = dsl.textResponse("");
expect(p.isEmpty).toBeTruthy();
expect(p.top).toEqual({});
// @ts-ignore // deliberately checking with empty input
expect(p.gen()).toEqual({});
});

it("returns empty object for empty schema", () => {
const p = transformers.textResponse("foo");
const p = dsl.textResponse("foo");
expect(p.isEmpty).toBeFalsy();
expect(p.top).toEqual({});
// @ts-ignore // deliberately checking with empty input
expect(p.gen()).toEqual({});
});

it("returns empty object for non-text schema", () => {
const p = transformers.textResponse("foo");
const p = dsl.textResponse("foo");
expect(p.isEmpty).toBeFalsy();
expect(p.top).toEqual({});
expect(p.gen({ type: "array", items: {} })).toEqual({});
});

it("returns correct state object for valid input", () => {
const p = transformers.textResponse("foo");
const p = dsl.textResponse("foo");
expect(p.isEmpty).toBeFalsy();
expect(p.top).toEqual({});
expect(p.gen({ type: "string" })).toEqual({ type: "string", const: "foo" });
});

it("top level DSL doesn't change response", () => {
// @ts-ignore // invalid value on purpose
const p = transformers.textResponse("foo", { $code: 200, notDSL: "a" });
const p = dsl.textResponse("foo", { $code: 200, notDSL: "a" });
expect(p.isEmpty).toBeFalsy();
expect(p.top).toEqual({ $code: 200 }); // non DSL is filtered out
expect(p.gen({ type: "string" })).toEqual({ type: "string", const: "foo" });
Expand All @@ -82,21 +82,21 @@ describe("Test text provider", () => {

describe("Test default provider", () => {
it("returns empty objects for undefined state", () => {
const p = transformers.default();
const p = dsl.objResponse();
expect(p.isEmpty).toBeTruthy();
expect(p.top).toEqual({});
expect(p.gen({})).toEqual({});
});

it("returns empty objects for empty state", () => {
const p = transformers.default({});
const p = dsl.objResponse({});
expect(p.isEmpty).toBeTruthy();
expect(p.top).toEqual({});
expect(p.gen({})).toEqual({});
});

it("filters out top level DSL from state", () => {
const p = transformers.default({ $code: 200, foo: "bar" });
const p = dsl.objResponse({ $code: 200, foo: "bar" });
expect(p.isEmpty).toBeFalsy();
expect(p.top).toEqual({ $code: 200 });
expect(p.gen({})).toEqual({}); // no schema to expand
Expand All @@ -111,13 +111,13 @@ describe("Test default provider", () => {
});

it("with empty path", () => {
const spreadState = transformers.default({}).gen(schema);
const spreadState = dsl.objResponse({}).gen(schema);
expect(spreadState).toEqual({}); // Empty state => empty spread state
});

it("with specific path", () => {
const spreadState = transformers
.default({
const spreadState = dsl
.objResponse({
test: { id: "a" },
})
.gen(schema);
Expand All @@ -134,13 +134,13 @@ describe("Test default provider", () => {
});

it("with vague path", () => {
const spreadState = transformers.default({ id: 5 }).gen(schema);
const spreadState = dsl.objResponse({ id: 5 }).gen(schema);
// no "id" in top-most level or immediately under properties\items
expect(spreadState).toEqual({ id: null });
});

it("with missing parameters", () => {
const spreadState = transformers.default({ ida: "a" }).gen(schema);
const spreadState = dsl.objResponse({ ida: "a" }).gen(schema);
expect(spreadState).toEqual({ ida: null }); // Nothing to spread
});
});
16 changes: 8 additions & 8 deletions packages/unmock-core/src/__tests__/validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dsl } from "..";
import { Response, Responses, Schema } from "../service/interfaces";
import defMiddleware from "../service/state/transformers";
import { getValidStatesForOperationWithState } from "../service/state/validator";

const arraySchema: Schema = {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("with empty state", () => {
const spreadState = getValidStatesForOperationWithState(
op,
defMiddleware(),
dsl.objResponse(),
deref,
);
expect(spreadState.error).toBeUndefined();
Expand All @@ -76,7 +76,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("invalid parameter returns error", () => {
const spreadState = getValidStatesForOperationWithState(
op,
defMiddleware({
dsl.objResponse({
boom: 5,
}),
deref,
Expand All @@ -91,7 +91,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
200: { content: { "application/json": {} }, description: "foo" },
},
},
defMiddleware(),
dsl.objResponse(),
deref,
);
expect(spreadState.error).toContain("No schema defined");
Expand All @@ -100,7 +100,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("with $code specified", () => {
const spreadState = getValidStatesForOperationWithState(
op,
defMiddleware({
dsl.objResponse({
$code: 200,
tag: "foo",
}),
Expand All @@ -119,7 +119,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("with $size in top-level specified", () => {
const spreadState = getValidStatesForOperationWithState(
arrResponses,
defMiddleware({ $size: 5 }),
dsl.objResponse({ $size: 5 }),
deref,
);
expect(spreadState.error).toBeUndefined();
Expand All @@ -136,7 +136,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("with missing $code specified", () => {
const spreadState = getValidStatesForOperationWithState(
op,
defMiddleware({
dsl.objResponse({
$code: 404,
}),
deref,
Expand All @@ -150,7 +150,7 @@ describe("Tests getValidResponsesForOperationWithState", () => {
it("with no $code specified", () => {
const spreadState = getValidStatesForOperationWithState(
op,
defMiddleware({
dsl.objResponse({
test: { id: 5 },
}),
deref,
Expand Down
Loading

0 comments on commit cdd46ee

Please sign in to comment.