| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {setCurrentLanguage} from '../i18n'; |
| b69ab31 | | | 9 | import {relativeDate} from '../relativeDate'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | describe('relativeDate', () => { |
| b69ab31 | | | 12 | const SECOND = 1000; |
| b69ab31 | | | 13 | const MINUTE = 60 * SECOND; |
| b69ab31 | | | 14 | const HOUR = 60 * MINUTE; |
| b69ab31 | | | 15 | const DAY = 24 * HOUR; |
| b69ab31 | | | 16 | const WEEK = 7 * DAY; |
| b69ab31 | | | 17 | const YEAR = DAY * 365; |
| b69ab31 | | | 18 | const MONTH = YEAR / 12; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | const reference = 157765000000; // 01.01.1975 00:00 |
| b69ab31 | | | 21 | const now = new Date().getTime(); |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | const check = (time: number) => expect(relativeDate(time, {reference})); |
| b69ab31 | | | 24 | const checkShort = (time: number) => |
| b69ab31 | | | 25 | expect(relativeDate(time, {reference, useShortVariant: true})); |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | describe('en', () => { |
| b69ab31 | | | 28 | beforeAll(() => { |
| b69ab31 | | | 29 | setCurrentLanguage('en'); |
| b69ab31 | | | 30 | }); |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | it('renders relative dates', () => { |
| b69ab31 | | | 33 | expect(relativeDate(new Date(), {})).toEqual('just now'); |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | // test long format |
| b69ab31 | | | 36 | expect(relativeDate(0, {})).toEqual(Math.round(now / YEAR) + ' years ago'); |
| b69ab31 | | | 37 | check(reference - 41 * SECOND).toEqual('just now'); |
| b69ab31 | | | 38 | check(reference - 42 * SECOND).toEqual('a minute ago'); |
| b69ab31 | | | 39 | check(reference - MINUTE).toEqual('a minute ago'); |
| b69ab31 | | | 40 | check(reference - MINUTE * 1.5).toEqual('2 minutes ago'); |
| b69ab31 | | | 41 | check(reference - MINUTE * 59).toEqual('59 minutes ago'); |
| b69ab31 | | | 42 | check(reference - HOUR).toEqual('an hour ago'); |
| b69ab31 | | | 43 | check(reference - HOUR * 1.5).toEqual('2 hours ago'); |
| b69ab31 | | | 44 | check(reference - HOUR * 16).toEqual('16 hours ago'); |
| b69ab31 | | | 45 | check(reference - HOUR * 23).toEqual('23 hours ago'); |
| b69ab31 | | | 46 | check(reference - DAY * 1.8).toEqual('yesterday'); |
| b69ab31 | | | 47 | check(reference - DAY * 3).toEqual('3 days ago'); |
| b69ab31 | | | 48 | check(reference - DAY * 6).toEqual('6 days ago'); |
| b69ab31 | | | 49 | check(reference - WEEK).toEqual('a week ago'); |
| b69ab31 | | | 50 | check(reference - WEEK * 2).toEqual('2 weeks ago'); |
| b69ab31 | | | 51 | check(reference - WEEK * 4).toEqual('4 weeks ago'); |
| b69ab31 | | | 52 | check(reference - MONTH * 1.2).toEqual('a month ago'); |
| b69ab31 | | | 53 | check(reference - YEAR + HOUR).toEqual('12 months ago'); |
| b69ab31 | | | 54 | check(reference - YEAR).toEqual('a year ago'); |
| b69ab31 | | | 55 | check(reference - YEAR * 2).toEqual('2 years ago'); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | it('renders short relative dates', () => { |
| b69ab31 | | | 59 | // test short format |
| b69ab31 | | | 60 | checkShort(reference - 41 * SECOND).toEqual('now'); |
| b69ab31 | | | 61 | checkShort(reference - 42 * SECOND).toEqual('1m'); |
| b69ab31 | | | 62 | checkShort(reference - MINUTE).toEqual('1m'); |
| b69ab31 | | | 63 | checkShort(reference - MINUTE * 1.5).toEqual('2m'); |
| b69ab31 | | | 64 | checkShort(reference - MINUTE * 59).toEqual('59m'); |
| b69ab31 | | | 65 | checkShort(reference - HOUR).toEqual('1h'); |
| b69ab31 | | | 66 | checkShort(reference - HOUR * 1.5).toEqual('2h'); |
| b69ab31 | | | 67 | checkShort(reference - HOUR * 16).toEqual('16h'); |
| b69ab31 | | | 68 | checkShort(reference - HOUR * 23).toEqual('23h'); |
| b69ab31 | | | 69 | checkShort(reference - DAY * 1.8).toEqual('1d'); |
| b69ab31 | | | 70 | checkShort(reference - DAY * 3).toEqual('3d'); |
| b69ab31 | | | 71 | checkShort(reference - DAY * 6).toEqual('6d'); |
| b69ab31 | | | 72 | checkShort(reference - WEEK).toEqual('1w'); |
| b69ab31 | | | 73 | checkShort(reference - WEEK * 2).toEqual('2w'); |
| b69ab31 | | | 74 | checkShort(reference - WEEK * 4).toEqual('4w'); |
| b69ab31 | | | 75 | checkShort(reference - MONTH * 1.2).toEqual('1mo'); |
| b69ab31 | | | 76 | checkShort(reference - YEAR + HOUR).toEqual('12mo'); |
| b69ab31 | | | 77 | checkShort(reference - YEAR).toEqual('1y'); |
| b69ab31 | | | 78 | checkShort(reference - YEAR * 2).toEqual('2y'); |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | }); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | describe('de', () => { |
| b69ab31 | | | 83 | beforeAll(() => { |
| b69ab31 | | | 84 | setCurrentLanguage('de'); |
| b69ab31 | | | 85 | }); |
| b69ab31 | | | 86 | it('renders relative dates', () => { |
| b69ab31 | | | 87 | check(reference - 41 * SECOND).toEqual('vor 1 Min.'); |
| b69ab31 | | | 88 | check(reference - 42 * SECOND).toEqual('vor 1 Min.'); |
| b69ab31 | | | 89 | check(reference - MINUTE).toEqual('vor 1 Min.'); |
| b69ab31 | | | 90 | check(reference - MINUTE * 1.5).toEqual('vor 2 Min.'); |
| b69ab31 | | | 91 | check(reference - MINUTE * 59).toEqual('vor 59 Min.'); |
| b69ab31 | | | 92 | check(reference - HOUR).toEqual('vor 60 Min.'); |
| b69ab31 | | | 93 | check(reference - HOUR * 1.5).toEqual('vor 2 Std.'); |
| b69ab31 | | | 94 | check(reference - HOUR * 16).toEqual('vor 16 Std.'); |
| b69ab31 | | | 95 | check(reference - HOUR * 23).toEqual('vor 23 Std.'); |
| b69ab31 | | | 96 | check(reference - DAY * 1.8).toEqual('vorgestern'); |
| b69ab31 | | | 97 | check(reference - DAY * 3).toEqual('vor 3 Tagen'); |
| b69ab31 | | | 98 | check(reference - DAY * 6).toEqual('vor 6 Tagen'); |
| b69ab31 | | | 99 | check(reference - WEEK).toEqual('vor 7 Tagen'); |
| b69ab31 | | | 100 | check(reference - WEEK * 2).toEqual('vor 14 Tagen'); |
| b69ab31 | | | 101 | check(reference - WEEK * 4).toEqual('vor 28 Tagen'); |
| b69ab31 | | | 102 | check(reference - MONTH * 1.2).toEqual('letzten Monat'); |
| b69ab31 | | | 103 | // check(reference - YEAR + HOUR).toEqual('vor 12 Monaten'); // some kind of whitespace issue |
| b69ab31 | | | 104 | // check(reference - YEAR).toEqual('vor 12 Monaten'); // some kind of whitespace issue |
| b69ab31 | | | 105 | check(reference - YEAR * 2).toEqual('vor 2 Jahren'); |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | check(reference + 2 * HOUR).toEqual('in 2 Std.'); |
| b69ab31 | | | 108 | check(reference + MINUTE).toEqual('in 1 Min.'); |
| b69ab31 | | | 109 | }); |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | }); |