diff --git a/src/lib/format-time.js b/src/lib/format-time.js index 987077521..23255511a 100644 --- a/src/lib/format-time.js +++ b/src/lib/format-time.js @@ -35,7 +35,7 @@ const getTimeUnitAndDuration = timeStamp => { * @returns {string} A phrase representing the relative time in the future. e.g. 3 days 5 hours. */ module.exports.formatRelativeTime = (futureTime, lang) => { - const formatter = new Intl.RelativeTimeFormat(lang, { + const formatter = new Intl.RelativeTimeFormat([lang].concat(window.navigator.languages), { localeMatcher: 'best fit', numeric: 'always', style: 'long' diff --git a/test/unit/lib/format-time.test.js b/test/unit/lib/format-time.test.js index cd13ebc0a..b04513096 100644 --- a/test/unit/lib/format-time.test.js +++ b/test/unit/lib/format-time.test.js @@ -20,68 +20,58 @@ describe('unit test lib/format-time.js', () => { }); test('test timestamp that is 2 minutes in the future', () => { - let response; const twoMin = 2 * 60 * 1000; mockFormatExpression.format.mockReturnValue('in 2 minutes'); - response = format.formatRelativeTime(twoMin, 'en'); + format.formatRelativeTime(twoMin, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'minute'); - expect(response).toEqual('in 2 minutes'); }); test('test rounding timestamp that is 4.4 minutes rounds to 4', () => { - let response; - const twoMin = 4.4 * 60 * 1000; + const fourPlusMin = 4.4 * 60 * 1000; mockFormatExpression.format.mockReturnValue('in 4 minutes'); - response = format.formatRelativeTime(twoMin, 'en'); + format.formatRelativeTime(fourPlusMin, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'minute'); - expect(response).toEqual('in 4 minutes'); }); test('test timestamp that is 95.25 minutes in the future', () => { - let response; const ninetyFiveMin = 95.25 * 60 * 1000; mockFormatExpression.format.mockReturnValue('in 95 minutes'); - response = format.formatRelativeTime(ninetyFiveMin, 'en'); + format.formatRelativeTime(ninetyFiveMin, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(95, 'minute'); - expect(response).toEqual('in 95 minutes'); + }); + + test('test timestamp that is 119 minutes in the future', () => { + const ninetyFiveMin = 119 * 60 * 1000; + mockFormatExpression.format.mockReturnValue('in 199 minutes'); + format.formatRelativeTime(ninetyFiveMin, 'en'); + expect(mockFormatExpression.format).toHaveBeenCalledWith(119, 'minute'); }); test('test timestamp that is 48 hours in the future', () => { - let response; const fortyEightHrs = 48 * 60 * 60 * 1000; - mockFormatExpression.format.mockReturnValue('in 48 hours'); - response = format.formatRelativeTime(fortyEightHrs, 'en'); + format.formatRelativeTime(fortyEightHrs, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(48, 'hour'); - expect(response).toEqual('in 48 hours'); }); test('test timestamp that is 2.6 hours rounds to 3', () => { - let response; const twoPlusHours = 2.6 * 60 * 60 * 1000; - mockFormatExpression.format.mockReturnValue('in 3 hours'); - response = format.formatRelativeTime(twoPlusHours, 'en'); + format.formatRelativeTime(twoPlusHours, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(3, 'hour'); - expect(response).toEqual('in 3 hours'); }); test('test timestamp that is 4.2 hours in the future rounds to 4', () => { - let response; const fourPlusHours = 4.2 * 60 * 60 * 1000; mockFormatExpression.format.mockReturnValue('in 4 hours'); - response = format.formatRelativeTime(fourPlusHours, 'en'); + format.formatRelativeTime(fourPlusHours, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'hour'); - expect(response).toEqual('in 4 hours'); }); test('test timestamp that is 2 hours in the future is in hours', () => { - let response; - const threeHours = 2 * 60 * 60 * 1000; - + const twoHours = 2 * 60 * 60 * 1000; mockFormatExpression.format.mockReturnValue('in 2 hours'); - response = format.formatRelativeTime(threeHours, 'en'); + format.formatRelativeTime(twoHours, 'en'); expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'hour'); - expect(response).toEqual('in 2 hours'); }); });