| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.InputDatePickerTest'); |
| 16 |
goog.setTestOnly('goog.ui.InputDatePickerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.i18n.DateTimeFormat'); |
| 20 |
goog.require('goog.i18n.DateTimeParse'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.ui.InputDatePicker'); |
| 23 |
|
| 24 |
var dateTimeFormatter = new goog.i18n.DateTimeFormat('MM/dd/yyyy'); |
| 25 |
var dateTimeParser = new goog.i18n.DateTimeParse('MM/dd/yyyy'); |
| 26 |
|
| 27 |
var inputDatePicker; |
| 28 |
var popupDatePicker; |
| 29 |
|
| 30 |
function setUp() {} |
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
if (inputDatePicker) { |
| 34 |
inputDatePicker.dispose(); |
| 35 |
} |
| 36 |
if (popupDatePicker) { |
| 37 |
popupDatePicker.dispose(); |
| 38 |
} |
| 39 |
goog.dom.removeChildren(goog.dom.getElement('renderElement')); |
| 40 |
goog.dom.removeChildren(goog.dom.getElement('popupParent')); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Ensure that if setPopupParentElement is not called, that the |
| 46 |
* PopupDatePicker is parented to the body element. |
| 47 |
*/ |
| 48 |
function test_setPopupParentElementDefault() { |
| 49 |
setPopupParentElement_(null); |
| 50 |
assertEquals( |
| 51 |
'PopupDatePicker should be parented to the body element', document.body, |
| 52 |
popupDatePicker.getElement().parentNode); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Ensure that if setPopupParentElement is called, that the |
| 58 |
* PopupDatePicker is parented to the specified element. |
| 59 |
*/ |
| 60 |
function test_setPopupParentElement() { |
| 61 |
var popupParentElement = goog.dom.getElement('popupParent'); |
| 62 |
setPopupParentElement_(popupParentElement); |
| 63 |
assertEquals( |
| 64 |
'PopupDatePicker should be parented to the popupParent DIV', |
| 65 |
popupParentElement, popupDatePicker.getElement().parentNode); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Creates a new InputDatePicker and calls setPopupParentElement with the |
| 71 |
* specified element, if provided. If el is null, then setPopupParentElement |
| 72 |
* is not called. |
| 73 |
* @param {Element} el If non-null, the argument to pass to |
| 74 |
* inputDatePicker.setPopupParentElement(). |
| 75 |
* @private |
| 76 |
*/ |
| 77 |
function setPopupParentElement_(el) { |
| 78 |
inputDatePicker = |
| 79 |
new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser); |
| 80 |
|
| 81 |
if (el) { |
| 82 |
inputDatePicker.setPopupParentElement(el); |
| 83 |
} |
| 84 |
|
| 85 |
inputDatePicker.render(goog.dom.getElement('renderElement')); |
| 86 |
popupDatePicker = inputDatePicker.popupDatePicker_; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
function test_ItParsesDataCorrectly() { |
| 91 |
inputDatePicker = |
| 92 |
new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser); |
| 93 |
inputDatePicker.render(goog.dom.getElement('renderElement')); |
| 94 |
|
| 95 |
inputDatePicker.createDom(); |
| 96 |
inputDatePicker.setInputValue('8/9/2009'); |
| 97 |
|
| 98 |
var parsedDate = inputDatePicker.getInputValueAsDate_(); |
| 99 |
assertEquals(2009, parsedDate.getYear()); |
| 100 |
assertEquals(7, parsedDate.getMonth()); // Months start from 0 |
| 101 |
assertEquals(9, parsedDate.getDate()); |
| 102 |
} |
| 103 |
|
| 104 |
function test_ItUpdatesItsValueOnPopupShown() { |
| 105 |
inputDatePicker = |
| 106 |
new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser); |
| 107 |
|
| 108 |
setPopupParentElement_(null); |
| 109 |
inputDatePicker.setInputValue('1/1/1'); |
| 110 |
inputDatePicker.showForElement(document.body); |
| 111 |
var inputValue = inputDatePicker.getInputValue(); |
| 112 |
assertEquals('01/01/0001', inputValue); |
| 113 |
} |
| 114 |
|
| 115 |
function test_ItDoesNotClearInputOnPopupShown() { |
| 116 |
// if popup does not have a date set, don't update input value |
| 117 |
inputDatePicker = |
| 118 |
new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser); |
| 119 |
|
| 120 |
setPopupParentElement_(null); |
| 121 |
inputDatePicker.setInputValue('i_am_not_a_date'); |
| 122 |
inputDatePicker.showForElement(document.body); |
| 123 |
var inputValue = inputDatePicker.getInputValue(); |
| 124 |
assertEquals('i_am_not_a_date', inputValue); |
| 125 |
} |