/html/tests/unit/navigation/navigation_helpers.js

https://github.com/RichardBronosky/jQueryMobileDemo · JavaScript · 113 lines · 91 code · 19 blank · 3 comment · 0 complexity · adf1dcdb8136b1d58a383ca8201fb993 MD5 · raw file

  1. /*
  2. * mobile navigation unit tests
  3. */
  4. (function($){
  5. module('jquery.mobile.navigation.js', {
  6. setup: function(){
  7. location.hash = "";
  8. }
  9. });
  10. test( "path.get method is working properly", function(){
  11. window.location.hash = "foo";
  12. same($.mobile.path.get(), "foo", "get method returns location.hash minus hash character");
  13. same($.mobile.path.get( "#foo/bar/baz.html" ), "foo/bar/", "get method with hash arg returns path with no filename or hash prefix");
  14. same($.mobile.path.get( "#foo/bar/baz.html/" ), "foo/bar/baz.html/", "last segment of hash is retained if followed by a trailing slash");
  15. });
  16. test( "path.isPath method is working properly", function(){
  17. ok(!$.mobile.path.isPath('bar'), "anything without a slash is not a path");
  18. ok($.mobile.path.isPath('bar/'), "anything with a slash is a path");
  19. ok($.mobile.path.isPath('/bar'), "anything with a slash is a path");
  20. ok($.mobile.path.isPath('a/r'), "anything with a slash is a path");
  21. ok($.mobile.path.isPath('/'), "anything with a slash is a path");
  22. });
  23. test( "path.getFilePath method is working properly", function(){
  24. same($.mobile.path.getFilePath("foo.html" + "&" + $.mobile.subPageUrlKey ), "foo.html", "returns path without sub page key");
  25. });
  26. test( "path.set method is working properly", function(){
  27. $.mobile.urlHistory.ignoreNextHashChange = false;
  28. $.mobile.path.set("foo");
  29. same("foo", window.location.hash.replace(/^#/,""), "sets location.hash properly");
  30. });
  31. test( "path.makeAbsolute is working properly", function(){
  32. $.mobile.urlHistory.ignoreNextHashChange = false;
  33. $.mobile.path.set("bar/");
  34. same( $.mobile.path.makeAbsolute("test.html"), "bar/test.html", "prefixes path with absolute base path from hash");
  35. $.mobile.path.set("bar");
  36. same( $.mobile.path.makeAbsolute("test.html"), "test.html", "returns the relative path unaltered for non path hash");
  37. $.mobile.path.set("bar/bing/bang");
  38. same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "bar/bing/bang?foo=bar&bak=baz", "appends query string paths to current path");
  39. $.mobile.path.set("");
  40. same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "/tests/unit/navigation/?foo=bar&bak=baz", "uses pathname for empty hash");
  41. $.mobile.path.set("bar");
  42. same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "/tests/unit/navigation/?foo=bar&bak=baz", "uses pathname for embedded pages");
  43. $.mobile.path.set("bar/bing?foo=bar");
  44. same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "bar/bing?foo=bar&bak=baz", "prevents addition of many sets of query params");
  45. });
  46. test( "path.clean is working properly", function(){
  47. var localroot = location.protocol + "//" + location.host + location.pathname,
  48. remoteroot = "http://google.com/",
  49. fakepath = "#foo/bar/baz.html",
  50. pathWithParam = localroot + "/bar?baz=" + localroot,
  51. localpath = localroot + fakepath,
  52. remotepath = remoteroot + fakepath;
  53. same( $.mobile.path.clean( localpath ), fakepath, "removes location protocol, host, port, pathname from same-domain path");
  54. same( $.mobile.path.clean( remotepath ), remotepath, "does nothing to an external domain path");
  55. same( $.mobile.path.clean( pathWithParam ), "/bar?baz=" + localroot, "doesn't remove params with localroot value");
  56. });
  57. test( "path.stripHash is working properly", function(){
  58. same( $.mobile.path.stripHash( "#bar" ), "bar", "returns a hash without the # prefix");
  59. });
  60. test( "path.hasProtocol is working properly", function(){
  61. same( $.mobile.path.hasProtocol( "tel:5559999" ), true, "value in tel protocol format has protocol" );
  62. same( $.mobile.path.hasProtocol( location.href ), true, "location href has protocol" );
  63. same( $.mobile.path.hasProtocol( "foo/bar/baz.html" ), false, "simple directory path has no protocol" );
  64. same( $.mobile.path.hasProtocol( "file://foo/bar/baz.html" ), true, "simple directory path with file:// has protocol" );
  65. });
  66. test( "path.isRelative is working properly", function(){
  67. same( $.mobile.path.isRelative("#foo/bar"), false, "path starting with a # is not relative" );
  68. same( $.mobile.path.isRelative("/foo/bar"), false, "path starting with a / is not relative" );
  69. same( $.mobile.path.isRelative("http://example.com/foo"), false, "full url path is not relative" );
  70. same( $.mobile.path.isRelative("foo/bar.html"), true, "simple path is relative" );
  71. });
  72. test( "path.isExternal is working properly", function(){
  73. same( $.mobile.path.isExternal( location.href ), false, "same domain is not external" );
  74. same( $.mobile.path.isExternal( "http://example.com" ), true, "example.com is external" );
  75. same($.mobile.path.isExternal("mailto:"), true, "mailto protocol");
  76. same($.mobile.path.isExternal("http://foo.com"), true, "http protocol");
  77. same($.mobile.path.isExternal("http://www.foo.com"), true, "http protocol with www");
  78. same($.mobile.path.isExternal("tel:16178675309"), true, "tel protocol");
  79. same($.mobile.path.isExternal("foo.html"), false, "filename");
  80. same($.mobile.path.isExternal("foo/foo/foo.html"), false, "file path");
  81. same($.mobile.path.isExternal("../../index.html"), false, "relative parent path");
  82. same($.mobile.path.isExternal("/foo"), false, "root-relative path");
  83. same($.mobile.path.isExternal("foo"), false, "simple string");
  84. same($.mobile.path.isExternal("#foo"), false, "local id reference");
  85. });
  86. test( "path.isQuery is working properly", function(){
  87. ok( $.mobile.path.isQuery( "?foo=bar" ), "string prefixed with ?");
  88. ok( !$.mobile.path.isQuery( "anything else" ), "string not prefixed with ?");
  89. });
  90. test( "path.cleanHash", function(){
  91. same( $.mobile.path.cleanHash( "#anything/atall?akjfdjjf" ), "anything/atall", "removes query param");
  92. same( $.mobile.path.cleanHash( "#nothing/atall" ), "nothing/atall", "removes query param");
  93. });
  94. })(jQuery);