Skip to content

Commit

Permalink
added tests for clientUnarydEcorator
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 7, 2019
1 parent 837950a commit d1a9a3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
42 changes: 36 additions & 6 deletions __tests__/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
const object = {};
const clientUnaryDecorator = require("../../../lib/callFactories/callDecorators/clientUnaryDecorator");

const customCall = {};

const mockOn = jest.fn();
const mockCallback = jest.fn();
const originalCall = {
hello: "hello"
hello: "hello",
request: "request",
metadata: "metadata",
on: mockOn
};
const clientUnaryDecorator = require("../../../lib/callFactories/callDecorators/clientUnaryDecorator");

clientUnaryDecorator(customCall, originalCall);

describe("tests for client unary decorator", () => {
describe("basic Tests for method assignment", () => {
it("should have an on", () => {
expect(customCall.hasOwnProperty("on")).toBeTruthy();
});

it("should assign the body property to be call's request", () => {
expect(customCall.body).toBe(originalCall.request);
});

it("should assign the head to be the metadata", () => {
expect(customCall.head).toBe(originalCall.metadata);
});
});

describe("tests for on method", () => {
beforeEach(() => {
clientUnaryDecorator(object, () => {});
mockOn.mockClear();
mockCallback.mockClear();
});
it("should have an on", () => {
expect(object.hasOwnProperty("on")).toBeTruthy();
it("on should accept a data string and callback", () => {
customCall.on("data", mockCallback);
expect(mockCallback.mock.calls.length).toBe(1);
});

it("should work without a string and just a callback", () => {
customCall.on(mockCallback);
expect(mockCallback.mock.calls.length).toBe(1);
});
});
});
3 changes: 3 additions & 0 deletions lib/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function clientUnaryDecorator(customCall, originalCall) {
customCall.head = originalCall.metadata;

customCall.on = function(string, callback) {
if (typeof string !== "function" && callback === undefined) {
throw new Error("Second parameter to .on must be a callback.");
}
if (typeof string === "function") {
string(this.body);
} else {
Expand Down

0 comments on commit d1a9a3c

Please sign in to comment.