PageRenderTime 65ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/www/js/dbp/tests/Router.js

https://github.com/tornak47/dojo-demo
JavaScript | 109 lines | 95 code | 14 blank | 0 comment | 0 complexity | bb0aa1e71e807ff2f88d087ef072dd63 MD5 | raw file
  1. dojo.provide('dbp.tests.Router');
  2. dojo.require('dbp.Router');
  3. (function() {
  4. var testVal,
  5. routes = [
  6. {
  7. path : '/test',
  8. handler : function() {
  9. testVal = '/test';
  10. },
  11. defaultRoute : true
  12. },
  13. {
  14. path : '/test2',
  15. handler : function() {
  16. testVal = '/test2';
  17. }
  18. },
  19. {
  20. path : '/basic',
  21. handler : function() {
  22. testVal = 'basic';
  23. }
  24. },
  25. {
  26. path : '/bar/:baz',
  27. handler : function(params) {
  28. testVal = params.baz;
  29. }
  30. },
  31. {
  32. path : '/bar/:baz/:bim',
  33. handler : function(params) {
  34. testVal = params.baz + params.bim;
  35. }
  36. },
  37. {
  38. path : '/bar/:baz/bim/:bop',
  39. handler : function(params) {
  40. testVal = params.baz + params.bop;
  41. }
  42. },
  43. {
  44. path : /\/splat\/(.*)/,
  45. handler : function(params) {
  46. testVal = params.splat[0];
  47. }
  48. },
  49. {
  50. path : /\/splat\/(.*)\/foo\/(.*)/,
  51. handler : function(params) {
  52. testVal = params.splat[0] + ':' + params.splat[1];
  53. }
  54. }
  55. ];
  56. doh.register('dbp.Router', [
  57. {
  58. name : "It should handle a hash if one is set when initialized",
  59. setUp : function() {
  60. this.router = new dbp.Router(routes);
  61. },
  62. runTest : function() {
  63. window.location.hash = '#/test';
  64. this.router.init();
  65. doh.is(testVal, '/test');
  66. },
  67. tearDown : function() {
  68. this.router.destroy();
  69. }
  70. },
  71. {
  72. name : "It should route the request to the appropriate route with the appropriate parameters",
  73. setUp : function() {
  74. this.router = new dbp.Router(routes);
  75. },
  76. runTest : function() {
  77. this.router.go('/bar/test-123');
  78. doh.is(testVal, 'test-123');
  79. this.router.go('/basic');
  80. doh.is(testVal, 'basic');
  81. this.router.go('/bar/hello/world');
  82. doh.is(testVal, 'helloworld');
  83. this.router.go('/bar/testing/bim/123');
  84. doh.is(testVal, 'testing123');
  85. this.router.go('/splat/1/2/3');
  86. doh.is(testVal, '1/2/3');
  87. this.router.go('/splat/1/2/3/foo/4/5/6');
  88. doh.is(testVal, '1/2/3:4/5/6');
  89. },
  90. tearDown : function() {
  91. this.router.destroy();
  92. }
  93. }
  94. ]);
  95. }());