From 080c233a93b157fa0b25e1ca4f6fc7f84243fcbc Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 27 Jan 2016 12:10:42 -0800 Subject: [PATCH] Add timestamp update interval test Break out delay computation into its own function and add tests, including a regression test for #646. // FREEBIE --- js/views/timestamp_view.js | 21 ++++++++++++--------- test/views/timestamp_view_test.js | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/js/views/timestamp_view.js b/js/views/timestamp_view.js index 7f475de2..fedff7d4 100644 --- a/js/views/timestamp_view.js +++ b/js/views/timestamp_view.js @@ -20,8 +20,18 @@ var result = this.getRelativeTimeSpanString(millis); this.$el.text(result); - var delay; var millis_since = millis_now - millis; + var delay = this.computeDelay(millis_since); + if (delay) { + if (delay < 0) { delay = 1000; } + this.timeout = setTimeout(this.update.bind(this), delay); + } + }, + clearTimeout: function() { + clearTimeout(this.timeout); + }, + computeDelay: function(millis_since) { + var delay; if (millis_since <= moment.relativeTimeThreshold('s') * 1000) { // a few seconds ago delay = 45 * 1000 - millis_since; @@ -42,14 +52,7 @@ return; } } - - if (delay) { - if (delay < 0) { delay = 1000; } - this.timeout = setTimeout(this.update.bind(this), delay); - } - }, - clearTimeout: function() { - clearTimeout(this.timeout); + return delay; } }); diff --git a/test/views/timestamp_view_test.js b/test/views/timestamp_view_test.js index 6d471208..fe0a0ed2 100644 --- a/test/views/timestamp_view_test.js +++ b/test/views/timestamp_view_test.js @@ -69,4 +69,18 @@ describe('TimestampView', function() { checkStartsWith(ext_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012'); }); + + it('updates at reasonable intervals', function() { + var view = new Whisper.TimestampView(); + assert(view.computeDelay(1000) < 60 * 1000); // < minute + assert.strictEqual(view.computeDelay(1000 * 60 * 5), 60 * 1000); // minute + assert.strictEqual(view.computeDelay(1000 * 60 * 60 * 5), 60 * 60 * 1000); // hour + + assert(view.computeDelay(6 * 24 * 60 * 60 * 1000) < 7 * 24 * 60 * 60 * 1000); // < week + + // return falsey value for long ago dates that don't update + assert.notOk(view.computeDelay(1000 * 60 * 60 * 24 * 8)); + + }); + });