4 <script src=
"../../../resources/js-test.js"></script>
7 <p id=
"description"></p>
8 <div id=
"console"></div>
10 description('Tests for .valueAsNumber with <input type=week>.');
12 var input
= document
.createElement('input');
15 function valueAsNumberFor(stringValue
) {
16 input
.value
= stringValue
;
17 return input
.valueAsNumber
;
20 function setValueAsNumberAndGetValue(year
, month
, day
) {
21 var date
= new Date();
22 date
.setTime(Date
.UTC(year
, month
, day
));
24 date
.setUTCFullYear(year
);
25 input
.valueAsNumber
= date
.getTime();
29 shouldBe('valueAsNumberFor("")', 'Number.NaN');
30 // January 1st is Monday. W01 starts on January 1nd.
31 shouldBe('valueAsNumberFor("2007-W01")', 'Date.UTC(2007, 0, 1)');
32 // January 1st is Tuesday. W01 starts on December 31th.
33 shouldBe('valueAsNumberFor("2008-W01")', 'Date.UTC(2007, 11, 31)');
34 // January 1st is Wednesday. W01 starts on December 29th.
35 shouldBe('valueAsNumberFor("2003-W01")', 'Date.UTC(2002, 11, 30)');
36 // January 1st is Thursday. W01 starts on December 29th.
37 shouldBe('valueAsNumberFor("2004-W01")', 'Date.UTC(2003, 11, 29, 0, 0, 0, 0)');
38 // January 1st is Friday. W01 starts on January 4th.
39 shouldBe('valueAsNumberFor("2010-W01")', 'Date.UTC(2010, 0, 4)');
40 // January 1st is Saturday. W01 starts on January 3rd.
41 shouldBe('valueAsNumberFor("2005-W01")', 'Date.UTC(2005, 0, 3)');
42 // January 1st is Sunday. W01 starts on January 2nd.
43 shouldBe('valueAsNumberFor("2006-W01")', 'Date.UTC(2006, 0, 2)');
45 debug('Various January 1st:');
46 shouldBe('setValueAsNumberAndGetValue(2007, 0, 1)', '"2007-W01"');
47 shouldBe('setValueAsNumberAndGetValue(2008, 0, 1)', '"2008-W01"');
48 shouldBe('setValueAsNumberAndGetValue(2003, 0, 1)', '"2003-W01"');
49 shouldBe('setValueAsNumberAndGetValue(2004, 0, 1)', '"2004-W01"');
50 shouldBe('setValueAsNumberAndGetValue(2010, 0, 1)', '"2009-W53"');
51 shouldBe('setValueAsNumberAndGetValue(2005, 0, 1)', '"2004-W53"');
52 shouldBe('setValueAsNumberAndGetValue(2006, 0, 1)', '"2005-W52"');
54 debug('Normal cases:');
55 shouldBe('setValueAsNumberAndGetValue(2010, 0, 3)', '"2009-W53"');
56 shouldBe('setValueAsNumberAndGetValue(2010, 0, 4)', '"2010-W01"');
57 shouldBe('setValueAsNumberAndGetValue(2010, 0, 10)', '"2010-W01"');
58 shouldBe('setValueAsNumberAndGetValue(2010, 0, 11)', '"2010-W02"');
59 shouldBe('setValueAsNumberAndGetValue(2010, 0, 17)', '"2010-W02"');
60 shouldBe('setValueAsNumberAndGetValue(2010, 11, 31)', '"2010-W52"');
62 debug('Around the minimum/maximum values:');
63 shouldBe('setValueAsNumberAndGetValue(0, 11, 31)', '""');
64 shouldBe('setValueAsNumberAndGetValue(1, 0, 1)', '"0001-W01"');
65 shouldBe('setValueAsNumberAndGetValue(275760, 8, 8)', '"275760-W37"');
66 shouldBe('setValueAsNumberAndGetValue(275760, 8, 13)', '"275760-W37"');
68 debug('Tests to set invalid values to valueAsNumber:');
69 shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-W01"');
70 shouldBeNaN('input.valueAsNumber = "foo"; input.valueAsNumber');
71 shouldBeEqualToString('input.valueAsNumber = "foo"; input.value', '');
72 shouldBeNaN('input.valueAsNumber = NaN; input.valueAsNumber');
73 shouldBeEqualToString('input.valueAsNumber = NaN; input.value', '');
74 shouldBeNaN('input.valueAsNumber = Number.NaN; input.valueAsNumber');
75 shouldBeEqualToString('input.valueAsNumber = Number.NaN; input.value', '');
76 shouldThrow('input.valueAsNumber = Infinity', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
77 shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
78 shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
79 shouldBeNaN('input.valueAsNumber = Date.UTC(275760, 8, 14); input.valueAsNumber');
80 shouldBeEqualToString('input.valueAsNumber = Date.UTC(275760, 8, 14); input.value', '');