scratch-www/test/unit/lib/format-time.test.js

78 lines
3.1 KiB
JavaScript
Raw Normal View History

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()
};
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');
format.formatRelativeTime(twoMin, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'minute');
});
2020-11-30 16:16:52 -05:00
test('test rounding timestamp that is 4.4 minutes rounds to 4', () => {
const fourPlusMin = 4.4 * 60 * 1000;
2020-11-30 16:16:52 -05:00
mockFormatExpression.format.mockReturnValue('in 4 minutes');
format.formatRelativeTime(fourPlusMin, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'minute');
});
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');
format.formatRelativeTime(ninetyFiveMin, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(95, 'minute');
});
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-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');
format.formatRelativeTime(fortyEightHrs, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(48, 'hour');
});
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');
format.formatRelativeTime(twoPlusHours, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(3, 'hour');
});
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');
format.formatRelativeTime(fourPlusHours, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(4, 'hour');
});
2020-11-30 16:16:52 -05:00
test('test timestamp that is 2 hours in the future is in hours', () => {
const twoHours = 2 * 60 * 60 * 1000;
2020-11-30 16:16:52 -05:00
mockFormatExpression.format.mockReturnValue('in 2 hours');
format.formatRelativeTime(twoHours, 'en');
2020-11-30 16:16:52 -05:00
expect(mockFormatExpression.format).toHaveBeenCalledWith(2, 'hour');
});
});