/public/jquerypp/dom/route/route_test.js

https://github.com/jupiterjs/javascriptmvc-site · JavaScript · 267 lines · 207 code · 53 blank · 7 comment · 0 complexity · ece8faded9fdce802af47ef5dde8b258 MD5 · raw file

  1. steal('funcunit/qunit').then('./route.js',function(){
  2. module("jquery/dom/route")
  3. test("deparam", function(){
  4. $.route.routes = {};
  5. $.route(":page",{
  6. page: "index"
  7. })
  8. var obj = $.route.deparam("jQuery.Controller");
  9. same(obj, {
  10. page : "jQuery.Controller",
  11. route: ":page"
  12. });
  13. obj = $.route.deparam("");
  14. same(obj, {
  15. page : "index",
  16. route: ":page"
  17. });
  18. obj = $.route.deparam("jQuery.Controller&where=there");
  19. same(obj, {
  20. page : "jQuery.Controller",
  21. where: "there",
  22. route: ":page"
  23. });
  24. $.route.routes = {};
  25. $.route(":page/:index",{
  26. page: "index",
  27. index: "foo"
  28. });
  29. obj = $.route.deparam("jQuery.Controller/&where=there");
  30. same(obj, {
  31. page : "jQuery.Controller",
  32. index: "foo",
  33. where: "there",
  34. route: ":page/:index"
  35. });
  36. })
  37. test("deparam of invalid url", function(){
  38. $.route.routes = {};
  39. $.route("pages/:var1/:var2/:var3", {
  40. var1: 'default1',
  41. var2: 'default2',
  42. var3: 'default3'
  43. });
  44. // This path does not match the above route, and since the hash is not
  45. // a &key=value list there should not be data.
  46. obj = $.route.deparam("pages//");
  47. same(obj, {});
  48. // A valid path with invalid parameters should return the path data but
  49. // ignore the parameters.
  50. obj = $.route.deparam("pages/val1/val2/val3&invalid-parameters");
  51. same(obj, {
  52. var1: 'val1',
  53. var2: 'val2',
  54. var3: 'val3',
  55. route: "pages/:var1/:var2/:var3"
  56. });
  57. })
  58. test("deparam of url with non-generated hash (manual override)", function(){
  59. $.route.routes = {};
  60. // This won't be set like this by route, but it could easily happen via a
  61. // user manually changing the URL or when porting a prior URL structure.
  62. obj = $.route.deparam("page=foo&bar=baz&where=there");
  63. same(obj, {
  64. page: 'foo',
  65. bar: 'baz',
  66. where: 'there'
  67. });
  68. })
  69. test("param", function(){
  70. $.route.routes = {};
  71. $.route("pages/:page",{
  72. page: "index"
  73. })
  74. var res = $.route.param({page: "foo"});
  75. equals(res, "pages/foo")
  76. res = $.route.param({page: "foo", index: "bar"});
  77. equals(res, "pages/foo&index=bar")
  78. $.route("pages/:page/:foo",{
  79. page: "index",
  80. foo: "bar"
  81. })
  82. res = $.route.param({page: "foo", foo: "bar", where: "there"});
  83. equals(res, "pages/foo/&where=there")
  84. // There is no matching route so the hash should be empty.
  85. res = $.route.param({});
  86. equals(res, "")
  87. $.route.routes = {};
  88. res = $.route.param({page: "foo", bar: "baz", where: "there"});
  89. equals(res, "&page=foo&bar=baz&where=there")
  90. res = $.route.param({});
  91. equals(res, "")
  92. });
  93. test("symmetry", function(){
  94. $.route.routes = {};
  95. var obj = {page: "=&[]", nestedArray : ["a"], nested : {a :"b"} }
  96. var res = $.route.param(obj)
  97. var o2 = $.route.deparam(res)
  98. same(o2, obj)
  99. })
  100. test("light param", function(){
  101. $.route.routes = {};
  102. $.route(":page",{
  103. page: "index"
  104. })
  105. var res = $.route.param({page: "index"});
  106. equals(res, "")
  107. $.route("pages/:p1/:p2/:p3",{
  108. p1: "index",
  109. p2: "foo",
  110. p3: "bar"
  111. })
  112. res = $.route.param({p1: "index", p2: "foo", p3: "bar"});
  113. equals(res, "pages///")
  114. res = $.route.param({p1: "index", p2: "baz", p3: "bar"});
  115. equals(res, "pages//baz/")
  116. });
  117. test('param doesnt add defaults to params', function(){
  118. $.route.routes = {};
  119. $.route("pages/:p1",{
  120. p2: "foo"
  121. })
  122. var res = $.route.param({p1: "index", p2: "foo"});
  123. equals(res, "pages/index")
  124. })
  125. test("param-deparam", function(){
  126. $.route(":page/:type",{
  127. page: "index",
  128. type: "foo"
  129. })
  130. var data = {page: "jQuery.Controller",
  131. type: "document",
  132. bar: "baz",
  133. where: "there"};
  134. var res = $.route.param(data);
  135. var obj = $.route.deparam(res);
  136. delete obj.route
  137. same(obj,data )
  138. return;
  139. data = {page: "jQuery.Controller", type: "foo", bar: "baz", where: "there"};
  140. res = $.route.param(data);
  141. obj = $.route.deparam(res);
  142. delete obj.route;
  143. same(data, obj)
  144. data = {page: " a ", type: " / "};
  145. res = $.route.param(data);
  146. obj = $.route.deparam(res);
  147. delete obj.route;
  148. same(obj ,data ,"slashes and spaces")
  149. data = {page: "index", type: "foo", bar: "baz", where: "there"};
  150. res = $.route.param(data);
  151. obj = $.route.deparam(res);
  152. delete obj.route;
  153. same(data, obj)
  154. $.route.routes = {};
  155. data = {page: "foo", bar: "baz", where: "there"};
  156. res = $.route.param(data);
  157. obj = $.route.deparam(res);
  158. same(data, obj)
  159. })
  160. test("precident", function(){
  161. $.route.routes = {};
  162. $.route(":who",{who: "index"});
  163. $.route("search/:search");
  164. var obj = $.route.deparam("jQuery.Controller");
  165. same(obj, {
  166. who : "jQuery.Controller",
  167. route: ":who"
  168. });
  169. obj = $.route.deparam("search/jQuery.Controller");
  170. same(obj, {
  171. search : "jQuery.Controller",
  172. route: "search/:search"
  173. },"bad deparam");
  174. equal( $.route.param({
  175. search : "jQuery.Controller"
  176. }),
  177. "search/jQuery.Controller" , "bad param");
  178. equal( $.route.param({
  179. who : "jQuery.Controller"
  180. }),
  181. "jQuery.Controller" );
  182. })
  183. test("precident2", function(){
  184. $.route.routes = {};
  185. $.route(":type",{who: "index"});
  186. $.route(":type/:id");
  187. equal( $.route.param({
  188. type : "foo",
  189. id: "bar"
  190. }),
  191. "foo/bar" );
  192. })
  193. test("linkTo", function(){
  194. $.route.routes = {};
  195. $.route(":foo");
  196. var res = $.route.link("Hello",{foo: "bar", baz: 'foo'});
  197. equal( res, '<a href="#!bar&baz=foo">Hello</a>');
  198. })
  199. test("param with route defined", function(){
  200. $.route.routes = {};
  201. $.route("holler")
  202. $.route("foo");
  203. var data = {foo: "abc",route: "foo"}
  204. var res = $.route.param(data);
  205. equal(res, "foo&foo=abc")
  206. })
  207. test("route endings", function(){
  208. $.route.routes = {};
  209. $.route("foo",{foo: true});
  210. $.route("food",{food: true})
  211. var res = $.route.deparam("food")
  212. ok(res.food, "we get food back")
  213. })
  214. })