summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Gascou-Vaillancourt <paul.gascvail@gmail.com>2019-05-24 10:25:08 -0400
committerPaul Gascou-Vaillancourt <paul.gascvail@gmail.com>2019-05-24 13:38:30 -0400
commit3bbf37f93ab6e297965705c5353699dce0647885 (patch)
treeb9ab3bd547035300a13ae70c943eeb5b6eb5adee
parentd6df4fb20a4d6f622865c2c70e17caeba70c1027 (diff)
downloadgitlab-ce-3bbf37f93ab6e297965705c5353699dce0647885.tar.gz
Throw an error when formatDate's input is invalidfix-format-date-safari-ff
-rw-r--r--app/assets/javascripts/lib/utils/datetime_utility.js7
-rw-r--r--changelogs/unreleased/fix-format-date-safari-ff.yml5
-rw-r--r--spec/frontend/lib/utils/datetime_utility_spec.js (renamed from spec/javascripts/lib/utils/datetime_utility_spec.js)22
3 files changed, 32 insertions, 2 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js
index 624878cb5d7..32cafb74d91 100644
--- a/app/assets/javascripts/lib/utils/datetime_utility.js
+++ b/app/assets/javascripts/lib/utils/datetime_utility.js
@@ -79,7 +79,12 @@ export const getDayName = date =>
* @param {date} datetime
* @returns {String}
*/
-export const formatDate = datetime => dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
+export const formatDate = datetime => {
+ if (_.isString(datetime) && datetime.match(/\d+-\d+\d+ /)) {
+ throw new Error('Invalid date');
+ }
+ return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
+};
/**
* Timeago uses underscores instead of dashes to separate language from country code.
diff --git a/changelogs/unreleased/fix-format-date-safari-ff.yml b/changelogs/unreleased/fix-format-date-safari-ff.yml
new file mode 100644
index 00000000000..e71ea2867f3
--- /dev/null
+++ b/changelogs/unreleased/fix-format-date-safari-ff.yml
@@ -0,0 +1,5 @@
+---
+title: Throw an error when formatDate's input is invalid
+merge_request: 28713
+author:
+type: fixed
diff --git a/spec/javascripts/lib/utils/datetime_utility_spec.js b/spec/frontend/lib/utils/datetime_utility_spec.js
index 5327ec9d2a0..9f49e68cfe8 100644
--- a/spec/javascripts/lib/utils/datetime_utility_spec.js
+++ b/spec/frontend/lib/utils/datetime_utility_spec.js
@@ -65,6 +65,26 @@ describe('Date time utils', () => {
});
});
+ describe('formatDate', () => {
+ it('should format date properly', () => {
+ const formattedDate = datetimeUtility.formatDate(new Date('07/23/2016'));
+
+ expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
+ });
+
+ it('should format ISO date properly', () => {
+ const formattedDate = datetimeUtility.formatDate('2016-07-23T00:00:00.559Z');
+
+ expect(formattedDate).toBe('Jul 23, 2016 12:00am GMT+0000');
+ });
+
+ it('should throw an error if date is invalid', () => {
+ expect(() => {
+ datetimeUtility.formatDate('2016-07-23 00:00:00 UTC');
+ }).toThrow(new Error('Invalid date'));
+ });
+ });
+
describe('get day difference', () => {
it('should return 7', () => {
const firstDay = new Date('07/01/2016');
@@ -380,7 +400,7 @@ describe('prettyTime methods', () => {
describe('calculateRemainingMilliseconds', () => {
beforeEach(() => {
- spyOn(Date, 'now').and.callFake(() => new Date('2063-04-04T00:42:00Z').getTime());
+ jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
});
it('calculates the remaining time for a given end date', () => {