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

Commit

Permalink
test(input): fix tests on Firefox v93+
Browse files Browse the repository at this point in the history
Since version 93, Firefox started more closely following the spec on
formatting `datetime-local` input values by removing trailing zeros from
the string representation of the value. This causes some of our tests to
fail ([example failure][1]).

For example, a value is reported by Firefox as `2009-01-06T16:25` while
the tests expect `2009-01-06T16:25:00.000`. I.e. Firefox started leaving
out seconds/milliseconds if they are zero.

According to [MDN][2], this is the correct behavior according to the
spec. Indeed the spec says that [if the value of the element is a valid
local date and time string, then it must be set to a **valid normalized
local date and time string**][3], where **valid normalized local date
and time string** is [defined as consisting of][4]:
> - A valid date string representing the date.
> - A U+0054 LATIN CAPITAL LETTER T character (T).
> - A valid time string representing the time, expressed as the
>   **shortest possible string** for the given time (e.g. **omitting the
>   seconds component** entirely if the given time is zero seconds past
>   the minute).

This commit fixes the relevant tests by explicitly specifying non-zero
values for seconds and milliseconds.

[1]: https://circleci.com/gh/angular/angular.js/3527
[2]: https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats
  #local_date_and_time_strings
[3]: https://html.spec.whatwg.org/multipage/input.html
  #local-date-and-time-state-(type=datetime-local)
[4]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html
  #concept-datetime-local

Co-authored-by: George Kalpakas <[email protected]>
  • Loading branch information
Ed Clement and gkalpak committed Dec 9, 2021
1 parent ed30c4d commit 6a52c4f
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,10 +1271,10 @@ describe('input', function() {
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="breakMe"/>');

$rootScope.$apply(function() {
$rootScope.breakMe = new Date(2009, 0, 6, 16, 25, 0);
$rootScope.breakMe = new Date(2009, 0, 6, 16, 25, 1, 337);
});

expect(inputElm.val()).toBe('2009-01-06T16:25:00.000');
expect(inputElm.val()).toBe('2009-01-06T16:25:01.337');

//set to text for browsers with datetime-local validation.
inputElm[0].setAttribute('type', 'text');
Expand Down Expand Up @@ -1324,32 +1324,32 @@ describe('input', function() {
it('should use UTC if specified in the options', function() {
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />');

helper.changeInputValueTo('2000-01-01T01:02');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 0));
helper.changeInputValueTo('2000-01-01T01:02:03.456');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 3, 456));

$rootScope.$apply(function() {
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 0));
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 3, 456));
});
expect(inputElm.val()).toBe('2001-01-01T01:02:00.000');
expect(inputElm.val()).toBe('2001-01-01T01:02:03.456');
});


it('should be possible to override the timezone', function() {
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />');

helper.changeInputValueTo('2000-01-01T01:02');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 0));
helper.changeInputValueTo('2000-01-01T01:02:03.456');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 3, 456));

inputElm.controller('ngModel').$overrideModelOptions({timezone: '+0500'});
$rootScope.$apply(function() {
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 0));
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 3, 456));
});
expect(inputElm.val()).toBe('2001-01-01T06:02:00.000');
expect(inputElm.val()).toBe('2001-01-01T06:02:03.456');

inputElm.controller('ngModel').$overrideModelOptions({timezone: 'UTC'});

helper.changeInputValueTo('2000-01-01T01:02');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 0));
helper.changeInputValueTo('2000-01-01T01:02:03.456');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 3, 456));
});


Expand All @@ -1360,13 +1360,13 @@ describe('input', function() {
var inputElm = helper.compileInput(
'<input type="datetime-local" ng-model="value" ng-model-options="' + ngModelOptions + '" />');

helper.changeInputValueTo('2000-01-01T06:02');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 0));
helper.changeInputValueTo('2000-01-01T06:02:03.456');
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 1, 2, 3, 456));

$rootScope.$apply(function() {
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 0));
$rootScope.value = new Date(Date.UTC(2001, 0, 1, 1, 2, 3, 456));
});
expect(inputElm.val()).toBe('2001-01-01T06:02:00.000');
expect(inputElm.val()).toBe('2001-01-01T06:02:03.456');
}
);

Expand Down Expand Up @@ -1401,13 +1401,13 @@ describe('input', function() {
it('should allow to specify the seconds', function() {
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value"" />');

helper.changeInputValueTo('2000-01-01T01:02:03');
expect(+$rootScope.value).toBe(+new Date(2000, 0, 1, 1, 2, 3));
helper.changeInputValueTo('2000-01-01T01:02:03.456');
expect(+$rootScope.value).toBe(+new Date(2000, 0, 1, 1, 2, 3, 456));

$rootScope.$apply(function() {
$rootScope.value = new Date(2001, 0, 1, 1, 2, 3);
$rootScope.value = new Date(2001, 0, 1, 1, 2, 3, 456);
});
expect(inputElm.val()).toBe('2001-01-01T01:02:03.000');
expect(inputElm.val()).toBe('2001-01-01T01:02:03.456');
});


Expand All @@ -1425,13 +1425,13 @@ describe('input', function() {
it('should allow four or more digits in year', function() {
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" />');

helper.changeInputValueTo('10123-01-01T01:02');
expect(+$rootScope.value).toBe(+new Date(10123, 0, 1, 1, 2, 0));
helper.changeInputValueTo('10123-01-01T01:02:03.456');
expect(+$rootScope.value).toBe(+new Date(10123, 0, 1, 1, 2, 3, 456));

$rootScope.$apply(function() {
$rootScope.value = new Date(20456, 1, 1, 1, 2, 0);
$rootScope.value = new Date(20456, 1, 1, 1, 2, 3, 456);
});
expect(inputElm.val()).toBe('20456-02-01T01:02:00.000');
expect(inputElm.val()).toBe('20456-02-01T01:02:03.456');
}
);
}
Expand Down

0 comments on commit 6a52c4f

Please sign in to comment.