2019-06-18 20:28:31 +00:00
|
|
|
import * as DateUtils from 'src/services/date_utils/date_utils.js'
|
|
|
|
|
|
|
|
describe('DateUtils', () => {
|
|
|
|
describe('relativeTime', () => {
|
|
|
|
it('returns now with low enough amount of seconds', () => {
|
|
|
|
const futureTime = Date.now() + 20 * DateUtils.SECOND
|
|
|
|
const pastTime = Date.now() - 20 * DateUtils.SECOND
|
|
|
|
expect(DateUtils.relativeTime(futureTime, 30)).to.eql({ num: 0, key: 'time.now' })
|
|
|
|
expect(DateUtils.relativeTime(pastTime, 30)).to.eql({ num: 0, key: 'time.now' })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('rounds down for past', () => {
|
|
|
|
const time = Date.now() - 1.8 * DateUtils.HOUR
|
2022-06-08 00:34:08 +00:00
|
|
|
expect(DateUtils.relativeTime(time)).to.eql({ num: 1, key: 'time.unit.hours' })
|
2019-06-18 20:28:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('rounds up for future', () => {
|
|
|
|
const time = Date.now() + 1.8 * DateUtils.HOUR
|
2022-06-08 00:34:08 +00:00
|
|
|
expect(DateUtils.relativeTime(time)).to.eql({ num: 2, key: 'time.unit.hours' })
|
2019-06-18 20:28:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('uses plural when necessary', () => {
|
|
|
|
const time = Date.now() - 3.8 * DateUtils.WEEK
|
2022-06-08 00:34:08 +00:00
|
|
|
expect(DateUtils.relativeTime(time)).to.eql({ num: 3, key: 'time.unit.weeks' })
|
2019-06-18 20:28:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('works with date string', () => {
|
|
|
|
const time = Date.now() - 4 * DateUtils.MONTH
|
|
|
|
const dateString = new Date(time).toISOString()
|
2022-06-08 00:34:08 +00:00
|
|
|
expect(DateUtils.relativeTime(dateString)).to.eql({ num: 4, key: 'time.unit.months' })
|
2019-06-18 20:28:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('relativeTimeShort', () => {
|
|
|
|
it('returns the short version of the same relative time', () => {
|
|
|
|
const time = Date.now() + 2 * DateUtils.YEAR
|
2022-06-08 00:34:08 +00:00
|
|
|
expect(DateUtils.relativeTimeShort(time)).to.eql({ num: 2, key: 'time.unit.years_short' })
|
2019-06-18 20:28:31 +00:00
|
|
|
})
|
|
|
|
})
|
2019-07-05 07:02:14 +00:00
|
|
|
})
|