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

Commit 7a9447c

Browse files
committed
fixup! simplify and improve test info
1 parent 8cf76e4 commit 7a9447c

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/ng/directive/input.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,10 +1443,11 @@ function createDateInputType(type, regexp, parseDate, format) {
14431443
// Note: We cannot read ctrl.$modelValue, as there might be a different
14441444
// parser/formatter in the processing chain so that the model
14451445
// contains some different data format!
1446-
if (timezone && previousTimezone && previousTimezone !== timezone) {
1447-
// If the timezone has changed, adjust the previousDate to the default timzeone
1446+
1447+
if (previousTimezone && previousTimezone !== timezone) {
1448+
// If the timezone has changed, adjust the previousDate to the default timezone
14481449
// so that the new date is converted with the correct timezone offset
1449-
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone, 0));
1450+
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone));
14501451
}
14511452

14521453
var parsedDate = parseDate(value, previousDate);

test/ng/directive/inputSpec.js

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

983-
helper.changeInputValueTo('2013-W03');
984-
expect(+$rootScope.value).toBe(Date.UTC(2013, 0, 17));
983+
// January 19 2013 is a Saturday
984+
$rootScope.$apply(function() {
985+
$rootScope.value = new Date(Date.UTC(2013, 0, 19));
986+
});
987+
988+
expect(inputElm.val()).toBe('2013-W03');
985989

986-
inputElm.controller('ngModel').$overrideModelOptions({timezone: '+5000'});
990+
inputElm.controller('ngModel').$overrideModelOptions({timezone: '+2400'});
987991

992+
// To check that the timezone overwrite works, apply an offset of +24 hours.
993+
// Since January 19 is a Saturday, +24 will turn the formatted Date into January 20 - Sunday -
994+
// which is in calendar week 4 instead of 3.
988995
$rootScope.$apply(function() {
989-
// the 17. with an offset of +5000 moves the date into next week
990-
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
996+
$rootScope.value = new Date(Date.UTC(2013, 0, 19));
991997
});
998+
999+
// Verifying that the displayed week is week 4 confirms that overriding the timezone worked
9921000
expect(inputElm.val()).toBe('2013-W04');
9931001
});
9941002

@@ -1997,30 +2005,34 @@ describe('input', function() {
19972005
dealoc(formElm);
19982006
});
19992007

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

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

20062015
// Change the timezone offset so that the display date is a day earlier
20072016
// This does not change the model, but our implementation
20082017
// internally caches a Date object with this offset
2009-
// and re-uses it if part of the date changes
2018+
// and re-uses it if part of the Date changes.
2019+
// See https://github.com/angular/angular.js/commit/1a1ef62903c8fdf4ceb81277d966a8eff67f0a96
20102020
inputElm.controller('ngModel').$overrideModelOptions({timezone: '-0500'});
20112021
$rootScope.$apply(function() {
20122022
$rootScope.value = new Date(Date.UTC(2000, 0, 1, 0));
20132023
});
20142024
expect(inputElm.val()).toBe('1999-12-31');
20152025

2016-
// Emptying the input should clear the cached date object
2017-
helper.changeInputValueTo('');
2018-
2026+
// At this point, the cached Date has its hours set to to 19 (00:00 - 05:00 = 19:00)
20192027
inputElm.controller('ngModel').$overrideModelOptions({timezone: 'UTC'});
2028+
2029+
// When changing the timezone back to UTC, the hours part of the Date should be set to
2030+
// the default 0 (UTC) and not use the modified value of the cached Date object.
20202031
helper.changeInputValueTo('2000-01-01');
20212032
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
20222033
});
20232034

2035+
20242036
describe('min', function() {
20252037

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

0 commit comments

Comments
 (0)