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