formpost_test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2009 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.FormPostTest');
  15. goog.setTestOnly('goog.ui.FormPostTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.object');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.ui.FormPost');
  22. goog.require('goog.userAgent.product');
  23. goog.require('goog.userAgent.product.isVersion');
  24. var TARGET = 'target';
  25. var ACTION_URL = 'http://url/';
  26. var formPost;
  27. var parameters;
  28. var submits;
  29. var originalCreateDom = goog.ui.FormPost.prototype.createDom;
  30. function isChrome7or8() {
  31. // Temporarily disabled in Chrome 7 & 8. See b/3176768
  32. if (goog.userAgent.product.CHROME &&
  33. goog.userAgent.product.isVersion('7.0') &&
  34. !goog.userAgent.product.isVersion('8.0')) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. function setUp() {
  40. formPost = new goog.ui.FormPost();
  41. submits = 0;
  42. // Replace the form's submit method with a fake.
  43. goog.ui.FormPost.prototype.createDom = function() {
  44. originalCreateDom.apply(this, arguments);
  45. this.getElement().submit = function() { submits++ };
  46. };
  47. parameters = {'foo': 'bar', 'baz': 1, 'array': [0, 'yes']};
  48. }
  49. function tearDown() {
  50. formPost.dispose();
  51. goog.ui.FormPost.prototype.createDom = originalCreateDom;
  52. }
  53. function testPost() {
  54. formPost.post(parameters, ACTION_URL, TARGET);
  55. expectUrlAndParameters_(ACTION_URL, TARGET, parameters);
  56. }
  57. function testPostWithDefaults() {
  58. // Temporarily disabled in Chrome 7. See See b/3176768
  59. if (isChrome7or8) {
  60. return;
  61. }
  62. formPost = new goog.ui.FormPost();
  63. formPost.post(parameters);
  64. expectUrlAndParameters_('', '', parameters);
  65. }
  66. function expectUrlAndParameters_(url, target, parameters) {
  67. var form = formPost.getElement();
  68. assertEquals(
  69. 'element must be a form', String(goog.dom.TagName.FORM), form.tagName);
  70. assertEquals('form must be hidden', 'none', form.style.display);
  71. assertEquals('form method must be POST', 'POST', form.method.toUpperCase());
  72. assertEquals('submits', 1, submits);
  73. assertEquals('action attribute', url, form.action);
  74. assertEquals('target attribute', target, form.target);
  75. var inputs =
  76. goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.INPUT, null, form);
  77. var formValues = {};
  78. for (var i = 0, input = inputs[i]; input = inputs[i]; i++) {
  79. if (goog.isArray(formValues[input.name])) {
  80. formValues[input.name].push(input.value);
  81. } else if (input.name in formValues) {
  82. formValues[input.name] = [formValues[input.name], input.value];
  83. } else {
  84. formValues[input.name] = input.value;
  85. }
  86. }
  87. var expected = goog.object.map(parameters, valueToString);
  88. assertObjectEquals('form values must match', expected, formValues);
  89. }
  90. function valueToString(value) {
  91. if (goog.isArray(value)) {
  92. return goog.array.map(value, valueToString);
  93. }
  94. return String(value);
  95. }