From 701cdae433773a2db275f7e440a8da5385a7a51f Mon Sep 17 00:00:00 2001 From: Rafael Specht Date: Thu, 9 Aug 2018 12:11:37 -0300 Subject: [PATCH 1/2] Updating timeAgo function, creating a 'helpers' file to be easy to import to test --- .../client/views/app/directory.js | 13 +---- .../rocketchat-ui/client/views/app/helpers.js | 19 ++++++++ .../client/views/app/tests/helpers.tests.js | 48 +++++++++++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 packages/rocketchat-ui/client/views/app/helpers.js create mode 100644 packages/rocketchat-ui/client/views/app/tests/helpers.tests.js diff --git a/packages/rocketchat-ui/client/views/app/directory.js b/packages/rocketchat-ui/client/views/app/directory.js index 3c8a3e68cc9f..dc46d3416989 100644 --- a/packages/rocketchat-ui/client/views/app/directory.js +++ b/packages/rocketchat-ui/client/views/app/directory.js @@ -1,16 +1,5 @@ -import moment from 'moment'; import _ from 'underscore'; - -function timeAgo(time) { - if (!time) { - return; - } - - const now = new Date(); - const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1); - - return (now.getDate() === time.getDate() && moment(time).format('LT')) || (yesterday.getDate() === time.getDate() && t('yesterday')) || moment(time).format('MMM D, YYYY'); -} +import { timeAgo } from './helpers'; function directorySearch(config, cb) { return Meteor.call('browseChannels', config, (err, result) => { diff --git a/packages/rocketchat-ui/client/views/app/helpers.js b/packages/rocketchat-ui/client/views/app/helpers.js new file mode 100644 index 000000000000..1af4d5792c92 --- /dev/null +++ b/packages/rocketchat-ui/client/views/app/helpers.js @@ -0,0 +1,19 @@ +import moment from 'moment'; + +export function timeAgo(time, now = new Date()) { + if (!time) { + return; + } + + const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1); + const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + + const isToday = time.getFullYear() >= today.getFullYear() && time.getMonth() >= today.getMonth() && time.getDate() >= today.getDate(); + const wasYesterday = time.getFullYear() >= yesterday.getFullYear() && time.getMonth() >= yesterday.getMonth() && time.getDate() >= yesterday.getDate(); + + const todayFormatted = (isToday && moment(time).format('LT')); + const yesterdayFormatted = (wasYesterday && 'yesterday'); + const beforeFormatted = moment(time).format('MMM D, YYYY'); + + return todayFormatted || yesterdayFormatted || beforeFormatted; +} diff --git a/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js b/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js new file mode 100644 index 000000000000..c212542ea5d1 --- /dev/null +++ b/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js @@ -0,0 +1,48 @@ +/* eslint-env mocha */ +import 'babel-polyfill'; +import assert from 'assert'; +import { timeAgo } from '../helpers'; + +describe('Helpers', () => { + describe('timeAgo', () => { + it('returns formated time when the passed value is on the same day', () => { + const now = new Date('Mon Aug 06 2018 22:00:00 GMT-0300'); + + const t1 = new Date('Mon Aug 06 2018 01:00:00 GMT-0300'); + const t2 = new Date('Mon Aug 06 2018 10:00:10 GMT-0300'); + const t3 = new Date('Mon Aug 06 2018 14:30:10 GMT-0300'); + + assert.equal(timeAgo(t1, now), '1:00 AM'); + assert.equal(timeAgo(t2, now), '10:00 AM'); + assert.equal(timeAgo(t3, now), '2:30 PM'); + }); + + it('returns "yesterday" when the passed value is on the day before', () => { + const now = new Date('Tue Jul 03 2018 23:00:00 GMT-0300'); + + const t1 = new Date('Tue Jul 02 2018 23:59:00 GMT-0300'); + const t2 = new Date('Tue Jul 02 2018 22:30:00 GMT-0300'); + const t3 = new Date('Tue Jul 02 2018 01:00:00 GMT-0300'); + + assert.equal(timeAgo(t1, now), 'yesterday'); + assert.equal(timeAgo(t2, now), 'yesterday'); + assert.equal(timeAgo(t3, now), 'yesterday'); + }); + + it('returns formated date when the passed value two or more days before', () => { + const now = new Date('Wed Jun 20 2018 00:00:01 GMT-0300'); + + const t1 = new Date('Mon Jun 18 2018 10:00:00 GMT-0300'); + const t2 = new Date('Sun Jun 10 2018 00:00:00 GMT-0300'); + const t3 = new Date('Thu May 10 2018 00:00:00 GMT-0300'); + const t4 = new Date('Sun May 20 2018 00:00:01 GMT-0300'); + const t5 = new Date('Fri Nov 10 2017 00:00:00 GMT-0300'); + + assert.equal(timeAgo(t1, now), 'Jun 18, 2018'); + assert.equal(timeAgo(t2, now), 'Jun 10, 2018'); + assert.equal(timeAgo(t3, now), 'May 10, 2018'); + assert.equal(timeAgo(t4, now), 'May 20, 2018'); + assert.equal(timeAgo(t5, now), 'Nov 10, 2017'); + }); + }); +}); From b14e014c2a7e81a125e2c0da5b69f005013d3a70 Mon Sep 17 00:00:00 2001 From: Rafael Specht Date: Thu, 9 Aug 2018 14:24:06 -0300 Subject: [PATCH 2/2] Removing GMT time from tests to debug CI --- .../client/views/app/tests/helpers.tests.js | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js b/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js index c212542ea5d1..dec796805df9 100644 --- a/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js +++ b/packages/rocketchat-ui/client/views/app/tests/helpers.tests.js @@ -6,11 +6,11 @@ import { timeAgo } from '../helpers'; describe('Helpers', () => { describe('timeAgo', () => { it('returns formated time when the passed value is on the same day', () => { - const now = new Date('Mon Aug 06 2018 22:00:00 GMT-0300'); + const now = new Date('Mon Aug 06 2018 22:00:00'); - const t1 = new Date('Mon Aug 06 2018 01:00:00 GMT-0300'); - const t2 = new Date('Mon Aug 06 2018 10:00:10 GMT-0300'); - const t3 = new Date('Mon Aug 06 2018 14:30:10 GMT-0300'); + const t1 = new Date('Mon Aug 06 2018 01:00:00'); + const t2 = new Date('Mon Aug 06 2018 10:00:10'); + const t3 = new Date('Mon Aug 06 2018 14:30:10'); assert.equal(timeAgo(t1, now), '1:00 AM'); assert.equal(timeAgo(t2, now), '10:00 AM'); @@ -18,11 +18,11 @@ describe('Helpers', () => { }); it('returns "yesterday" when the passed value is on the day before', () => { - const now = new Date('Tue Jul 03 2018 23:00:00 GMT-0300'); + const now = new Date('Tue Jul 03 2018 23:00:00'); - const t1 = new Date('Tue Jul 02 2018 23:59:00 GMT-0300'); - const t2 = new Date('Tue Jul 02 2018 22:30:00 GMT-0300'); - const t3 = new Date('Tue Jul 02 2018 01:00:00 GMT-0300'); + const t1 = new Date('Tue Jul 02 2018 23:59:00'); + const t2 = new Date('Tue Jul 02 2018 22:30:00'); + const t3 = new Date('Tue Jul 02 2018 01:00:00'); assert.equal(timeAgo(t1, now), 'yesterday'); assert.equal(timeAgo(t2, now), 'yesterday'); @@ -30,13 +30,13 @@ describe('Helpers', () => { }); it('returns formated date when the passed value two or more days before', () => { - const now = new Date('Wed Jun 20 2018 00:00:01 GMT-0300'); + const now = new Date('Wed Jun 20 2018 00:00:01'); - const t1 = new Date('Mon Jun 18 2018 10:00:00 GMT-0300'); - const t2 = new Date('Sun Jun 10 2018 00:00:00 GMT-0300'); - const t3 = new Date('Thu May 10 2018 00:00:00 GMT-0300'); - const t4 = new Date('Sun May 20 2018 00:00:01 GMT-0300'); - const t5 = new Date('Fri Nov 10 2017 00:00:00 GMT-0300'); + const t1 = new Date('Mon Jun 18 2018 10:00:00'); + const t2 = new Date('Sun Jun 10 2018 00:00:00'); + const t3 = new Date('Thu May 10 2018 00:00:00'); + const t4 = new Date('Sun May 20 2018 00:00:01'); + const t5 = new Date('Fri Nov 10 2017 00:00:00'); assert.equal(timeAgo(t1, now), 'Jun 18, 2018'); assert.equal(timeAgo(t2, now), 'Jun 10, 2018');