diff --git a/CHANGELOG.md b/CHANGELOG.md index 67a76e5e..aefee995 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/modules/default/calendar/calendar.js b/modules/default/calendar/calendar.js index 06a47e4b..0b73fa49 100644 --- a/modules/default/calendar/calendar.js +++ b/modules/default/calendar/calendar.js @@ -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) + "…"; + if (maxLength && typeof maxLength === "number" && string.length > maxLength) { + return string.trim().slice(0, maxLength) + "…"; } else { - return string; + return string.trim(); } } }, diff --git a/tests/unit/functions/calendar_spec.js b/tests/unit/functions/calendar_spec.js index be699847..3d65f497 100644 --- a/tests/unit/functions/calendar_spec.js +++ b/tests/unit/functions/calendar_spec.js @@ -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…" }, + "long string that needs shortening": { length: 16, return: "long string that…" }, + "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
wrapEvent test. Should wrap
the string instead of
shorten it if called with
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
test. Should wrap the string
instead of shorten it if called
with wrapEvent = true"); + }); + }); });