Merge pull request #970 from QNimbus/unittest-calendar

Add new unit tests
This commit is contained in:
Michael Teeuw 2017-07-31 15:04:25 +02:00 committed by GitHub
commit 4772fbcd4a
3 changed files with 57 additions and 17 deletions

View File

@ -10,7 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Add `clientonly` script to start only the electron client for a remote server.
- Add symbol and color properties of event when `CALENDAR_EVENTS` notification is broadcasted from `default/calendar` module.
- Add `.vscode/` folder to `.gitignore` to keep custom Visual Studio Code config out of git.
- Add unit test the capitalizeFirstLetter function of newfeed module
- Add unit test the capitalizeFirstLetter function of newfeed module.
- Add new unit tests for function `shorten` in calendar module.
### Updated
- Changed 'default.js' - listen on all attached interfaces by default.

View File

@ -424,25 +424,27 @@ Module.register("calendar", {
return defaultValue;
},
/* shorten(string, maxLength)
* Shortens a string if it's longer than maxLength.
* Adds an ellipsis to the end.
*
* argument string string - The string to shorten.
* argument maxLength number - The max length of the string.
* argument wrapEvents - Wrap the text after the line has reached maxLength
*
* return string - The shortened string.
/**
* Shortens a string if it's longer than maxLength and add a ellipsis to the end
*
* @param {string} string Text string to shorten
* @param {number} maxLength The max length of the string
* @param {boolean} wrapEvents Wrap the text after the line has reached maxLength
* @returns {string} The shortened string
*/
shorten: function (string, maxLength, wrapEvents) {
if (wrapEvents) {
if (typeof string !== "string") {
return "";
}
if (wrapEvents === true) {
var temp = "";
var currentLine = "";
var words = string.split(" ");
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (currentLine.length + word.length < 25 - 1) { // max - 1 to account for a space
if (currentLine.length + word.length < (typeof maxLength === "number" ? maxLength : 25) - 1) { // max - 1 to account for a space
currentLine += (word + " ");
} else {
if (currentLine.length > 0) {
@ -454,12 +456,12 @@ Module.register("calendar", {
}
}
return temp + currentLine;
return (temp + currentLine).trim();
} else {
if (string.length > maxLength) {
return string.slice(0, maxLength) + "&hellip;";
if (maxLength && typeof maxLength === "number" && string.length > maxLength) {
return string.trim().slice(0, maxLength) + "&hellip;";
} else {
return string;
return string.trim();
}
}
},

View File

@ -29,10 +29,47 @@ describe("Functions into modules/default/calendar/calendar.js", function() {
};
Object.keys(words).forEach(word => {
it(`for ${word} should return ${words[word]}`, function() {
it(`for '${word}' should return '${words[word]}'`, function() {
expect(Module.definitions.calendar.capFirst(word)).to.equal(words[word]);
});
});
});
describe("shorten", function() {
strings = {
" String with whitespace at the beginning that needs trimming" : { length: 16, return: "String with whit&hellip;" },
"long string that needs shortening": { length: 16, return: "long string that&hellip;" },
"short string": { length: 16, return: "short string" },
"long string with no maxLength defined": { return: "long string with no maxLength defined" },
};
Object.keys(strings).forEach(string => {
it(`for '${string}' should return '${strings[string].return}'`, function() {
expect(Module.definitions.calendar.shorten(string, strings[string].length)).to.equal(strings[string].return);
});
});
it("should return an empty string if shorten is called with a non-string", function () {
expect(Module.definitions.calendar.shorten(100)).to.equal("");
});
it("should not shorten the string if shorten is called with a non-number maxLength", function () {
expect(Module.definitions.calendar.shorten("This is a test string", "This is not a number")).to.equal("This is a test string");
});
it("should wrap the string instead of shorten it if shorten is called with wrapEvents = true (with maxLength defined as 20)", function () {
expect(Module.definitions.calendar.shorten(
"This is a wrapEvent test. Should wrap the string instead of shorten it if called with wrapEvent = true",
20,
true)).to.equal("This is a <br>wrapEvent test. Should wrap <br>the string instead of <br>shorten it if called with <br>wrapEvent = true");
});
it("should wrap the string instead of shorten it if shorten is called with wrapEvents = true (without maxLength defined, default 25)", function () {
expect(Module.definitions.calendar.shorten(
"This is a wrapEvent test. Should wrap the string instead of shorten it if called with wrapEvent = true",
undefined,
true)).to.equal("This is a wrapEvent <br>test. Should wrap the string <br>instead of shorten it if called <br>with wrapEvent = true");
});
});
});