import { getNow } from './datetime' import { relativeDate } from './index' describe('formatting - relativeDate', () => { it.each([ ['en-US', 'now'], ['en-GB', 'now'], ['en-CA', 'now'], ['es-ES', 'ahora'], ['es-MX', 'ahora'], ['fr-FR', 'maintenant'], ['ru-RU', 'сейчас'], ['pt-BR', 'agora'], ['de-DE', 'jetzt'], ['it-IT', 'ora'], ['hi-IN', 'अब'], ['zh-CN', '现在'], ['ja-JP', '今'], // short locales ['en', 'now'], ['es', 'ahora'], ['fr', 'maintenant'], ['ru', 'сейчас'], ['pt', 'agora'], ['de', 'jetzt'], ['it', 'ora'], ['hi', 'अब'], ['zh', '现在'], ['ja', '今'], ])( 'returns a relative date, `now`: locale=%s, relative date=%s', (locale, expected) => { const now = getNow() const dateString = now.toISO() const result = relativeDate(dateString, { now, locale }) expect(result).toEqual(expected) } ) it.each([ ['en-US', '1 day ago'], ['en-GB', '1 day ago'], ['en-CA', '1 day ago'], ['es-ES', 'hace 1 día'], ['es-MX', 'hace 1 día'], ['fr-FR', 'il y a 1 jour'], ['ru-RU', '1 день назад'], ['pt-BR', 'há 1 dia'], ['de-DE', 'vor 1 Tag'], ['it-IT', '1 giorno fa'], ['hi-IN', '1 दिन पहले'], ['zh-CN', '1天前'], ['ja-JP', '1 日前'], // short locales ['en', '1 day ago'], ['es', 'hace 1 día'], ['fr', 'il y a 1 jour'], ['ru', '1 день назад'], ['pt', 'há 1 dia'], ['de', 'vor 1 Tag'], ['it', '1 giorno fa'], ['hi', '1 दिन पहले'], ['zh', '1天前'], ['ja', '1 日前'], ])( 'returns a relative date, 1 day ago: locale=%s, relative date=%s', (locale, expected) => { const now = getNow() const dateString = now.minus({ days: 1, minute: 1 }).toISO() const result = relativeDate(dateString, { now, locale }) expect(result).toEqual(expected) } ) it.each([ ['en-US', 'in 1 day'], ['en-GB', 'in 1 day'], ['en-CA', 'in 1 day'], ['es-ES', 'dentro de 1 día'], ['es-MX', 'dentro de 1 día'], ['fr-FR', 'dans 1 jour'], ['ru-RU', 'через 1 день'], ['pt-BR', 'em 1 dia'], ['de-DE', 'in 1 Tag'], ['it-IT', 'tra 1 giorno'], ['hi-IN', '1 दिन में'], ['zh-CN', '1天后'], ['ja-JP', '1 日後'], // short locales ['en', 'in 1 day'], ['es', 'dentro de 1 día'], ['fr', 'dans 1 jour'], ['ru', 'через 1 день'], ['pt', 'em 1 dia'], ['de', 'in 1 Tag'], ['it', 'tra 1 giorno'], ['hi', '1 दिन में'], ['zh', '1天后'], ['ja', '1 日後'], ])( 'returns a relative date, in 1 day: locale=%s, relative date=%s', (locale, expected) => { const now = getNow() const dateString = now.plus({ days: 1, minute: 1 }).toISO() const result = relativeDate(dateString, { now, locale }) expect(result).toEqual(expected) } ) it.each([ // present ['en-US', getNow().plus({ seconds: 1 }).toISO(), 'now'], ['en-US', getNow().plus({ seconds: 10 }).toISO(), 'now'], // future ['en-US', getNow().plus({ seconds: 11 }).toISO(), 'in 11 seconds'], ['en-US', getNow().plus({ minute: 1 }).toISO(), 'in 1 minute'], ['en-US', getNow().plus({ minutes: 2 }).toISO(), 'in 2 minutes'], ['en-US', getNow().plus({ hour: 1 }).toISO(), 'in 1 hour'], ['en-US', getNow().plus({ hours: 2 }).toISO(), 'in 2 hours'], ['en-US', getNow().plus({ days: 1 }).toISO(), 'in 1 day'], ['en-US', getNow().plus({ days: 1, hours: 11, minutes: 59, seconds: 59 }).toISO(), 'in 1 day'], // prettier-ignore ['en-US', getNow().plus({ days: 1, hours: 12, minute: 1, second: 1 }).toISO(), 'in 2 days'], // prettier-ignore ['en-US', getNow().plus({ days: 2 }).toISO(), 'in 2 days'], ['en-US', getNow().plus({ days: 7 }).toISO(), 'in 1 week'], ['en-US', getNow().plus({ weeks: 1 }).toISO(), 'in 1 week'], ['en-US', getNow().plus({ days: 8 }).toISO(), 'in 1 week'], ['en-US', getNow().plus({ days: 14 }).toISO(), 'in 2 weeks'], ['en-US', getNow().plus({ weeks: 2 }).toISO(), 'in 2 weeks'], ['en-US', getNow().plus({ months: 1 }).toISO(), 'in 1 month'], ['en-US', getNow().plus({ months: 1, days: 1 }).toISO(), 'in 1 month'], ['en-US', getNow().plus({ months: 2 }).toISO(), 'in 2 months'], ['en-US', getNow().plus({ months: 2, days: 1 }).toISO(), 'in 2 months'], ['en-US', getNow().plus({ year: 1 }).toISO(), 'in 1 year'], ['en-US', getNow().plus({ years: 1, days: 1 }).toISO(), 'in 1 year'], ['en-US', getNow().plus({ year: 2 }).toISO(), 'in 2 years'], ['en-US', getNow().plus({ years: 2, days: 1 }).toISO(), 'in 2 years'], // past ['en-US', getNow().minus({ seconds: 11 }).toISO(), '12 seconds ago'], // padding causes this to round up ['en-US', getNow().minus({ minute: 1 }).toISO(), '1 minute ago'], ['en-US', getNow().minus({ minutes: 2 }).toISO(), '2 minutes ago'], ['en-US', getNow().minus({ hour: 1 }).toISO(), '1 hour ago'], ['en-US', getNow().minus({ hours: 2 }).toISO(), '2 hours ago'], ['en-US', getNow().minus({ days: 1 }).toISO(), '1 day ago'], ['en-US', getNow().minus({ days: 1, hours: 11, minutes: 59, seconds: 59 }).toISO(), '1 day ago'], // prettier-ignore ['en-US', getNow().minus({ days: 1, hours: 12, minute: 1, second: 1 }).toISO(), '2 days ago'], // prettier-ignore ['en-US', getNow().minus({ days: 2 }).toISO(), '2 days ago'], ['en-US', getNow().minus({ days: 7 }).toISO(), '1 week ago'], ['en-US', getNow().minus({ weeks: 1 }).toISO(), '1 week ago'], ['en-US', getNow().minus({ days: 8 }).toISO(), '1 week ago'], ['en-US', getNow().minus({ days: 14 }).toISO(), '2 weeks ago'], ['en-US', getNow().minus({ weeks: 2 }).toISO(), '2 weeks ago'], ['en-US', getNow().minus({ months: 1 }).toISO(), '1 month ago'], ['en-US', getNow().minus({ months: 1, days: 1 }).toISO(), '1 month ago'], ['en-US', getNow().minus({ months: 2 }).toISO(), '2 months ago'], ['en-US', getNow().minus({ months: 2, days: 1 }).toISO(), '2 months ago'], ['en-US', getNow().minus({ year: 1 }).toISO(), '1 year ago'], ['en-US', getNow().minus({ years: 1, days: 1 }).toISO(), '1 year ago'], ['en-US', getNow().minus({ year: 2 }).toISO(), '2 years ago'], ['en-US', getNow().minus({ years: 2, days: 1 }).toISO(), '2 years ago'], ])('returns a relative date, various units', (locale, dateString, expected) => expect(relativeDate(dateString, { now: getNow(), locale })).toEqual( expected ) ) })