timestamp_view_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.TimestampView({brief: true}).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', 'now');
  37. checkAbs(new Date(), 'now', 'now');
  38. checkAbs(moment(), 'now', 'now');
  39. // check recent timestamps
  40. checkDiff(30, 'now', 'now'); // 30 seconds
  41. checkDiff(40*60, '40 minutes', '40 minutes ago');
  42. checkDiff(60*60, '1 hour', '1 hour ago');
  43. checkDiff(125*60, '2 hours', '2 hours ago');
  44. // set to third of month to avoid problems on the 29th/30th/31st
  45. var last_month = moment().subtract(1, 'month').date(3),
  46. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  47. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  48. day_of_month = new Date().getDate();
  49. check(brief_view,last_month, months[last_month.month()] + ' 3');
  50. checkStartsWith(ext_view,last_month, months[last_month.month()] + ' 3');
  51. // subtract 26 hours to be safe in case of DST stuff
  52. var yesterday = new Date(timestamp - 26*60*60*1000),
  53. days_of_week = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  54. check(brief_view, yesterday, days_of_week[yesterday.getDay()]);
  55. checkStartsWith(ext_view, yesterday, days_of_week[yesterday.getDay()]);
  56. // Check something long ago
  57. // months are zero-indexed in JS for some reason
  58. check(brief_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012');
  59. checkStartsWith(ext_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012');
  60. });
  61. describe('updates within a minute reasonable intervals', function() {
  62. var view;
  63. beforeEach(function() {
  64. view = new Whisper.TimestampView();
  65. });
  66. afterEach(function() {
  67. clearTimeout(view.timeout);
  68. });
  69. it('updates timestamps this minute within a minute', function() {
  70. var now = Date.now();
  71. view.$el.attr('data-timestamp', now - 1000);
  72. view.update();
  73. assert.isAbove(view.delay, 0); // non zero
  74. assert.isBelow(view.delay, 60 * 1000); // < minute
  75. });
  76. it('updates timestamps from this hour within a minute', function() {
  77. var now = Date.now();
  78. view.$el.attr('data-timestamp', now - 1000 - 1000*60*5); // 5 minutes and 1 sec ago
  79. view.update();
  80. assert.isAbove(view.delay, 0); // non zero
  81. assert.isBelow(view.delay, 60 * 1000); // minute
  82. });
  83. it('updates timestamps from today within an hour', function() {
  84. var now = Date.now();
  85. view.$el.attr('data-timestamp', now - 1000 - 1000*60*60*5); // 5 hours and 1 sec ago
  86. view.update();
  87. assert.isAbove(view.delay, 60 * 1000); // minute
  88. assert.isBelow(view.delay, 60 * 60 * 1000); // hour
  89. });
  90. it('updates timestamps from this week within a day', function() {
  91. var now = Date.now();
  92. view.$el.attr('data-timestamp', now - 1000 - 6*24*60*60*1000); // 6 days and 1 sec ago
  93. view.update();
  94. assert.isAbove(view.delay, 60 * 60 * 1000); // hour
  95. assert.isBelow(view.delay, 24 * 60 * 60 * 1000); // day
  96. });
  97. it('does not updates very old timestamps', function() {
  98. var now = Date.now();
  99. // return falsey value for long ago dates that don't update
  100. view.$el.attr('data-timestamp', now - 8*24*60*60*1000);
  101. view.update();
  102. assert.notOk(view.delay);
  103. });
  104. });
  105. });