From 35e3b889c3a2b1ec212b3e12912d50daf325e1e1 Mon Sep 17 00:00:00 2001 From: Bas van Wetten Date: Sat, 29 Jul 2017 16:02:53 +0200 Subject: [PATCH] Add new unit tests Add new unit tests for 'shorten' function in calendar module --- CHANGELOG.md | 1 + modules/default/calendar/calendar.js | 32 +++++++++++----------- tests/unit/functions/calendar_spec.js | 39 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90bae37b..9e69fcee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ 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 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 a5716d77..34077cde 100644 --- a/modules/default/calendar/calendar.js +++ b/modules/default/calendar/calendar.js @@ -437,25 +437,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) { @@ -467,12 +469,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 2c7f62f8..2dc78b2d 100644 --- a/tests/unit/functions/calendar_spec.js +++ b/tests/unit/functions/calendar_spec.js @@ -27,10 +27,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"); + }); + }); });