|
| 1 | +import { expect } from 'chai'; |
| 2 | +import 'mocha'; |
| 3 | + |
| 4 | +import { humanizeRelativeTime, humanizeUnit } from '../../src/pure/time'; |
| 5 | + |
| 6 | +describe('Time', () => { |
| 7 | + it('should return a humanized unit', () => { |
| 8 | + expect(humanizeUnit(undefined)).to.eq('Less than a minute'); |
| 9 | + expect(humanizeUnit(0)).to.eq('Less than a minute'); |
| 10 | + expect(humanizeUnit(-1)).to.eq('Less than a minute'); |
| 11 | + expect(humanizeUnit(1000 * 60 - 1)).to.eq('Less than a minute'); |
| 12 | + expect(humanizeUnit(1000 * 60)).to.eq('1 minute'); |
| 13 | + expect(humanizeUnit(1000 * 60 * 2 - 1)).to.eq('1 minute'); |
| 14 | + expect(humanizeUnit(1000 * 60 * 2)).to.eq('2 minutes'); |
| 15 | + expect(humanizeUnit(1000 * 60 * 60)).to.eq('1 hour'); |
| 16 | + expect(humanizeUnit(1000 * 60 * 60 * 2)).to.eq('2 hours'); |
| 17 | + expect(humanizeUnit(1000 * 60 * 60 * 24)).to.eq('1 day'); |
| 18 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 2)).to.eq('2 days'); |
| 19 | + |
| 20 | + // assume every month has 30 days |
| 21 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 30)).to.eq('1 month'); |
| 22 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 30 * 2)).to.eq('2 months'); |
| 23 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 30 * 12)).to.eq('12 months'); |
| 24 | + |
| 25 | + // assume every year has 365 days |
| 26 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 365)).to.eq('1 year'); |
| 27 | + expect(humanizeUnit(1000 * 60 * 60 * 24 * 365 * 2)).to.eq('2 years'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return a humanized duration positive', () => { |
| 31 | + expect(humanizeRelativeTime(undefined)).to.eq(''); |
| 32 | + expect(humanizeRelativeTime(0)).to.eq('this minute'); |
| 33 | + expect(humanizeRelativeTime(1)).to.eq('this minute'); |
| 34 | + expect(humanizeRelativeTime(1000 * 60 - 1)).to.eq('this minute'); |
| 35 | + expect(humanizeRelativeTime(1000 * 60)).to.eq('in 1 minute'); |
| 36 | + expect(humanizeRelativeTime(1000 * 60 * 2 - 1)).to.eq('in 1 minute'); |
| 37 | + expect(humanizeRelativeTime(1000 * 60 * 2)).to.eq('in 2 minutes'); |
| 38 | + expect(humanizeRelativeTime(1000 * 60 * 60)).to.eq('in 1 hour'); |
| 39 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 2)).to.eq('in 2 hours'); |
| 40 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24)).to.eq('tomorrow'); |
| 41 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 2)).to.eq('in 2 days'); |
| 42 | + |
| 43 | + // assume every month has 30 days |
| 44 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 30)).to.eq('next month'); |
| 45 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 30 * 2)).to.eq('in 2 months'); |
| 46 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 30 * 12)).to.eq('in 12 months'); |
| 47 | + |
| 48 | + // assume every year has 365 days |
| 49 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 365)).to.eq('next year'); |
| 50 | + expect(humanizeRelativeTime(1000 * 60 * 60 * 24 * 365 * 2)).to.eq('in 2 years'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return a humanized duration negative', () => { |
| 54 | + expect(humanizeRelativeTime(-1)).to.eq('1 minute ago'); |
| 55 | + expect(humanizeRelativeTime(-1000 * 60)).to.eq('1 minute ago'); |
| 56 | + expect(humanizeRelativeTime(-1000 * 60 - 1)).to.eq('2 minutes ago'); |
| 57 | + expect(humanizeRelativeTime(-1000 * 60 * 2)).to.eq('2 minutes ago'); |
| 58 | + expect(humanizeRelativeTime(-1000 * 60 * 2 - 1)).to.eq('3 minutes ago'); |
| 59 | + expect(humanizeRelativeTime(-1000 * 60 * 60)).to.eq('1 hour ago'); |
| 60 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 2)).to.eq('2 hours ago'); |
| 61 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24)).to.eq('yesterday'); |
| 62 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 2)).to.eq('2 days ago'); |
| 63 | + |
| 64 | + // assume every month has 30 days |
| 65 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 30)).to.eq('last month'); |
| 66 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 30 * 2)).to.eq('2 months ago'); |
| 67 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 30 * 12)).to.eq('12 months ago'); |
| 68 | + |
| 69 | + // assume every year has 365 days |
| 70 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 365)).to.eq('last year'); |
| 71 | + expect(humanizeRelativeTime(-1000 * 60 * 60 * 24 * 365 * 2)).to.eq('2 years ago'); |
| 72 | + }); |
| 73 | +}); |
0 commit comments