Skip to content

Commit f3cf031

Browse files
committed
Merge branch 'fix-format-date-safari-ff' into 'master'
Throw an error when formatDate's input is invalid See merge request gitlab-org/gitlab-ce!28713
2 parents ff7766b + dade5a4 commit f3cf031

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

app/assets/javascripts/lib/utils/datetime_utility.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ export const getDayName = date =>
7979
* @param {date} datetime
8080
* @returns {String}
8181
*/
82-
export const formatDate = datetime => dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
82+
export const formatDate = datetime => {
83+
if (_.isString(datetime) && datetime.match(/\d+-\d+\d+ /)) {
84+
throw new Error('Invalid date');
85+
}
86+
return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
87+
};
8388

8489
/**
8590
* Timeago uses underscores instead of dashes to separate language from country code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Throw an error when formatDate's input is invalid
3+
merge_request: 28713
4+
author:
5+
type: fixed

spec/javascripts/lib/utils/datetime_utility_spec.js renamed to spec/frontend/lib/utils/datetime_utility_spec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ describe('Date time utils', () => {
6565
});
6666
});
6767

68+
describe('formatDate', () => {
69+
it('should format date properly', () => {
70+
const formattedDate = datetimeUtility.formatDate(new Date('07/23/2016'));
71+
72+
expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
73+
});
74+
75+
it('should format ISO date properly', () => {
76+
const formattedDate = datetimeUtility.formatDate('2016-07-23T00:00:00.559Z');
77+
78+
expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
79+
});
80+
81+
it('should throw an error if date is invalid', () => {
82+
expect(() => {
83+
datetimeUtility.formatDate('2016-07-23 00:00:00 UTC');
84+
}).toThrow(new Error('Invalid date'));
85+
});
86+
});
87+
6888
describe('get day difference', () => {
6989
it('should return 7', () => {
7090
const firstDay = new Date('07/01/2016');
@@ -380,7 +400,7 @@ describe('prettyTime methods', () => {
380400

381401
describe('calculateRemainingMilliseconds', () => {
382402
beforeEach(() => {
383-
spyOn(Date, 'now').and.callFake(() => new Date('2063-04-04T00:42:00Z').getTime());
403+
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
384404
});
385405

386406
it('calculates the remaining time for a given end date', () => {

0 commit comments

Comments
 (0)