Skip to content

Commit

Permalink
typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
arvitaly committed Dec 10, 2016
1 parent cc79181 commit 8f211b0
Show file tree
Hide file tree
Showing 22 changed files with 447 additions and 420 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Place your settings in this file to overwrite default and user settings.
{

"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.js.map": true,
"**/*.js": {"when": "$(basename).ts"}


}
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-w", "-p", "."],
"showOutput": "silent",
"isWatching": true,
"problemMatcher": "$tsc-watch"
}
1 change: 1 addition & 0 deletions __tests__/__snapshots__/errors-spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports[`Errors unknown error type 1`] = `"Unknown transport type:: test1 for transport \"test2\""`;
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index-spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports[`NanoService when transport not setted should throw error 1`] = `[Error: Unknown transport type:: t for transport {"type":"fix1","opts":"fix2"}]`;
7 changes: 7 additions & 0 deletions __tests__/errors-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
const errors_1 = require("./../errors");
describe("Errors", () => {
it("unknown error type", () => {
expect(errors_1.default.unknownTransportType("test1", "test2")).toMatchSnapshot();
});
});
6 changes: 6 additions & 0 deletions __tests__/errors-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import errors from "./../errors";
describe("Errors", () => {
it("unknown error type", () => {
expect(errors.unknownTransportType("test1", "test2")).toMatchSnapshot();
});
});
111 changes: 111 additions & 0 deletions __tests__/index-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"use strict";
const _1 = require("./../");
const fixture1 = "fix1";
const fixture2 = "fix2";
const fixture3 = "fix3";
const fixture4 = "fix4";
describe("NanoService", () => {
it("when create without links and transports should work as event emitter with args and env", () => {
const service1 = (opts) => {
return {
in1: (a) => {
opts.out("out1", opts.args + a + fixture2 + opts.env("env1"));
},
};
};
const nanoservice = _1.default();
const n1 = nanoservice(service1, {
args: fixture4,
env: {
env1: fixture3,
},
});
let test;
n1.on("out1", (res) => {
test = res;
});
n1.emit("in1", fixture1);
expect(test).toBe(fixture4 + fixture1 + fixture2 + fixture3);
});
it("when transport not setted should throw error", () => {
const nanoservice = _1.default();
try {
nanoservice(() => { }, {
transports: { t: { type: fixture1, opts: fixture2 } },
});
}
catch (e) {
expect(e).toMatchSnapshot();
}
});
it("when transport setted, opts should be equal", () => {
const transport = jest.fn();
const nanoservice = _1.default({
transports: {
"tr1": transport,
},
});
nanoservice(() => { }, {
transports: { t: { type: "tr1", opts: fixture1 } },
});
expect(transport.mock.calls).toEqual([[fixture1]]);
});
describe("Transport", () => {
let inTransportSpy;
let transportSpy;
let serviceSpy;
let serviceLinkSpy;
let outTransportSpy;
let nanoservice;
beforeEach(() => {
// Create transport
transportSpy = jest.fn(() => {
return { in: inTransportSpy, out: outTransportSpy };
});
inTransportSpy = jest.fn();
outTransportSpy = jest.fn();
// Create service
serviceSpy = jest.fn();
serviceLinkSpy = jest.fn();
// Init nanoservice module with transport
nanoservice = _1.default({ transports: { "tr1": transportSpy } });
});
it("when set transport and in-link", () => {
serviceSpy = jest.fn(() => {
return {
in1: serviceLinkSpy,
};
});
// Create nanoservice
nanoservice(serviceSpy, {
links: [{ type: "in", name: "in1", transport: "t", to: fixture2 }],
transports: { t: { type: "tr1" } },
});
// Check transport in-subscribe
expect(inTransportSpy.mock.calls.length).toBe(1);
expect(inTransportSpy.mock.calls[0][0]).toBe(fixture2);
// Call transport in-link
inTransportSpy.mock.calls[0][1](fixture3);
// Check service in1 call
expect(serviceLinkSpy.mock.calls).toEqual([[fixture3]]);
});
it("when set transport and out-link", () => {
// Add transport out spy
const outTransportCallbackSpy = jest.fn();
outTransportSpy = jest.fn(() => {
return outTransportCallbackSpy;
});
// Create nanoservice
const nanoservice1 = nanoservice(serviceSpy, {
links: [{ type: "out", name: fixture1, transport: "t", to: fixture2 }],
transports: { t: { type: "tr1" } },
});
// Check transport out-subscribe
expect(outTransportSpy.mock.calls).toEqual([[fixture2]]);
// Subscribe on service
nanoservice1.emit(fixture1, fixture3);
// Check transport out call
expect(outTransportCallbackSpy.mock.calls).toEqual([[fixture3]]);
});
});
});
109 changes: 109 additions & 0 deletions __tests__/index-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Nanoservice, { IServiceOpts, NanoserviceFactory } from "./../";
const fixture1 = "fix1";
const fixture2 = "fix2";
const fixture3 = "fix3";
const fixture4 = "fix4";
describe("NanoService", () => {
it("when create without links and transports should work as event emitter with args and env", () => {
const service1 = (opts: IServiceOpts) => {
return {
in1: (a: any) => {
opts.out("out1", opts.args + a + fixture2 + opts.env("env1"));
},
};
};
const nanoservice = Nanoservice();
const n1 = nanoservice(service1, {
args: fixture4,
env: {
env1: fixture3,
},
});
let test: any;
n1.on("out1", (res: any) => {
test = res;
});
n1.emit("in1", fixture1);
expect(test).toBe(fixture4 + fixture1 + fixture2 + fixture3);
});
it("when transport not setted should throw error", () => {
const nanoservice = Nanoservice();
try {
nanoservice(() => { /* */ }, {
transports: { t: { type: fixture1, opts: fixture2 } },
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});
it("when transport setted, opts should be equal", () => {
const transport = jest.fn();
const nanoservice = Nanoservice({
transports: {
"tr1": transport,
},
});
nanoservice(() => { /* */ }, {
transports: { t: { type: "tr1", opts: fixture1 } },
});
expect(transport.mock.calls).toEqual([[fixture1]]);
});
describe("Transport", () => {
let inTransportSpy: jest.Mock<any>;
let transportSpy: jest.Mock<any>;
let serviceSpy: jest.Mock<any>;
let serviceLinkSpy: jest.Mock<any>;
let outTransportSpy: jest.Mock<any>;
let nanoservice: NanoserviceFactory;
beforeEach(() => {
// Create transport
transportSpy = jest.fn(() => {
return { in: inTransportSpy, out: outTransportSpy };
});
inTransportSpy = jest.fn();
outTransportSpy = jest.fn();
// Create service
serviceSpy = jest.fn();
serviceLinkSpy = jest.fn();
// Init nanoservice module with transport
nanoservice = Nanoservice({ transports: { "tr1": transportSpy } });
});
it("when set transport and in-link", () => {
serviceSpy = jest.fn(() => {
return {
in1: serviceLinkSpy,
};
});
// Create nanoservice
nanoservice(serviceSpy, {
links: [{ type: "in", name: "in1", transport: "t", to: fixture2 }],
transports: { t: { type: "tr1" } },
});
// Check transport in-subscribe
expect(inTransportSpy.mock.calls.length).toBe(1);
expect(inTransportSpy.mock.calls[0][0]).toBe(fixture2);
// Call transport in-link
inTransportSpy.mock.calls[0][1](fixture3);
// Check service in1 call
expect(serviceLinkSpy.mock.calls).toEqual([[fixture3]]);
});
it("when set transport and out-link", () => {
// Add transport out spy
const outTransportCallbackSpy = jest.fn();
outTransportSpy = jest.fn(() => {
return outTransportCallbackSpy;
});
// Create nanoservice
const nanoservice1 = nanoservice(serviceSpy, {
links: [{ type: "out", name: fixture1, transport: "t", to: fixture2 }],
transports: { t: { type: "tr1" } },
});
// Check transport out-subscribe
expect(outTransportSpy.mock.calls).toEqual([[fixture2]]);
// Subscribe on service
nanoservice1.emit(fixture1, fixture3);
// Check transport out call
expect(outTransportCallbackSpy.mock.calls).toEqual([[fixture3]]);
});
});
});
19 changes: 7 additions & 12 deletions errors.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
var code = 0;
module.exports = {
"use strict";
let code = 0;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
unknownTransportType: (type, opts) => {
return {
message: "Unknown transport type:: " + type,
code: ++code,
transport: opts,
toString: () => {
return "Unknown transport type:: " + type
}
}
}
}
return "Unknown transport type:: " + type + " for transport " + JSON.stringify(opts);
},
};
6 changes: 6 additions & 0 deletions errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let code = 0;
export default {
unknownTransportType: (type: string, opts: any) => {
return "Unknown transport type:: " + type + " for transport " + JSON.stringify(opts);
},
};

0 comments on commit 8f211b0

Please sign in to comment.