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

Commit 6e618ef

Browse files
committed
fixup! simplify and improve test info
1 parent 9ef6897 commit 6e618ef

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/ng/directive/input.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,14 +1535,15 @@ function createDateInputType(type, regexp, parseDate, format) {
15351535
}
15361536

15371537
function parseDateAndConvertTimeZoneToLocal(value, previousDate) {
1538-
if (timezone && previousTimezone && previousTimezone !== timezone) {
1539-
// If the timezone has changed, adjust the previousDate to the default timzeone
1538+
var timezone = ctrl.$options.getOption('timezone');
1539+
1540+
if (previousTimezone && previousTimezone !== timezone) {
1541+
// If the timezone has changed, adjust the previousDate to the default timezone
15401542
// so that the new date is converted with the correct timezone offset
1541-
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone, 0));
1543+
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone));
15421544
}
15431545

15441546
var parsedDate = parseDate(value, previousDate);
1545-
var timezone = ctrl.$options.getOption('timezone');
15461547

15471548
if (!isNaN(parsedDate) && timezone) {
15481549
parsedDate = convertTimezoneToLocal(parsedDate, timezone);

test/ng/directive/inputSpec.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,15 +1022,23 @@ describe('input', function() {
10221022
it('should be possible to override the timezone', function() {
10231023
var inputElm = helper.compileInput('<input type="week" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />');
10241024

1025-
helper.changeInputValueTo('2013-W03');
1026-
expect(+$rootScope.value).toBe(Date.UTC(2013, 0, 17));
1025+
// January 19 2013 is a Saturday
1026+
$rootScope.$apply(function() {
1027+
$rootScope.value = new Date(Date.UTC(2013, 0, 19));
1028+
});
1029+
1030+
expect(inputElm.val()).toBe('2013-W03');
10271031

1028-
inputElm.controller('ngModel').$overrideModelOptions({timezone: '+5000'});
1032+
inputElm.controller('ngModel').$overrideModelOptions({timezone: '+2400'});
10291033

1034+
// To check that the timezone overwrite works, apply an offset of +24 hours.
1035+
// Since January 19 is a Saturday, +24 will turn the formatted Date into January 20 - Sunday -
1036+
// which is in calendar week 4 instead of 3.
10301037
$rootScope.$apply(function() {
1031-
// the 17. with an offset of +5000 moves the date into next week
1032-
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
1038+
$rootScope.value = new Date(Date.UTC(2013, 0, 19));
10331039
});
1040+
1041+
// Verifying that the displayed week is week 4 confirms that overriding the timezone worked
10341042
expect(inputElm.val()).toBe('2013-W04');
10351043
});
10361044

@@ -2092,30 +2100,34 @@ describe('input', function() {
20922100
dealoc(formElm);
20932101
});
20942102

2095-
it('should not reuse the hour part of a previous date object after changing the timezone', function() {
2103+
it('should not reuse the hours part of a previous date object after changing the timezone', function() {
20962104
var inputElm = helper.compileInput('<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />');
20972105

20982106
helper.changeInputValueTo('2000-01-01');
2107+
// The Date parser sets the hours part of the Date to 0 (00:00) (UTC)
20992108
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
21002109

21012110
// Change the timezone offset so that the display date is a day earlier
21022111
// This does not change the model, but our implementation
21032112
// internally caches a Date object with this offset
2104-
// and re-uses it if part of the date changes
2113+
// and re-uses it if part of the Date changes.
2114+
// See https://github.com/angular/angular.js/commit/1a1ef62903c8fdf4ceb81277d966a8eff67f0a96
21052115
inputElm.controller('ngModel').$overrideModelOptions({timezone: '-0500'});
21062116
$rootScope.$apply(function() {
21072117
$rootScope.value = new Date(Date.UTC(2000, 0, 1, 0));
21082118
});
21092119
expect(inputElm.val()).toBe('1999-12-31');
21102120

2111-
// Emptying the input should clear the cached date object
2112-
helper.changeInputValueTo('');
2113-
2121+
// At this point, the cached Date has its hours set to to 19 (00:00 - 05:00 = 19:00)
21142122
inputElm.controller('ngModel').$overrideModelOptions({timezone: 'UTC'});
2123+
2124+
// When changing the timezone back to UTC, the hours part of the Date should be set to
2125+
// the default 0 (UTC) and not use the modified value of the cached Date object.
21152126
helper.changeInputValueTo('2000-01-01');
21162127
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
21172128
});
21182129

2130+
21192131
describe('min', function() {
21202132

21212133
it('should invalidate', function() {

0 commit comments

Comments
 (0)