From e0414e4eb9419449a94b83a9642fc7ac20f831ae Mon Sep 17 00:00:00 2001 From: Bas van Wetten Date: Mon, 31 Jul 2017 22:09:08 +0200 Subject: [PATCH] Add unittest for calendar module Add unittest for 'getLocalSpecification' function. --- CHANGELOG.md | 1 + modules/default/calendar/calendar.js | 50 +++++++++++---------- tests/unit/functions/calendar_spec.js | 63 ++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aefee995..b4c953e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - 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 new unit tests for function `shorten` in calendar module. +- Add new unit tests for function `getLocaleSpecification` 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 0b73fa49..ad71ea75 100644 --- a/modules/default/calendar/calendar.js +++ b/modules/default/calendar/calendar.js @@ -67,30 +67,7 @@ Module.register("calendar", { Log.log("Starting module: " + this.name); // Set locale. - moment.locale(config.language); - - switch (config.timeFormat) { - case 12: { - moment.updateLocale(config.language, { - longDateFormat: { - LT: "h:mm A" - } - }); - break; - } - case 24: { - moment.updateLocale(config.language, { - longDateFormat: { - LT: "HH:mm" - } - }); - break; - } - // If config.timeFormat was not given (or has invalid format) default to locale default - default: { - break; - } - } + moment.updateLocale(config.language, this.getLocaleSpecification(config.timeFormat)); for (var c in this.config.calendars) { var calendar = this.config.calendars[c]; @@ -306,6 +283,31 @@ Module.register("calendar", { return wrapper; }, + /** + * This function accepts a number (either 12 or 24) and returns a moment.js LocaleSpecification with the + * corresponding timeformat to be used in the calendar display. If no number is given (or otherwise invalid input) + * it will a localeSpecification object with the system locale time format. + * + * @param {number} timeFormat Specifies either 12 or 24 hour time format + * @returns {moment.LocaleSpecification} + */ + getLocaleSpecification: function(timeFormat) { + switch (timeFormat) { + case 12: { + return { longDateFormat: {LT: "h:mm A"} }; + break; + } + case 24: { + return { longDateFormat: {LT: "HH:mm"} }; + break; + } + default: { + return { longDateFormat: {LT: moment.localeData().longDateFormat("LT")} }; + break; + } + } + }, + /* hasCalendarURL(url) * Check if this config contains the calendar url. * diff --git a/tests/unit/functions/calendar_spec.js b/tests/unit/functions/calendar_spec.js index 3d65f497..d4035f33 100644 --- a/tests/unit/functions/calendar_spec.js +++ b/tests/unit/functions/calendar_spec.js @@ -1,9 +1,6 @@ -var fs = require("fs"); -var path = require("path"); -var chai = require("chai"); -var expect = chai.expect; -var vm = require("vm"); +const expect = require("chai").expect; +global.moment = require("moment"); describe("Functions into modules/default/calendar/calendar.js", function() { @@ -35,6 +32,62 @@ describe("Functions into modules/default/calendar/calendar.js", function() { }); }); + describe("getLocaleSpecification", function() { + it("Should return a valid moment.LocaleSpecification for a 12-hour format", function() { + expect(Module.definitions.calendar.getLocaleSpecification(12)).to.deep.equal({ longDateFormat: {LT: "h:mm A"} }); + }); + + it("Should return a valid moment.LocaleSpecification for a 24-hour format", function() { + expect(Module.definitions.calendar.getLocaleSpecification(24)).to.deep.equal({ longDateFormat: {LT: "HH:mm"} }); + }); + + it("Should return the current system locale when called without timeFormat number", function() { + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: moment.localeData().longDateFormat("LT")} } ); + }); + + it("Should return a 12-hour longDateFormat when using the 'en' locale", function() { + var localeBackup = moment.locale(); + moment.locale("en"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "h:mm A"} }); + moment.locale(localeBackup); + }); + + it("Should return a 12-hour longDateFormat when using the 'au' locale", function() { + var localeBackup = moment.locale(); + moment.locale("au"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "h:mm A"} }); + moment.locale(localeBackup); + }); + + it("Should return a 12-hour longDateFormat when using the 'eg' locale", function() { + var localeBackup = moment.locale(); + moment.locale("eg"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "h:mm A"} }); + moment.locale(localeBackup); + }); + + it("Should return a 24-hour longDateFormat when using the 'nl' locale", function() { + var localeBackup = moment.locale(); + moment.locale("nl"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "HH:mm"} }); + moment.locale(localeBackup); + }); + + it("Should return a 24-hour longDateFormat when using the 'fr' locale", function() { + var localeBackup = moment.locale(); + moment.locale("fr"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "HH:mm"} }); + moment.locale(localeBackup); + }); + + it("Should return a 24-hour longDateFormat when using the 'uk' locale", function() { + var localeBackup = moment.locale(); + moment.locale("uk"); + expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: {LT: "HH:mm"} }); + moment.locale(localeBackup); + }); + }); + describe("shorten", function() { strings = { " String with whitespace at the beginning that needs trimming" : { length: 16, return: "String with whit…" },