Skip to content

Commit

Permalink
added tests for stateandcall decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 5, 2019
1 parent c429453 commit 601a393
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 0 deletions.
16 changes: 16 additions & 0 deletions __tests__/callFactories/callDecorators/addStateAndCallDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const object = {};
const originalCall = {
hello: "hello"
};
const addStateAndCallDecorator = require("../../../lib/callFactories/callDecorators/addStateAndCallDecorator");

describe("Tests for addStateAndCallDecorator", () => {
it("Adds call property", () => {
addStateAndCallDecorator(object, originalCall);
expect(object.call).toBeTruthy();
});
it("Adds state property", () => {
addStateAndCallDecorator(object, originalCall);
expect(object.state === null).toBeTruthy();
});
});
25 changes: 25 additions & 0 deletions __tests__/callFactories/callDecorators/clientStreamDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// on accepts a string... if it is not a string... it adds the callback to the data property... otherwise it just adds it to the actual one

// catch catches an error

module.exports = function(customCall, originalCall) {
customCall.on = function(event, callback) {
if (typeof event === "function") {
this.$call.on("data", event);
} else if (typeof event === "string") {
if (event === "data") {
this.$call.on("data", callback);
} else {
this.$call.on(event, callback);
}
} else {
throw new Error(
"type of first parameter must be string or callback function."
);
}
};

customCall.catch = function(callback) {
this.call.on("error", callback);
};
};
29 changes: 29 additions & 0 deletions __tests__/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// head... read straight off

// client unary means that head gets read straight off
// .on is a method that immediately invokes on the body
//this.body becomes the thing

module.exports = function clientUnaryDecorator(customCall, originalCall) {
customCall.body = originCall.request;

customCall.head = originalCall.metadata;

customCall.on = function(string, callback) {
if (typeof string === "function") {
string(this.body);
} else {
if (typeof string !== "string") {
throw new Error(
'.on takes "data" parameter and a callback to be invoked on data, or just the callback.'
);
} else {
if (string === "data") {
return callback(this.body);
} else {
throw new Error('Only the "data" event is support by unary .on');
}
}
}
};
};
49 changes: 49 additions & 0 deletions __tests__/callFactories/callDecorators/serverStreamDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// set()
// adds to a metadata property... if there is one... is adds it... otherwise it does not... adds it to the metadata property

//.send()
// writes

//throw
//sends an error

//.emit

const generateMeta = require("../../utils/generateMeta");

module.exports = function serverStreamDecorator(customCall, originalCall) {
customCall.metadata = undefined;
customCall.metadataSent = false;

customCall.throw = function(err) {
if (!(err instanceof Error)) {
throw new Error(
"Please pass your error details as an Error class. Firecomm supports adding additional error metadata in the trailers property using context.setStatus()"
);
}
this.call.emit("error", err);
};

customCall.set = function(object) {
// loop through
if (typeof object !== "object") {
throw new Error("Set should be passed an object with string values.");
}
if (this.metadata) {
this.metadata = {
...this.metadata,
...object
};
} else {
this.metadata;
}
};

customCall.customCall.send = function(message) {
if (!this.metadataSent && this.metadata) {
const metadata = generateMeta(this.metadata);
this.call.sendMetadata(metadata);
}
this.call.write(message);
};
};
92 changes: 92 additions & 0 deletions __tests__/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const grpc = require("grpc");
const generateMeta = require("../../utils/generateMeta");
// server unary

// .send () // writes
// .throw passes everything into call... if they pass a second parameter it becomes trailers
// set() adds things to the metadata...

module.exports = function serverUnaryDecorator(customCall, sendCallback) {
customCall.metadata = undefined;
customCall.sendCallback = sendCallback;

customCall.send = function(message) {
if (this.metadata) {
this.sendMetadata(this.metadata);
}
this.sendCallback(undefined);
};

customCall.set = function(object) {
// loop through
if (typeof object !== "object") {
throw new Error("Set should be passed an object with string values.");
}
if (this.metadata) {
this.metadata = {
...this.metadata,
...object
};
} else {
this.metadata;
}
};

customCall.throw = function(err, trailers) {
if (!(err instanceof Error)) {
throw new Error(
"Please pass your error details as an Error class. Firecomm supports adding additional error metadata in the trailers property using call.setStatus()"
);
}

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]]);
}

this.sendCallback(err, {}, this.trailers);
};
// ServerUnaryCallClone.send = function(message = {}) {
// if (this.metaData) {
// this.sendMetadata(this.metaData);
// }
// this.sendCallback(this.err, message, this.trailer);
// };
// return ServerUnaryCallClone;
// }

// ServerUnaryCallClone.throw = function(err) {
// if (!(err instanceof Error)) {
// throw new Error(
// "Please pass your error details as an Error class. Firecomm supports adding additional error metadata in the trailers property using call.setStatus()"
// );
// }
// this.sendCallback(err, {}, this.trailer);
// };
// ServerUnaryCallClone.setMeta = function(metaObject) {
// if (!this.metaData) {
// this.metaData = new grpc.Metadata();
// }
// const keys = Object.keys(metaObject);
// for (let i = 0; i < keys.length; i++) {
// this.metaData.set(keys[i], metaObject[keys[i]]);
// }
// };

// ServerUnaryCallClone.setStatus = function(metaObject) {
// if (!this.trailer) {
// this.trailer = new grpc.Metadata();
// }
// const keys = Object.keys(metaObject);
// for (let i = 0; i < keys.length; i++) {
// this.trailer.set(keys[i], metaObject[keys[i]]);
// }
// };
};

0 comments on commit 601a393

Please sign in to comment.