timestamp_view_test.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. 'use strict';
  5. describe('TimestampView', function() {
  6. it('formats long-ago timestamps correctly', function() {
  7. var timestamp = Date.now();
  8. var brief_view = new Whisper.BriefTimestampView().render(),
  9. ext_view = new Whisper.ExtendedTimestampView().render();
  10. // Helper functions to check absolute and relative timestamps
  11. // Helper to check an absolute TS for an exact match
  12. var check = function(view, ts, expected) {
  13. var result = view.getRelativeTimeSpanString(ts);
  14. assert.strictEqual(result, expected);
  15. };
  16. // Helper to check relative times for an exact match against both views
  17. var checkDiff = function(sec_ago, expected_brief, expected_ext) {
  18. check(brief_view, timestamp - sec_ago * 1000, expected_brief);
  19. check(ext_view, timestamp - sec_ago * 1000, expected_ext);
  20. };
  21. // Helper to check an absolute TS for an exact match against both views
  22. var checkAbs = function(ts, expected_brief, expected_ext) {
  23. if (!expected_ext) {
  24. expected_ext = expected_brief;
  25. }
  26. check(brief_view, ts, expected_brief);
  27. check(ext_view, ts, expected_ext);
  28. };
  29. // Helper to check an absolute TS for a match at the beginning against
  30. var checkStartsWith = function(view, ts, expected) {
  31. var result = view.getRelativeTimeSpanString(ts);
  32. var regexp = new RegExp("^" + expected);
  33. assert.match(result, regexp);
  34. };
  35. // check integer timestamp, JS Date object and moment object
  36. checkAbs(timestamp, 'now', 'a few seconds ago');
  37. checkAbs(new Date(), 'now', 'a few seconds ago');
  38. checkAbs(moment(), 'now', 'a few seconds ago');
  39. // check recent timestamps
  40. checkDiff(30, 'now', 'a few seconds ago'); // 30 seconds
  41. checkDiff(50, '1 min', 'a minute ago'); // >= 45 seconds => 1 minute
  42. checkDiff(40*60, '40 min', '40 minutes ago');
  43. checkDiff(60*60, '1 hour', 'an hour ago');
  44. checkDiff(125*60, '2 hours', '2 hours ago');
  45. // set to third of month to avoid problems on the 29th/30th/31st
  46. var last_month = moment().subtract(1, 'month').date(3),
  47. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  48. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  49. day_of_month = new Date().getDate();
  50. check(brief_view,last_month, months[last_month.month()] + ' 3');
  51. checkStartsWith(ext_view,last_month, months[last_month.month()] + ' 3');
  52. // subtract 26 hours to be safe in case of DST stuff
  53. var yesterday = new Date(timestamp - 26*60*60*1000),
  54. days_of_week = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  55. check(brief_view, yesterday, days_of_week[yesterday.getDay()]);
  56. checkStartsWith(ext_view, yesterday, days_of_week[yesterday.getDay()]);
  57. // Check something long ago
  58. // months are zero-indexed in JS for some reason
  59. check(brief_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012');
  60. checkStartsWith(ext_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012');
  61. });
  62. it('updates at reasonable intervals', function() {
  63. var view = new Whisper.TimestampView();
  64. assert.isBelow(view.computeDelay(1000), 60 * 1000); // < minute
  65. assert.strictEqual(view.computeDelay(1000 * 60 * 5), 60 * 1000); // minute
  66. assert.strictEqual(view.computeDelay(1000 * 60 * 60 * 5), 60 * 60 * 1000); // hour
  67. assert.isBelow(view.computeDelay(6 * 24 * 60 * 60 * 1000), 7 * 24 * 60 * 60 * 1000); // < week
  68. // return falsey value for long ago dates that don't update
  69. assert.notOk(view.computeDelay(1000 * 60 * 60 * 24 * 8));
  70. });
  71. });