/tests/integration/navigation/method/method_core.js

https://github.com/agcolom/jquery-mobile · JavaScript · 227 lines · 179 code · 43 blank · 5 comment · 6 complexity · 474272aa4086a8b9562c479d074ee4d4 MD5 · raw file

  1. // Check is the ?push-state=false is in the url and alter the tests accordingly
  2. $.testHelper.setPushState();
  3. (function( $ ) {
  4. var url = $.mobile.path.parseLocation(),
  5. home = url.pathname + url.search;
  6. module( "navigate", {
  7. setup: function() {
  8. $.mobile.navigate.history.stack = [];
  9. $.mobile.navigate.history.activeIndex = 0;
  10. },
  11. teardown: function() {
  12. stop();
  13. $( window ).one( "navigate", function() {
  14. start();
  15. });
  16. if( location.hash !== "#reset" ) {
  17. $.mobile.navigate( "#reset" );
  18. } else {
  19. $.mobile.navigate( "#seriously-reset" );
  20. }
  21. }
  22. });
  23. test( "navigation changes the url", function() {
  24. ok( location.hash.indexOf( "foo" ) == -1, "the hash is clean" );
  25. $.mobile.navigate( "#foo" );
  26. equal( location.hash, "#foo", "the hash has been altered" );
  27. });
  28. if( $.support.pushState ) {
  29. test( "navigation should squish the hash", function() {
  30. var destination = home + "#foo";
  31. ok( location.hash.indexOf( "foo" ) == -1, "the hash is clean" );
  32. ok( $.mobile.path.isPath(destination), "the destination is a path" );
  33. $.mobile.navigate( destination );
  34. equal( $.mobile.path.parseLocation().pathname, url.pathname, "the resulting url has the same pathname as the original test url" );
  35. equal( location.hash, "#foo", "the hash has been altered" );
  36. });
  37. }
  38. // Test the inclusion of state for both pushstate and hashchange
  39. // --nav--> #foo {state} --nav--> #bar --back--> #foo {state} --foward--> #bar {state}
  40. asyncTest( "navigating backward and forward should include the history state", function() {
  41. $.testHelper.eventTarget = $( window );
  42. $.testHelper.eventSequence( "navigate", [
  43. function( timedOut, data ) {
  44. $.mobile.navigate( "#foo", { foo: "bar" });
  45. },
  46. function( timedOut, data ) {
  47. $.mobile.navigate( "#bar", { baz: "bak" });
  48. },
  49. function( timedOut, data ) {
  50. window.history.back();
  51. },
  52. function( timedOut, data ) {
  53. equal( data.state.foo, "bar", "the data that was appended in the navigation is popped with the backward movement" );
  54. equal( data.state.direction, "back", "the direction is recorded as backward" );
  55. window.history.forward();
  56. },
  57. function( timedOut, data ) {
  58. equal( data.state.baz, "bak", "the data that was appended in the navigation is popped with the foward movement" );
  59. equal( data.state.direction, "forward", "the direction is recorded as forward" );
  60. start();
  61. }
  62. ]);
  63. });
  64. // --nav--> #foo {state} --nav--> #bar --nav--> #foo {state} --back--> #bar --back--> #foo {state.direction = back}
  65. asyncTest( "navigation back to a duplicate history state should prefer back", function() {
  66. $.testHelper.eventTarget = $( window );
  67. $.testHelper.eventSequence( "navigate", [
  68. function() {
  69. $.mobile.navigate( "#foo" );
  70. },
  71. function() {
  72. $.mobile.navigate( "#bar" );
  73. },
  74. function() {
  75. $.mobile.navigate( "#baz" );
  76. },
  77. function() {
  78. equal( $.mobile.navigate.history.activeIndex, 2, "after n navigation events the active index is correct" );
  79. window.history.back();
  80. },
  81. function( timedOut, data ) {
  82. equal( $.mobile.navigate.history.activeIndex, 1, "after n navigation events, and a back, the active index is correct" );
  83. equal( data.state.direction, "back", "the direction should be back and not forward" );
  84. window.history.back();
  85. },
  86. function( timedOut, data ) {
  87. equal( $.mobile.navigate.history.stack.length, 3, "the history stack hasn't been truncated" );
  88. equal( $.mobile.navigate.history.activeIndex, 0, "the active history entry is the first" );
  89. equal( data.state.direction, "back", "the direction should be back and not forward" );
  90. start();
  91. }
  92. ]);
  93. });
  94. asyncTest( "Entries with identical URLs are distinguishable when pushState is enabled",
  95. function() {
  96. $.testHelper.eventSequence( "navigate", [
  97. function() {
  98. $.mobile.navigate( "#foo" );
  99. },
  100. function() {
  101. $.mobile.navigate( "#bar" );
  102. },
  103. function() {
  104. $.mobile.navigate( "#foo" );
  105. },
  106. function() {
  107. deepEqual( $.mobile.navigate.history.activeIndex, $.support.pushState ? 2 : 0,
  108. "After sequence start -> #foo -> #bar -> #foo activeIndex is correct" );
  109. window.history.back();
  110. },
  111. function() {
  112. deepEqual( $.mobile.navigate.history.activeIndex, 1,
  113. "After going back once in the sequence the activeIndex is correct" );
  114. window.history.forward();
  115. },
  116. function() {
  117. deepEqual( $.mobile.navigate.history.activeIndex, $.support.pushState ? 2 : 0,
  118. "After returning to the last sequnce entry the activeIndex is correct" );
  119. start();
  120. }
  121. ]);
  122. });
  123. asyncTest( "setting the hash with a url not in history should always create a new history entry", function() {
  124. $.testHelper.eventTarget = $( window );
  125. $.testHelper.eventSequence( "navigate", [
  126. function() {
  127. $.mobile.navigate( "#bar" );
  128. },
  129. function() {
  130. location.hash = "#foo";
  131. },
  132. function() {
  133. equal($.mobile.navigate.history.stack.length, 2, "there are two entries in the history stack" );
  134. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  135. start();
  136. }
  137. ]);
  138. });
  139. asyncTest( "setting the hash to the existing hash should not result in a new history entry", function() {
  140. $.testHelper.eventTarget = $( window );
  141. $.testHelper.eventSequence( "navigate", [
  142. function() {
  143. location.hash = "#foo";
  144. },
  145. function() {
  146. equal($.mobile.navigate.history.stack.length, 1, "there is one entry in the history stack" );
  147. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  148. location.hash = "#foo";
  149. },
  150. function( timedOut ) {
  151. equal($.mobile.navigate.history.stack.length, 1, "there is one entry in the history stack" );
  152. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  153. ok( timedOut, "there was no navigation event from setting the same hash" );
  154. start();
  155. }
  156. ]);
  157. });
  158. if( $.support.pushState ) {
  159. test( "squash is working properly", function() {
  160. var path = $.mobile.path, loc;
  161. $.mobile.navigate.navigator.squash( url.pathname + url.search + "#test-hash" );
  162. $.mobile.navigate.navigator.squash("#foo/bar");
  163. loc = path.parseLocation();
  164. equal( loc.pathname, url.directory + "foo/bar", "foo/bar has been squashed onto the url" );
  165. $.mobile.navigate.navigator.squash("bar/baz");
  166. loc = path.parseLocation();
  167. equal( loc.pathname, url.directory + "foo/bar/baz", "foo/bar has been squashed onto the url" );
  168. $.mobile.navigate.navigator.squash("#foo");
  169. loc = path.parseLocation();
  170. equal( loc.hash, "#foo", "foo is now the hash" );
  171. equal( loc.search, url.search, "the search is preserved" );
  172. // Make sure that the search delimiter is dictated by the squashed value
  173. $.mobile.navigate.navigator.squash("#foo/bar" + location.search.replace( "&", ";" ));
  174. loc = path.parseLocation();
  175. ok( loc.search.indexOf( "&" ) === -1, "the amp has been replaced" );
  176. $.mobile.navigate.navigator.squash( url.pathname + url.search );
  177. });
  178. test( "navigating with an absolute url matching the current url save for the hash should transplant the hash", function() {
  179. var loc = $.mobile.path.parseLocation();
  180. $.mobile.navigate( loc.hrefNoHash + loc.hash + "foo" );
  181. equal( location.hash, loc.hash + "foo", "the hash is set properly" );
  182. });
  183. }
  184. })( jQuery );