/tests/integration/navigation/navigation_base.js

https://github.com/gabrielschulhof/jquery-mobile · JavaScript · 268 lines · 180 code · 56 blank · 32 comment · 2 complexity · 33ec6327d0fc2456d2bb5caf7402a3de MD5 · raw file

  1. /*
  2. * Mobile navigation base tag unit tests
  3. */
  4. define( [
  5. "qunit",
  6. "jquery"
  7. ], function( QUnit, $ ) {
  8. var baseDir = $.mobile.path.parseUrl( $( "base" ).attr( "href" ) ).directory,
  9. contentDir = $.mobile.path.makePathAbsolute( "../content/", baseDir ),
  10. home = location.pathname + location.search;
  11. QUnit.module( "jquery.mobile.navigation.js - base tag", {
  12. beforeEach: function() {
  13. $.mobile.navigate.history.stack = [];
  14. $.mobile.navigate.history.activeIndex = 0;
  15. $.testHelper.navReset( home );
  16. }
  17. } );
  18. QUnit.test( "can navigate between internal and external pages", function( assert ) {
  19. var ready = assert.async();
  20. $.testHelper.pageSequence( [
  21. function() {
  22. // Navigate from default internal page to another internal page.
  23. $.testHelper.openPage( "#internal-page-2" );
  24. },
  25. function() {
  26. // Verify that we are on the 2nd internal page.
  27. $.testHelper.assertUrlLocation( assert, {
  28. push: home + "#internal-page-2",
  29. hash: "internal-page-2",
  30. report: "navigate to internal page"
  31. } );
  32. // Navigate to a page that is in the base directory. Note that the application
  33. // document and this new page are *NOT* in the same directory.
  34. $( "#internal-page-2 .bp1" ).click();
  35. },
  36. function() {
  37. // Verify that we are on the expected page.
  38. $.testHelper.assertUrlLocation( assert, {
  39. hashOrPush: baseDir + "base-page-1.html",
  40. report: "navigate from internal page to page in base directory"
  41. } );
  42. // Navigate to another page in the same directory as the current page.
  43. $( "#base-page-1 .bp2" ).click();
  44. },
  45. function() {
  46. // Verify that we are on the expected page.
  47. $.testHelper.assertUrlLocation( assert, {
  48. hashOrPush: baseDir + "base-page-2.html",
  49. report: "navigate from base directory page to another base directory page"
  50. } );
  51. // Navigate to another page in a directory that is the sibling of the base.
  52. $( "#base-page-2 .cp1" ).click();
  53. },
  54. function() {
  55. // Verify that we are on the expected page.
  56. $.testHelper.assertUrlLocation( assert, {
  57. hashOrPush: contentDir + "content-page-1.html",
  58. report: "navigate from base directory page to a page in a different directory " +
  59. "hierarchy"
  60. } );
  61. // Navigate to another page in a directory that is the sibling of the base.
  62. $( "#content-page-1 .cp2" ).click();
  63. },
  64. function() {
  65. // Verify that we are on the expected page.
  66. $.testHelper.assertUrlLocation( assert, {
  67. hashOrPush: contentDir + "content-page-2.html",
  68. report: "navigate to another page within the same non-base directory hierarchy"
  69. } );
  70. // Navigate to an internal page.
  71. $( "#content-page-2 .ip1" ).click();
  72. },
  73. function() {
  74. // Verify that we are on the expected page.
  75. // the hash based nav result (hash:) is dictate by the fact that #internal-page-1
  76. // is the original root page element
  77. $.testHelper.assertUrlLocation( assert, {
  78. hashOrPush: home,
  79. report: "navigate from a page in a non-base directory to an internal page"
  80. } );
  81. // Try calling change() directly with a relative path.
  82. $( ".ui-pagecontainer" ).pagecontainer( "change", "base-page-1.html" );
  83. },
  84. function() {
  85. // Verify that we are on the expected page.
  86. $.testHelper.assertUrlLocation( assert, {
  87. hashOrPush: baseDir + "base-page-1.html",
  88. report: "call changePage() with a filename (no path)"
  89. } );
  90. // Try calling changePage() directly with a relative path.
  91. $( ".ui-pagecontainer" ).pagecontainer( "change", "../content/content-page-1.html" );
  92. },
  93. function() {
  94. // Verify that we are on the expected page.
  95. $.testHelper.assertUrlLocation( assert, {
  96. hashOrPush: contentDir + "content-page-1.html",
  97. report: "call changePage() with a relative path containing up-level references"
  98. } );
  99. // Try calling changePage() with an id
  100. $( ".ui-pagecontainer" ).pagecontainer( "change", "content-page-2.html" );
  101. },
  102. function() {
  103. // Verify that we are on the expected page.
  104. $.testHelper.assertUrlLocation( assert, {
  105. hashOrPush: contentDir + "content-page-2.html",
  106. report: "call changePage() with a relative path should resolve relative to " +
  107. "current page"
  108. } );
  109. // Test that an internal page works
  110. $( "a.ip2" ).click();
  111. },
  112. function() {
  113. // Verify that we are on the expected page.
  114. $.testHelper.assertUrlLocation( assert, {
  115. hash: "internal-page-2",
  116. push: home + "#internal-page-2",
  117. report: "call changePage() with a page id"
  118. } );
  119. // Try calling change() with an id
  120. $( ".ui-pagecontainer" ).pagecontainer( "change", "internal-page-1" );
  121. },
  122. function() {
  123. // Verify that we are on the expected page.
  124. $.testHelper.assertUrlLocation( assert, {
  125. hash: "internal-page-2",
  126. push: home + "#internal-page-2",
  127. report: "calling change() with a page id that is not prefixed with '#' " +
  128. "should not change page"
  129. } );
  130. ready();
  131. }
  132. ] );
  133. } );
  134. QUnit.test( "internal form with no action submits to document URL", function( assert ) {
  135. var ready = assert.async();
  136. $.testHelper.pageSequence( [
  137. // Open our test page
  138. function() {
  139. $.testHelper.openPage( "#internal-no-action-form-page" );
  140. },
  141. function() {
  142. $( "#internal-no-action-form-page form" ).eq( 0 ).submit();
  143. },
  144. function() {
  145. $.testHelper.assertUrlLocation( assert, {
  146. hashOrPush: location.pathname + "?foo=1&bar=2",
  147. report: "hash should match document url and not base url"
  148. } );
  149. ready();
  150. }
  151. ] );
  152. } );
  153. QUnit.test( "external page form with no action submits to external page URL", function( assert ) {
  154. var ready = assert.async();
  155. $.testHelper.pageSequence( [
  156. function() {
  157. // Go to an external page that has a form.
  158. $( "#internal-page-1 .cp1" ).click();
  159. },
  160. function() {
  161. // Make sure we actually navigated to the external page.
  162. $.testHelper.assertUrlLocation( assert, {
  163. hashOrPush: contentDir + "content-page-1.html",
  164. report: "should be on content-page-1.html"
  165. } );
  166. // Now submit the form in the external page.
  167. $( "#content-page-1 form" ).eq( 0 ).submit();
  168. },
  169. function() {
  170. $.testHelper.assertUrlLocation( assert, {
  171. hashOrPush: contentDir + "content-page-1.html?foo=1&bar=2",
  172. report: "hash should match page url and not document url"
  173. } );
  174. ready();
  175. }
  176. ] );
  177. } );
  178. var testBaseTagAlteration = function( assert, assertions ) {
  179. var ready = assert.async();
  180. $.testHelper.pageSequence( [
  181. function() {
  182. $( ".ui-pagecontainer" ).pagecontainer( "change", "../../base-change.html" );
  183. },
  184. function() {
  185. assertions();
  186. window.history.back();
  187. },
  188. function() {
  189. ready();
  190. }
  191. ] );
  192. };
  193. QUnit.test( "disabling base tag changes should prevent base href value changes", function( assert ) {
  194. var baseHref = $( "base" ).attr( "href" );
  195. $.mobile.dynamicBaseEnabled = false;
  196. testBaseTagAlteration( assert, function() {
  197. if ( $.support.dynamicBaseTag ) {
  198. assert.equal( baseHref, $( "base" ).attr( "href" ), "the base href value should be unchanged" );
  199. } else {
  200. assert.equal( $.mobile.activePage.find( "#base-change-link" ).attr( "href" ), "foo", "the link href's remain unchanged" );
  201. }
  202. } );
  203. } );
  204. QUnit.test( "enabling base tag changes should enable base href value changes", function( assert ) {
  205. var baseHref = $( "base" ).attr( "href" );
  206. $.mobile.dynamicBaseEnabled = true;
  207. $.support.dynamicBaseTag = true;
  208. testBaseTagAlteration( assert, function() {
  209. assert.ok( baseHref !== $( "base" ).attr( "href" ), "the base href value should be changed" );
  210. } );
  211. } );
  212. } );