From f7b9384d261beaa70994340eb83d6b77ac6535af Mon Sep 17 00:00:00 2001 From: Zara Gebru Date: Fri, 15 Apr 2016 11:27:01 +0200 Subject: Issue #678: added utility converting time passed and dynamically changing version timestamp --- web-ui/app/js/helpers/view_helper.js | 44 ++++++++++++++++ web-ui/app/js/page/version.js | 14 ++++- web-ui/app/templates/page/version.hbs | 2 +- web-ui/config/add_git_version.sh | 2 +- web-ui/test/spec/helpers/view_helper.spec.js | 77 ++++++++++++++++++++++------ web-ui/test/spec/page/version.spec.js | 16 ++++++ 6 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 web-ui/test/spec/page/version.spec.js (limited to 'web-ui') diff --git a/web-ui/app/js/helpers/view_helper.js b/web-ui/app/js/helpers/view_helper.js index 7e07df75..f8152ff7 100644 --- a/web-ui/app/js/helpers/view_helper.js +++ b/web-ui/app/js/helpers/view_helper.js @@ -103,10 +103,53 @@ define( return fingerprint.replace(/(.{4})/g, '$1 ').trim(); } + function getSinceDate(sinceDate){ + var commitDate = new Date(sinceDate); + var number = Date.now(); + var millisecondsSince = number - commitDate; + + var SECONDS = 1000, + MIN = 60 * SECONDS, + HOUR = MIN * 60, + DAY = HOUR * 24, + WEEK = DAY * 7, + MONTH = WEEK * 4, + YEAR = DAY * 365; + + var years = Math.floor(millisecondsSince / YEAR); + if (years >= 1){ + return years + ' year(s)'; + } + + var months = Math.floor(millisecondsSince / MONTH); + if (months >= 1) { + return months + ' month(s)'; + } + + var weeks = Math.floor(millisecondsSince / WEEK); + if (weeks >= 1) { + return weeks + ' week(s)'; + } + + var days = Math.floor(millisecondsSince / DAY); + if (days >= 1) { + return days + ' day(s)'; + } + + var hours = Math.floor(millisecondsSince / HOUR); + if (hours >= 1) { + return hours + ' hour(s)'; + } + + var minutes = Math.floor(millisecondsSince / MIN); + return minutes + ' minute(s)'; + } + Handlebars.registerHelper('formatDate', formatDate); Handlebars.registerHelper('formatSize', formatSize); Handlebars.registerHelper('formatStatusClasses', formatStatusClasses); Handlebars.registerHelper('formatFingerPrint', formatFingerPrint); + Handlebars.registerHelper('sinceDate', getSinceDate); return { formatStatusClasses: formatStatusClasses, @@ -115,6 +158,7 @@ define( formatFingerPrint: formatFingerPrint, moveCaretToEndOfText: moveCaretToEndOfText, quoteMail: quoteMail, + sinceDate: getSinceDate, i18n: i18n }; }); diff --git a/web-ui/app/js/page/version.js b/web-ui/app/js/page/version.js index e5299f52..9fd5e629 100644 --- a/web-ui/app/js/page/version.js +++ b/web-ui/app/js/page/version.js @@ -14,13 +14,23 @@ * You should have received a copy of the GNU Affero General Public License * along with Pixelated. If not, see . */ -define(['flight/lib/component', 'views/templates'], function (defineComponent, templates) { +define(['flight/lib/component', 'views/templates', 'helpers/view_helper'], function (defineComponent, templates, viewHelper) { 'use strict'; return defineComponent(function () { - + this.defaultAttrs({ + 'sinceDate': '#version-date' + }); + this.render = function () { this.$node.html(templates.page.version()); + this.renderCommitDate(); + }; + + this.renderCommitDate = function(){ + var since = this.select('sinceDate').attr('data-since'), + commitDate = viewHelper.sinceDate(since); + this.select('sinceDate').html(commitDate + ' ago'); }; this.after('initialize', function () { diff --git a/web-ui/app/templates/page/version.hbs b/web-ui/app/templates/page/version.hbs index 181d2151..00344f60 100644 --- a/web-ui/app/templates/page/version.hbs +++ b/web-ui/app/templates/page/version.hbs @@ -1,2 +1,2 @@ version: UNKNOWN_VERSION
-COMMIT_DATE + diff --git a/web-ui/config/add_git_version.sh b/web-ui/config/add_git_version.sh index b9cfba72..5bb5df2d 100755 --- a/web-ui/config/add_git_version.sh +++ b/web-ui/config/add_git_version.sh @@ -3,7 +3,7 @@ TEMPLATE_FILE="app/js/generated/hbs/templates.js" COMMITISH=$(git rev-parse --short HEAD) -COMMITDATE=$(git show -s --format=%cr) +COMMITDATE=$(git show -s --format=%cI) perl -pi -e "s/UNKNOWN_VERSION/$COMMITISH/" $TEMPLATE_FILE perl -pi -e "s/COMMIT_DATE/$COMMITDATE/" $TEMPLATE_FILE diff --git a/web-ui/test/spec/helpers/view_helper.spec.js b/web-ui/test/spec/helpers/view_helper.spec.js index 19bef15f..5beba137 100644 --- a/web-ui/test/spec/helpers/view_helper.spec.js +++ b/web-ui/test/spec/helpers/view_helper.spec.js @@ -2,13 +2,13 @@ define(['helpers/view_helper'], function (viewHelper) { 'use strict'; var testData; - describe('view helper', function() { + describe('view helper', function () { beforeEach(function () { testData = Pixelated.testData(); }); - describe('quote email', function() { - it('should add > to body text', function() { + describe('quote email', function () { + it('should add > to body text', function () { testData.parsedMail.simpleTextPlain.textPlainBody = 'First Line\nSecond Line'; var quotedMail = viewHelper.quoteMail(testData.parsedMail.simpleTextPlain); @@ -16,7 +16,7 @@ define(['helpers/view_helper'], function (viewHelper) { expect(quotedMail).toContain('> First Line\n> Second Line'); }); - it('should add the mail sender information', function() { + it('should add the mail sender information', function () { testData.parsedMail.simpleTextPlain.textPlainBody = 'First Line\nSecond Line'; var quotedMail = viewHelper.quoteMail(testData.parsedMail.simpleTextPlain); @@ -25,25 +25,25 @@ define(['helpers/view_helper'], function (viewHelper) { }); }); - describe('formatDate', function() { + describe('formatDate', function () { var template; beforeEach(function () { template = Handlebars.compile('{{formatDate date}}'); }); - it('formats correctly a Date for today', function() { + it('formats correctly a Date for today', function () { var d = new Date(); var mailDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 14, 2, 36); - var result = template({ date: mailDate.toISOString() }); + var result = template({date: mailDate.toISOString()}); expect(result).toEqual('14:02'); }); - it('formats correctly a Date for a specific day', function() { + it('formats correctly a Date for a specific day', function () { var mailDate = new Date(2013, 2, 13, 7, 56, 1); - var result = template({ date: mailDate.toISOString() }); + var result = template({date: mailDate.toISOString()}); // This expectation is weird for the month - JS Dates have date numbers be zero-indexed, thus the discrepancy // Specifically, the 2 in the constructor DOES match the 3 in the expectation below. @@ -51,31 +51,76 @@ define(['helpers/view_helper'], function (viewHelper) { }); }); - describe('formatSize', function() { + describe('formatSize', function () { var template; beforeEach(function () { template = Handlebars.compile('{{formatSize size}}'); }); - it('formats size to bytes', function() { + it('formats size to bytes', function () { var bytes = 42; - var result = template({ size: bytes }); + var result = template({size: bytes}); expect(result).toEqual('42.00 b'); }); - it('formats size to kilobytes', function() { + it('formats size to kilobytes', function () { var bytes = 4200; - var result = template({ size: bytes }); + var result = template({size: bytes}); expect(result).toEqual('4.10 Kb'); }); - it('formats size to megabytes', function() { + it('formats size to megabytes', function () { var bytes = 4200000; - var result = template({ size: bytes }); + var result = template({size: bytes}); expect(result).toEqual('4.01 Mb'); }); }); + describe('sinceDate', function () { + var template; + beforeEach(function () { + template = Handlebars.compile('{{sinceDate commitDate}}'); + var milliseconds_on_2016_04_08_16h_20m_00s = 1460126400000; + spyOn(Date, 'now').and.returnValue(milliseconds_on_2016_04_08_16h_20m_00s); + }); + + it('gives time passed since date in minutes', function () { + var twenty_minutes_ago = "2016-04-08T16:20:00+02:00"; + var result = template({commitDate: twenty_minutes_ago}); + expect(result).toEqual('20 minute(s)'); + }); + + it('gives time passed since date above 60 min and less than a day in hours when ', function () { + var two_hours_ago = "2016-04-08T14:20:00+02:00"; + var result = template({commitDate: two_hours_ago}); + expect(result).toEqual('2 hour(s)'); + }); + + it('gives time passed since date above one day and less than a week in hours when ', function () { + var one_day_ago = "2016-04-07T16:20:00+02:00"; + var result = template({commitDate: one_day_ago}); + expect(result).toEqual('1 day(s)'); + }); + + it('gives time passed since date above one week and less than a month in hours when ', function () { + var one_week_ago = "2016-03-30T16:20:00+02:00"; + var result = template({commitDate: one_week_ago}); + expect(result).toEqual('1 week(s)'); + }); + + it('gives time passed since date above one month and less than a year in hours when ', function () { + var three_month_ago = "2016-01-03T16:20:00+02:00"; + var result = template({commitDate: three_month_ago}); + expect(result).toEqual('3 month(s)'); + }); + + it('gives time passed since date more then one year ago', function () { + var two_years_ago = "2014-03-08T16:20:00+02:00"; + var result = template({commitDate: two_years_ago}); + expect(result).toEqual('2 year(s)'); + }); + }); + describe('format status classes', function () { it('formats all the status of the email to css classes', function () { var statuses = ['read', 'banana']; diff --git a/web-ui/test/spec/page/version.spec.js b/web-ui/test/spec/page/version.spec.js new file mode 100644 index 00000000..6c1c741f --- /dev/null +++ b/web-ui/test/spec/page/version.spec.js @@ -0,0 +1,16 @@ +describeComponent('page/version', function () { + 'use strict'; + beforeEach(function () { + this.setupComponent('
0.3.1-beta
'); + }); + + + describe('render version on the left nav bar', function () { + it('should render commit sha and comm', function () { + expect(this.$node.html()).toContain('version: '); + expect(this.$node.html()).toContain('ago'); + expect(this.$node.html()).not.toContain('NaN'); + }); + }); + +}); -- cgit v1.2.3