|
1 | 1 | suite('time-ago', function() { |
| 2 | + let dateNow |
| 3 | + |
| 4 | + function freezeTime(date) { |
| 5 | + dateNow = Date.now |
| 6 | + Date.now = function() { |
| 7 | + return date |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + teardown(function() { |
| 12 | + if (dateNow) { |
| 13 | + Date.now = dateNow |
| 14 | + } |
| 15 | + }) |
| 16 | + |
2 | 17 | test('always uses relative dates', function() { |
3 | 18 | const now = new Date(Date.now() - 10 * 365 * 24 * 60 * 60 * 1000).toISOString() |
4 | 19 | const time = document.createElement('time-ago') |
@@ -44,11 +59,20 @@ suite('time-ago', function() { |
44 | 59 | assert.equal(time.textContent, '3 months ago') |
45 | 60 | }) |
46 | 61 |
|
47 | | - test('rewrites from now past datetime to years ago', function() { |
48 | | - const now = new Date(Date.now() - 12 * 30 * 24 * 60 * 60 * 1000).toISOString() |
49 | | - const time = document.createElement('time-ago') |
50 | | - time.setAttribute('datetime', now) |
51 | | - assert.equal(time.textContent, 'last year') |
| 62 | + test('rewrites time-ago datetimes < 18months as "months ago"', function() { |
| 63 | + freezeTime(new Date(2020, 0, 1)) |
| 64 | + const then = new Date(2018, 10, 1).toISOString() |
| 65 | + const timeElement = document.createElement('time-ago') |
| 66 | + timeElement.setAttribute('datetime', then) |
| 67 | + assert.equal(timeElement.textContent, '15 months ago') |
| 68 | + }) |
| 69 | + |
| 70 | + test('rewrites time-ago datetimes >= 18 months as "years ago"', function() { |
| 71 | + freezeTime(new Date(2020, 0, 1)) |
| 72 | + const then = new Date(2018, 6, 1).toISOString() |
| 73 | + const timeElement = document.createElement('time-ago') |
| 74 | + timeElement.setAttribute('datetime', then) |
| 75 | + assert.equal(timeElement.textContent, '2 years ago') |
52 | 76 | }) |
53 | 77 |
|
54 | 78 | test('micro formats years', function() { |
|
0 commit comments