2020-11-12 14:21:23 -05:00
|
|
|
const format = require('../../../src/lib/format-time');
|
|
|
|
|
|
|
|
describe('unit test lib/format-time.js', () => {
|
|
|
|
let realDateNow;
|
|
|
|
let _mockFormat;
|
|
|
|
const mockFormatExpression = {
|
2020-11-30 16:16:52 -05:00
|
|
|
format: jest.fn()
|
2020-11-12 14:21:23 -05:00
|
|
|
};
|
2023-10-24 14:22:28 -04:00
|
|
|
beforeEach(() => {
|
2020-11-12 14:21:23 -05:00
|
|
|
realDateNow = Date.now.bind(global.Date);
|
|
|
|
global.Date.now = () => 0;
|
|
|
|
_mockFormat = Intl.RelativeTimeFormat = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => mockFormatExpression);
|
|
|
|
|
|
|
|
});
|
2023-10-24 14:22:28 -04:00
|
|
|
afterEach(() => {
|
2020-11-12 14:21:23 -05:00
|
|
|
global.Date.now = realDateNow;
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 2 minutes in the future', () => {
|
2020-11-12 14:21:23 -05:00
|
|
|
const twoMin = 2 * 60 * 1000;
|
2020-11-30 16:16:52 -05:00
|
|
|
mockFormatExpression.format.mockReturnValue('in 2 minutes');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(twoMin, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'minute');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 15 seconds in the future displays 1', () => {
|
2021-02-01 09:53:46 -05:00
|
|
|
const fifteenSec = 15 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 1 minute');
|
|
|
|
format.formatRelativeTime(fifteenSec, 'en');
|
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(1, 'minute');
|
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('rounding timestamp that is 4.4 minutes rounds to 4', () => {
|
2020-12-07 15:38:58 -05:00
|
|
|
const fourPlusMin = 4.4 * 60 * 1000;
|
2020-11-30 16:16:52 -05:00
|
|
|
mockFormatExpression.format.mockReturnValue('in 4 minutes');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(fourPlusMin, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'minute');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 95.25 minutes in the future', () => {
|
2020-11-30 16:16:52 -05:00
|
|
|
const ninetyFiveMin = 95.25 * 60 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 95 minutes');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(ninetyFiveMin, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(95, 'minute');
|
2020-12-07 15:38:58 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 119 minutes in the future', () => {
|
2020-12-07 15:38:58 -05:00
|
|
|
const ninetyFiveMin = 119 * 60 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 199 minutes');
|
|
|
|
format.formatRelativeTime(ninetyFiveMin, 'en');
|
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(119, 'minute');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 48 hours in the future', () => {
|
2020-11-30 16:16:52 -05:00
|
|
|
const fortyEightHrs = 48 * 60 * 60 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 48 hours');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(fortyEightHrs, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(48, 'hour');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 2.6 hours rounds to 3', () => {
|
2020-11-30 16:16:52 -05:00
|
|
|
const twoPlusHours = 2.6 * 60 * 60 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 3 hours');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(twoPlusHours, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(3, 'hour');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 4.2 hours in the future rounds to 4', () => {
|
2020-11-30 16:16:52 -05:00
|
|
|
const fourPlusHours = 4.2 * 60 * 60 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 4 hours');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(fourPlusHours, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'hour');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2023-10-24 14:22:28 -04:00
|
|
|
test('timestamp that is 2 hours in the future is in hours', () => {
|
2020-12-07 15:38:58 -05:00
|
|
|
const twoHours = 2 * 60 * 60 * 1000;
|
2020-11-30 16:16:52 -05:00
|
|
|
mockFormatExpression.format.mockReturnValue('in 2 hours');
|
2020-12-07 15:38:58 -05:00
|
|
|
format.formatRelativeTime(twoHours, 'en');
|
2020-11-30 16:16:52 -05:00
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'hour');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
});
|