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
|
|
|
};
|
|
|
|
beforeEach(() =>{
|
|
|
|
realDateNow = Date.now.bind(global.Date);
|
|
|
|
global.Date.now = () => 0;
|
|
|
|
_mockFormat = Intl.RelativeTimeFormat = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => mockFormatExpression);
|
|
|
|
|
|
|
|
});
|
|
|
|
afterEach(()=>{
|
|
|
|
global.Date.now = realDateNow;
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test timestamp that is 2 minutes in the future', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-02-01 09:53:46 -05:00
|
|
|
test('test timestamp that is 15 seconds in the future displays 1', () => {
|
|
|
|
const fifteenSec = 15 * 1000;
|
|
|
|
mockFormatExpression.format.mockReturnValue('in 1 minute');
|
|
|
|
format.formatRelativeTime(fifteenSec, 'en');
|
|
|
|
expect(mockFormatExpression.format).toHaveBeenCalledWith(1, 'minute');
|
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('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
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('test timestamp that is 95.25 minutes in the future', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2020-11-12 14:21:23 -05:00
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('test timestamp that is 48 hours in the future', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('test timestamp that is 2.6 hours rounds to 3', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('test timestamp that is 4.2 hours in the future rounds to 4', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-11-30 16:16:52 -05:00
|
|
|
test('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
|
|
|
});
|
|
|
|
});
|