inputdatepicker_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. goog.provide('goog.ui.InputDatePickerTest');
  15. goog.setTestOnly('goog.ui.InputDatePickerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.i18n.DateTimeFormat');
  18. goog.require('goog.i18n.DateTimeParse');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.ui.InputDatePicker');
  21. var dateTimeFormatter = new goog.i18n.DateTimeFormat('MM/dd/yyyy');
  22. var dateTimeParser = new goog.i18n.DateTimeParse('MM/dd/yyyy');
  23. var inputDatePicker;
  24. var popupDatePicker;
  25. function setUp() {}
  26. function tearDown() {
  27. if (inputDatePicker) {
  28. inputDatePicker.dispose();
  29. }
  30. if (popupDatePicker) {
  31. popupDatePicker.dispose();
  32. }
  33. goog.dom.removeChildren(goog.dom.getElement('renderElement'));
  34. goog.dom.removeChildren(goog.dom.getElement('popupParent'));
  35. }
  36. /**
  37. * Ensure that if setPopupParentElement is not called, that the
  38. * PopupDatePicker is parented to the body element.
  39. */
  40. function test_setPopupParentElementDefault() {
  41. setPopupParentElement_(null);
  42. assertEquals(
  43. 'PopupDatePicker should be parented to the body element', document.body,
  44. popupDatePicker.getElement().parentNode);
  45. }
  46. /**
  47. * Ensure that if setPopupParentElement is called, that the
  48. * PopupDatePicker is parented to the specified element.
  49. */
  50. function test_setPopupParentElement() {
  51. var popupParentElement = goog.dom.getElement('popupParent');
  52. setPopupParentElement_(popupParentElement);
  53. assertEquals(
  54. 'PopupDatePicker should be parented to the popupParent DIV',
  55. popupParentElement, popupDatePicker.getElement().parentNode);
  56. }
  57. /**
  58. * Creates a new InputDatePicker and calls setPopupParentElement with the
  59. * specified element, if provided. If el is null, then setPopupParentElement
  60. * is not called.
  61. * @param {Element} el If non-null, the argument to pass to
  62. * inputDatePicker.setPopupParentElement().
  63. * @private
  64. */
  65. function setPopupParentElement_(el) {
  66. inputDatePicker =
  67. new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser);
  68. if (el) {
  69. inputDatePicker.setPopupParentElement(el);
  70. }
  71. inputDatePicker.render(goog.dom.getElement('renderElement'));
  72. popupDatePicker = inputDatePicker.popupDatePicker_;
  73. }
  74. function test_ItParsesDataCorrectly() {
  75. inputDatePicker =
  76. new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser);
  77. inputDatePicker.render(goog.dom.getElement('renderElement'));
  78. inputDatePicker.createDom();
  79. inputDatePicker.setInputValue('8/9/2009');
  80. var parsedDate = inputDatePicker.getInputValueAsDate_();
  81. assertEquals(2009, parsedDate.getYear());
  82. assertEquals(7, parsedDate.getMonth()); // Months start from 0
  83. assertEquals(9, parsedDate.getDate());
  84. }
  85. function test_ItUpdatesItsValueOnPopupShown() {
  86. inputDatePicker =
  87. new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser);
  88. setPopupParentElement_(null);
  89. inputDatePicker.setInputValue('1/1/1');
  90. inputDatePicker.showForElement(document.body);
  91. var inputValue = inputDatePicker.getInputValue();
  92. assertEquals('01/01/0001', inputValue);
  93. }
  94. function test_ItDoesNotClearInputOnPopupShown() {
  95. // if popup does not have a date set, don't update input value
  96. inputDatePicker =
  97. new goog.ui.InputDatePicker(dateTimeFormatter, dateTimeParser);
  98. setPopupParentElement_(null);
  99. inputDatePicker.setInputValue('i_am_not_a_date');
  100. inputDatePicker.showForElement(document.body);
  101. var inputValue = inputDatePicker.getInputValue();
  102. assertEquals('i_am_not_a_date', inputValue);
  103. }