Skip to content

Commit

Permalink
working on adding tests to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 6, 2019
1 parent 47015f7 commit 1f7a37e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
67 changes: 65 additions & 2 deletions __tests__/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
const object = {};

const mockSendCalback = jest.fn();
const mockSendMetadata = jest.fn();

const originalCall = {
hello: "hello"
hello: "hello",
sendMetadata: mockSendMetadata
};

const serverUnaryDecorator = require("../../../lib/callFactories/callDecorators/serverUnaryDecorator");

describe("Server unary decorator tests.", () => {
describe("decorator adds the right methods", () => {
beforeEach(() => {
mockSendCalback.mockClear();
});

xdescribe("decorator adds the right methods", () => {
//adds properties
beforeEach(() => {
serverUnaryDecorator(object, originalCall);
Expand All @@ -20,4 +30,57 @@ describe("Server unary decorator tests.", () => {
expect(object.hasOwnProperty("throw")).toBeTruthy();
});
});

xdescribe("Tests for throw method", () => {
it("Expect throw to throw if not type error passed.", () => {
serverUnaryDecorator(object, mockSendCalback);
const fakeError = "hello";
expect(() => {
object.throw(fakeError);
}).toThrow();
});

it("Throw passes error into sendCallback", () => {
serverUnaryDecorator(object, mockSendCalback);
const fakeError = new Error("fake error");
object.throw(fakeError);
expect(mockSendCalback.mock.calls[0][0] instanceof Error).toBeTruthy();
});
});

xdescribe("Tests for send method", () => {
it("Passes send message into the send callback as the second parameter.", () => {
serverUnaryDecorator(object, mockSendCalback);
const fakeMessage = { message: "hello" };
object.send(fakeMessage);
expect(
mockSendCalback.mock.calls[0][1].hasOwnProperty("message")
).toBeTruthy();
});
});

describe("Tests for set method", () => {
it("Adds new properties to the metadata object", () => {
serverUnaryDecorator(object, mockSendCalback);
const fakeMetadata = { message: "hello" };
object.set(fakeMetadata);
expect(
mockSendCalback.mock.calls[0][1].hasOwnProperty("message")
).toBeTruthy();
});
});

describe("Tests for metadata passing", () => {
it("Passes send message into the send callback as the second parameter.", () => {
// add metadata to the call with set
//send
//check if send metadata was called
// serverUnaryDecorator(object, mockSendCalback);
// const fakeMessage = { message: "hello" };
// object.send(fakeMessage);
// expect(
// mockSendCalback.mock.calls[0][1].hasOwnProperty("message")
// ).toBeTruthy();
});
});
});
10 changes: 5 additions & 5 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const firstChat = {

const { log: c } = console;

stub
.unaryChat(firstChat)
.then(res => console.log(res))
.catch(err => console.log(err));
// stub
// .unaryChat(firstChat)
// .then(res => console.log(res))
// .catch(err => console.log(err));

const testUnaryChat = () => {
// console.log(stub.getChannel().getConnectivityState(true))
Expand All @@ -62,7 +62,7 @@ const testClientStream = () => {
clientStream.end();
};

// testClientStream();
testClientStream();

const testServerStream = () => {
const serverStream = stub.serverStream(firstChat);
Expand Down
2 changes: 1 addition & 1 deletion examples/firecomm/methodHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function clientStream(context) {
// console.log('serverStream context: ', context);
context.on("data", data => {
console.log(data);
context.send({ message: "world" });
});
context.send({ message: "world" });
}

function bidiChat(context) {
Expand Down
22 changes: 13 additions & 9 deletions lib/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ module.exports = function serverUnaryDecorator(customCall, sendCallback) {
);
}

if (typeof trailers !== "object") {
throw new Error(
"Please input trailers, second parameter to Throw as an object."
);
}
this.trailers = generateMeta(trailers);
if (typeof trailers !== "undefined") {
if (typeof trailers !== "object") {
throw new Error(
"Please input trailers, second parameter to Throw as an object."
);
}
this.trailers = generateMeta(trailers);

const keys = Object.keys(trailers);
for (let i = 0; i < keys.length; i++) {
this.trailer.set(keys[i], metaObject[keys[i]]);
const keys = Object.keys(trailers);
for (let i = 0; i < keys.length; i++) {
this.trailer.set(keys[i], metaObject[keys[i]]);
}
} else {
this.trailers = trailers;
}

this.sendCallback(err, {}, this.trailers);
Expand Down

0 comments on commit 1f7a37e

Please sign in to comment.