Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import {FORMATTER, MS_PER_MONTH, standardTime, |
| 7 | relativeTime} from './chops-timestamp-helpers.js'; |
| 8 | import sinon from 'sinon'; |
| 9 | |
| 10 | // The formatted date strings differ based on time zone and browser, so we can't |
| 11 | // use static strings for testing. We can't stub out the format method because |
| 12 | // it's native code and can't be modified. So just use the FORMATTER object. |
| 13 | |
| 14 | let clock; |
| 15 | |
| 16 | describe('chops-timestamp-helpers', () => { |
| 17 | beforeEach(() => { |
| 18 | // Set clock to the Epoch. |
| 19 | clock = sinon.useFakeTimers({ |
| 20 | now: new Date(0), |
| 21 | shouldAdvanceTime: false, |
| 22 | }); |
| 23 | }); |
| 24 | |
| 25 | afterEach(() => { |
| 26 | clock.restore(); |
| 27 | }); |
| 28 | |
| 29 | describe('standardTime', () => { |
| 30 | it('shows relative timestamp when less than a month ago', () => { |
| 31 | const date = new Date(); |
| 32 | assert.equal(standardTime(date), `${FORMATTER.format(date)} (just now)`); |
| 33 | }); |
| 34 | |
| 35 | it('no relative time when more than a month in the future', () => { |
| 36 | const date = new Date(1548808276 * 1000); |
| 37 | assert.equal(standardTime(date), 'Tue, Jan 29, 2019, 4:31 PM PST'); |
| 38 | }); |
| 39 | |
| 40 | it('no relative time when more than a month in the past', () => { |
| 41 | // Jan 29, 2019, 4:31 PM PST |
| 42 | const now = 1548808276 * 1000; |
| 43 | clock.tick(now); |
| 44 | |
| 45 | const date = new Date(now - MS_PER_MONTH); |
| 46 | assert.equal(standardTime(date), 'Sun, Dec 30, 2018, 4:31 PM PST'); |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | it('relativeTime future', () => { |
| 51 | assert.equal(relativeTime(new Date()), `just now`); |
| 52 | |
| 53 | assert.equal(relativeTime(new Date(59 * 1000)), `just now`); |
| 54 | |
| 55 | assert.equal(relativeTime(new Date(60 * 1000)), `a minute from now`); |
| 56 | assert.equal(relativeTime(new Date(2 * 60 * 1000)), |
| 57 | `2 minutes from now`); |
| 58 | assert.equal(relativeTime(new Date(59 * 60 * 1000)), |
| 59 | `59 minutes from now`); |
| 60 | |
| 61 | assert.equal(relativeTime(new Date(60 * 60 * 1000)), `an hour from now`); |
| 62 | assert.equal(relativeTime(new Date(2 * 60 * 60 * 1000)), |
| 63 | `2 hours from now`); |
| 64 | assert.equal(relativeTime(new Date(23 * 60 * 60 * 1000)), |
| 65 | `23 hours from now`); |
| 66 | |
| 67 | assert.equal(relativeTime(new Date(24 * 60 * 60 * 1000)), |
| 68 | `a day from now`); |
| 69 | assert.equal(relativeTime(new Date(2 * 24 * 60 * 60 * 1000)), |
| 70 | `2 days from now`); |
| 71 | assert.equal(relativeTime(new Date(29 * 24 * 60 * 60 * 1000)), |
| 72 | `29 days from now`); |
| 73 | |
| 74 | assert.equal(relativeTime(new Date(30 * 24 * 60 * 60 * 1000)), |
| 75 | 'Jan 30, 1970'); |
| 76 | }); |
| 77 | |
| 78 | it('relativeTime past', () => { |
| 79 | const baseTime = 234234 * 1000; |
| 80 | |
| 81 | clock.tick(baseTime); |
| 82 | |
| 83 | assert.equal(relativeTime(new Date()), `just now`); |
| 84 | |
| 85 | assert.equal(relativeTime(new Date(baseTime - 59 * 1000)), |
| 86 | `just now`); |
| 87 | |
| 88 | assert.equal(relativeTime(new Date(baseTime - 60 * 1000)), |
| 89 | `a minute ago`); |
| 90 | assert.equal(relativeTime(new Date(baseTime - 2 * 60 * 1000)), |
| 91 | `2 minutes ago`); |
| 92 | assert.equal(relativeTime(new Date(baseTime - 59 * 60 * 1000)), |
| 93 | `59 minutes ago`); |
| 94 | |
| 95 | assert.equal(relativeTime(new Date(baseTime - 60 * 60 * 1000)), |
| 96 | `an hour ago`); |
| 97 | assert.equal(relativeTime(new Date(baseTime - 2 * 60 * 60 * 1000)), |
| 98 | `2 hours ago`); |
| 99 | assert.equal(relativeTime(new Date(baseTime - 23 * 60 * 60 * 1000)), |
| 100 | `23 hours ago`); |
| 101 | |
| 102 | assert.equal(relativeTime(new Date( |
| 103 | baseTime - 24 * 60 * 60 * 1000)), `a day ago`); |
| 104 | assert.equal(relativeTime(new Date( |
| 105 | baseTime - 2 * 24 * 60 * 60 * 1000)), `2 days ago`); |
| 106 | assert.equal(relativeTime(new Date( |
| 107 | baseTime - 29 * 24 * 60 * 60 * 1000)), `29 days ago`); |
| 108 | |
| 109 | assert.equal(relativeTime(new Date( |
| 110 | baseTime - 30 * 24 * 60 * 60 * 1000)), 'Dec 4, 1969'); |
| 111 | }); |
| 112 | }); |