/tests/unit/navigation/method/method_core.js

https://github.com/forresst/jquery-mobile-fr_FR · JavaScript · 197 lines · 150 code · 42 blank · 5 comment · 6 complexity · 7a6abe8a2d56e47dccb0b2db47f99dd9 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( "setting the hash with a url not in history should always create a new history entry", function() {
  95. $.testHelper.eventTarget = $( window );
  96. $.testHelper.eventSequence( "navigate", [
  97. function() {
  98. $.mobile.navigate( "#bar" );
  99. },
  100. function() {
  101. location.hash = "#foo";
  102. },
  103. function() {
  104. equal($.mobile.navigate.history.stack.length, 2, "there are two entries in the history stack" );
  105. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  106. start();
  107. }
  108. ]);
  109. });
  110. asyncTest( "setting the hash to the existing hash should not result in a new history entry", function() {
  111. $.testHelper.eventTarget = $( window );
  112. $.testHelper.eventSequence( "navigate", [
  113. function() {
  114. location.hash = "#foo";
  115. },
  116. function() {
  117. equal($.mobile.navigate.history.stack.length, 1, "there is one entry in the history stack" );
  118. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  119. location.hash = "#foo";
  120. },
  121. function( timedOut ) {
  122. equal($.mobile.navigate.history.stack.length, 1, "there is one entry in the history stack" );
  123. equal($.mobile.navigate.history.getActive().hash, "#foo", "the url for the active history entry matches the hash" );
  124. ok( timedOut, "there was no navigation event from setting the same hash" );
  125. start();
  126. }
  127. ]);
  128. });
  129. if( $.support.pushState ) {
  130. test( "squash is working properly", function() {
  131. var path = $.mobile.path, loc;
  132. $.mobile.navigate.navigator.squash( url.pathname + url.search + "#test-hash" );
  133. $.mobile.navigate.navigator.squash("#foo/bar");
  134. loc = path.parseLocation();
  135. equal( loc.pathname, url.directory + "foo/bar", "foo/bar has been squashed onto the url" );
  136. $.mobile.navigate.navigator.squash("bar/baz");
  137. loc = path.parseLocation();
  138. equal( loc.pathname, url.directory + "foo/bar/baz", "foo/bar has been squashed onto the url" );
  139. $.mobile.navigate.navigator.squash("#foo");
  140. loc = path.parseLocation();
  141. equal( loc.hash, "#foo", "foo is now the hash" );
  142. equal( loc.search, url.search, "the search is preserved" );
  143. // Make sure that the search delimiter is dictated by the squashed value
  144. $.mobile.navigate.navigator.squash("#foo/bar" + location.search.replace( "&", ";" ));
  145. loc = path.parseLocation();
  146. ok( loc.search.indexOf( "&" ) === -1, "the amp has been replaced" );
  147. $.mobile.navigate.navigator.squash( url.pathname + url.search );
  148. });
  149. test( "navigating with an absolute url matching the current url save for the hash should transplant the hash", function() {
  150. var loc = $.mobile.path.parseLocation();
  151. $.mobile.navigate( loc.hrefNoHash + loc.hash + "foo" );
  152. equal( location.hash, loc.hash + "foo", "the hash is set properly" );
  153. });
  154. }
  155. })( jQuery );