Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 2ea4da1

Browse files
committed
fix(calendar, datepicker): fix MomentJS custom format support
- revert simplification of `$$mdDateUtil.removeLocalTzAndReparseDate()` - enable unit testing of MomentJS - add test suite for MomentJS custom formatting Relates to #12003. Relates to #11949.
1 parent a897a67 commit 2ea4da1

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

config/karma-docs.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function(config) {
1818
'node_modules/angular-messages/angular-messages.js',
1919
'node_modules/angular-route/angular-route.js',
2020
'node_modules/angular-mocks/angular-mocks.js',
21+
'node_modules/moment/moment.js',
2122
'dist/angular-material.js',
2223
'config/test-utils.js',
2324
'dist/docs/docs.js',

config/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = function(config) {
3838
'node_modules/angular-sanitize/angular-sanitize.js',
3939
'node_modules/angular-touch/angular-touch.js',
4040
'node_modules/angular-mocks/angular-mocks.js',
41+
'node_modules/moment/moment.js',
4142
'test/angular-material-mocks.js',
4243
'test/angular-material-spec.js'
4344
]);

src/components/datepicker/js/dateLocale.spec.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
describe('$mdDateLocale', function() {
32
var dateLocale, dateUtil;
43

@@ -81,7 +80,7 @@ describe('$mdDateLocale', function() {
8180

8281
describe('with custom values', function() {
8382
var fakeMonths = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'];
84-
var fakeshortMonths = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'j', 'l'];
83+
var fakeShortMonths = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'j', 'l'];
8584
var fakeDays = ['D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'];
8685
var fakeShortDays = ['1', '2', '3', '4', '5', '6', '7'];
8786
var fakeDates = [undefined, 'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11',
@@ -90,7 +89,7 @@ describe('$mdDateLocale', function() {
9089

9190
beforeEach(module(function($mdDateLocaleProvider) {
9291
$mdDateLocaleProvider.months = fakeMonths;
93-
$mdDateLocaleProvider.shortMonths = fakeshortMonths;
92+
$mdDateLocaleProvider.shortMonths = fakeShortMonths;
9493
$mdDateLocaleProvider.days = fakeDays;
9594
$mdDateLocaleProvider.shortDays = fakeShortDays;
9695
$mdDateLocaleProvider.dates = fakeDates;
@@ -113,7 +112,7 @@ describe('$mdDateLocale', function() {
113112

114113
it('should expose custom settings', function() {
115114
expect(dateLocale.months).toEqual(fakeMonths);
116-
expect(dateLocale.shortMonths).toEqual(fakeshortMonths);
115+
expect(dateLocale.shortMonths).toEqual(fakeShortMonths);
117116
expect(dateLocale.days).toEqual(fakeDays);
118117
expect(dateLocale.shortDays).toEqual(fakeShortDays);
119118
expect(dateLocale.dates).toEqual(fakeDates);
@@ -124,4 +123,39 @@ describe('$mdDateLocale', function() {
124123
expect(dateLocale.isDateComplete('Anything Else')).toBe(false);
125124
});
126125
});
126+
127+
describe('with MomentJS custom formatting', function() {
128+
129+
beforeEach(module(function($mdDateLocaleProvider) {
130+
$mdDateLocaleProvider.formatDate = function(date) {
131+
return date ? moment(date).format('M/D') : '';
132+
};
133+
$mdDateLocaleProvider.parseDate = function(dateString) {
134+
var m = moment(dateString, 'M/D', true);
135+
return m.isValid() ? m.toDate() : new Date(NaN);
136+
};
137+
$mdDateLocaleProvider.isDateComplete = function(dateString) {
138+
dateString = dateString.trim();
139+
// Look for two chunks of content (either numbers or text) separated by delimiters.
140+
var re = /^(([a-zA-Z]{3,}|[0-9]{1,4})([ .,]+|[/-]))([a-zA-Z]{3,}|[0-9]{1,4})/;
141+
return re.test(dateString);
142+
};
143+
}));
144+
145+
beforeEach(inject(function($mdDateLocale, $$mdDateUtil) {
146+
dateLocale = $mdDateLocale;
147+
dateUtil = $$mdDateUtil;
148+
}));
149+
150+
it('should respect custom formatting', function() {
151+
var now = new Date();
152+
expect(dateLocale.formatDate(new Date('2020-08-31T00:00:00-04:00'))).toEqual('8/31');
153+
expect(dateLocale.parseDate('8/31')).toEqual(new Date(now.getFullYear(), 7, 31));
154+
expect(dateLocale.parseDate('1/1')).toEqual(new Date(now.getFullYear(), 0, 1));
155+
expect(dateLocale.isDateComplete('8/31')).toBe(true);
156+
expect(dateLocale.isDateComplete('8-31')).toBe(true);
157+
expect(dateLocale.isDateComplete('August_31st')).toBe(false);
158+
expect(dateLocale.isDateComplete('2020')).toBe(false);
159+
});
160+
});
127161
});

src/components/datepicker/js/dateUtil.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,12 @@
314314
* @return {Date} date with local timezone offset removed
315315
*/
316316
function removeLocalTzAndReparseDate(value) {
317-
return $mdDateLocale.parseDate(value.getTime() + 60000 * value.getTimezoneOffset());
317+
var dateValue, formattedDate;
318+
// Remove the local timezone offset before calling formatDate.
319+
dateValue = new Date(value.getTime() + 60000 * value.getTimezoneOffset());
320+
formattedDate = $mdDateLocale.formatDate(dateValue);
321+
// parseDate only works with a date formatted by formatDate when using Moment validation.
322+
return $mdDateLocale.parseDate(formattedDate);
318323
}
319324
});
320325
})();

0 commit comments

Comments
 (0)