Skip to content

Commit

Permalink
client and server sides both fully work with updated syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Aug 9, 2019
2 parents 2dc1732 + 8a2e3d0 commit 6659775
Show file tree
Hide file tree
Showing 21 changed files with 673 additions and 163 deletions.
20 changes: 20 additions & 0 deletions __tests__/callFactories/callDecorators/basicCallDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const object = {};
const originalCall = {
hello: "hello"
};
const basicCallDecorator = require("../../../lib/callFactories/callDecorators/basicCallDecorator");

describe("Tests for basicCallDecorator", () => {
it("Adds call property", () => {
basicCallDecorator(object, originalCall);
expect(object.call).toBeTruthy();
});
it("Adds state property", () => {
basicCallDecorator(object, originalCall);
expect(object.state === null).toBeTruthy();
});
it("Adds head property", () => {
basicCallDecorator(object, originalCall);
expect(object.hasOwnProperty("head")).toBeTruthy();
});
});
60 changes: 60 additions & 0 deletions __tests__/callFactories/callDecorators/clientStreamDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const basicCallDecorator = require("../../../lib/callFactories/callDecorators/basicCallDecorator");
const clientStreamDecorator = require("../../../lib/callFactories/callDecorators/clientStreamDecorator");

const object = {};
const mockOn = jest.fn();
const mockOnWrapper = (...args) => mockOn(...args);
const mockCallback = jest.fn();

const originalCall = {
hello: "hello",
on: mockOnWrapper
};

basicCallDecorator(object, originalCall);
clientStreamDecorator(object, originalCall);

describe("Client stream decorator tests.", () => {
describe("decorator adds the right methods", () => {
//adds properties
beforeEach(() => {});
it("Has an on property", () => {
expect(object.hasOwnProperty("on")).toBeTruthy();
});
it("Has a catch property", () => {
expect(object.hasOwnProperty("catch")).toBeTruthy();
});
});

describe("on method", () => {
beforeEach(() => {
mockOn.mockClear();
mockCallback.mockClear();
});
it("call's on is called with custom call's on", () => {
object.on(mockCallback);
expect(mockOn.mock.calls.length).toBe(1);
});
it("custom call's on works with no string", () => {
object.on(mockCallback);
expect(mockOn.mock.calls.length).toBe(1);
});
it("custom call's on works with a string", () => {
object.on("data", mockCallback);
expect(mockOn.mock.calls.length).toBe(1);
});
it("throws an error if string not data", () => {
expect(() => {
object.on("asdf", mockCallback);
expect(mockOn.mock.calls[0][0]).toBe("asdf");
});
});
});

describe("catch method", () => {
const myFun = () => {};
object.catch(myFun);
expect(mockOn.mock.calls[0][0]).toEqual("error");
expect(mockOn.mock.calls[0][1]).toBe(myFun);
});
});
46 changes: 46 additions & 0 deletions __tests__/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const clientUnaryDecorator = require("../../../lib/callFactories/callDecorators/clientUnaryDecorator");

const customCall = {};

const mockOn = jest.fn();
const mockCallback = jest.fn();
const originalCall = {
hello: "hello",
request: "request",
metadata: "metadata",
on: mockOn
};

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(() => {
mockOn.mockClear();
mockCallback.mockClear();
});
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);
});
});
});
91 changes: 91 additions & 0 deletions __tests__/callFactories/callDecorators/serverStreamDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const basicCallDecorator = require("../../../lib/callFactories/callDecorators/basicCallDecorator");
const serverStreamDecorator = require("../../../lib/callFactories/callDecorators/serverStreamDecorator");

const customCall = {};

const message = { message: "payload" };
const mockEmit = jest.fn();
const mockSendMetadata = jest.fn();
const mockWrite = jest.fn();

const originalCall = {
hello: "hello",
emit: mockEmit,
write: mockWrite,
sendMetadata: mockSendMetadata
};

basicCallDecorator(customCall, originalCall);
serverStreamDecorator(customCall, originalCall);

describe("server stream decorator tests.", () => {
beforeEach(() => {
mockWrite.mockClear();
mockEmit.mockClear();
mockSendMetadata.mockClear();
});
describe("decorator adds the right methods", () => {
//adds properties

it("Has an set property", () => {
expect(customCall.hasOwnProperty("set")).toBeTruthy();
});
it("Has an send property", () => {
expect(customCall.hasOwnProperty("send")).toBeTruthy();
});
it("Has an throw property", () => {
expect(customCall.hasOwnProperty("throw")).toBeTruthy();
});
});

describe("tests for throw", () => {
it("throw will throw if not an error", () => {
expect(() => customCall.throw("fake")).toThrow();
});
it("expect it to emit error", () => {
const fakeError = new Error("fake");
customCall.throw(fakeError);
expect(mockEmit.mock.calls[0][0]).toEqual("error");
expect(mockEmit.mock.calls[0][1]).toBe(fakeError);
});
});

describe("tests for set", () => {
it("adds a new metadata object", () => {
const newMetadata = { meta: "data" };
customCall.set(newMetadata);
expect(customCall.metadata).toBe(newMetadata);
});

it("does not lose metadata properties", () => {
const oldMeta = { old: "old" };
const newMeta = { new: "new" };
customCall.set(oldMeta);
customCall.set(newMeta);
expect(customCall.metadata.hasOwnProperty("new")).toBeTruthy();
});

it("metadata object overwrites old properties", () => {
const oldMeta = { old: "old" };
const newMeta = { old: "new" };
customCall.set(oldMeta);
customCall.set(newMeta);
expect(customCall.metadata.old).toBe("new");
});
});

describe("tests for send", () => {
it("sends message", () => {
customCall.send(message);
expect(mockWrite.mock.calls.length).toBe(1);
});

it("sends metadata as grpc metadata object", () => {
customCall.send(message);
expect(mockSendMetadata.mock.calls[0][0].constructor.name).toBe(
"Metadata"
);
expect(mockSendMetadata.mock.calls.length).toBe(1);
});
});
});
82 changes: 82 additions & 0 deletions __tests__/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const object = {};

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

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

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

basicCallDecorator(object, originalCall);
serverUnaryDecorator(object, mockSendCalback);

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

xdescribe("decorator adds the right methods", () => {
//adds properties
beforeEach(() => {
serverUnaryDecorator(object, originalCall);
});
it("Has an on property", () => {
expect(object.hasOwnProperty("set")).toBeTruthy();
});
it("Has a catch property", () => {
expect(object.hasOwnProperty("send")).toBeTruthy();
});
it("Has a throw property", () => {
expect(object.hasOwnProperty("throw")).toBeTruthy();
});
});

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

it("Throw passes error into sendCallback", () => {
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.", () => {
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", () => {
const fakeMetadata = { message: "hello" };
object.set(fakeMetadata);
object.send({ fakemessage: "fakemessage" });
expect(object.metadata.hasOwnProperty("message")).toBeTruthy();
});
});

describe("Tests for metadata passing", () => {
it("Passes send message into the send callback as the second parameter.", () => {
const fakeMetadata = { message: "hello" };
object.set(fakeMetadata);
object.send({ fakemessage: "fakemessage" });
expect(
mockSendMetadata.mock.calls[0][0].hasOwnProperty("message")
).toBeTruthy();
});
});
});
68 changes: 30 additions & 38 deletions __tests__/callFactories/generateClientStreamCall.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
const generateClientStreamCall = require('../../lib/callFactories/generateClientStreamCall')
const mockServerReadableStream = {on: () => {}};
const generateClientStreamCall = require("../../lib/callFactories/generateClientStreamCall");
const mockServerReadableStream = { on: () => {} };
const fakeCallback = (err, object, trailer) => {};
const mockServerReadable = generateClientStreamCall(mockServerReadableStream, fakeCallback);

describe('Unit tests for generating Client Stream Call', () => {

describe('Client Stream should have properties', () => {

test('Client Stream Call must have metaData property', () => {
expect(mockServerReadable.hasOwnProperty('metaData')).toBe(true);
})

test('Client Stream Call must have err property', () => {
expect(mockServerReadable.hasOwnProperty('err')).toBe(true);
})

test('Client Stream Call must have trailer property', () => {
expect(mockServerReadable.hasOwnProperty('trailer')).toBe(true);
})
const mockServerReadable = generateClientStreamCall(
mockServerReadableStream,
fakeCallback
);

describe("Unit tests for generating Client Stream Call", () => {
describe("Client Stream should have properties", () => {
test("Client Stream Call must have head property", () => {
expect(mockServerReadable.hasOwnProperty("head")).toBe(true);
});
});

describe('Client Stream should have methods', () => {

test('Client Stream Call must have throw method', () => {
expect(typeof mockServerReadable.throw === 'function').toBe(true);
})
describe("Client Stream should have methods", () => {
test("Client Stream Call must have throw method", () => {
expect(typeof mockServerReadable.throw === "function").toBe(true);
});

test('Client Stream Call must have setStatus method', () => {
expect(typeof mockServerReadable.setStatus === 'function').toBe(true);
})
test("Client Stream Call must have set method", () => {
expect(typeof mockServerReadable.set === "function").toBe(true);
});

test('Client Stream Call must have setMeta method', () => {
expect(typeof mockServerReadable.setMeta === 'function').toBe(true);
})
test("Client Stream Call must have catch method", () => {
expect(typeof mockServerReadable.catch === "function").toBe(true);
});

test('Client Stream Call must have send method', () => {
expect(typeof mockServerReadable.send === 'function').toBe(true);
})
test("Client Stream Call must have send method", () => {
expect(typeof mockServerReadable.send === "function").toBe(true);
});

test('Client Stream Call must have on method', () => {
expect(typeof mockServerReadable.on === 'function').toBe(true);
})
})
})
test("Client Stream Call must have on method", () => {
expect(typeof mockServerReadable.on === "function").toBe(true);
});
});
});

0 comments on commit 6659775

Please sign in to comment.