router.path.spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Router = FlowRouter.Router;
  2. Tinytest.addAsync('Common - Router - validate path definition', function (test, next) {
  3. // path must start with '/'
  4. try {
  5. FlowRouter.route(Random.id());
  6. } catch(ex) {
  7. next();
  8. }
  9. });
  10. Tinytest.add('Common - Router - path - generic', function (test) {
  11. var pathDef = "/blog/:blogId/some/:name";
  12. var fields = {
  13. blogId: "1001",
  14. name: "superb"
  15. };
  16. var expectedPath = "/blog/1001/some/superb";
  17. var path = FlowRouter.path(pathDef, fields);
  18. test.equal(path, expectedPath);
  19. });
  20. Tinytest.add('Common - Router - path - queryParams', function (test) {
  21. var pathDef = "/blog/:blogId/some/:name";
  22. var fields = {
  23. blogId: "1001",
  24. name: "superb"
  25. };
  26. var queryParams = {
  27. aa: "100",
  28. bb: "200"
  29. };
  30. var expectedPath = "/blog/1001/some/superb?aa=100&bb=200";
  31. var path = FlowRouter.path(pathDef, fields, queryParams);
  32. test.equal(path, expectedPath);
  33. });
  34. Tinytest.add('Common - Router - path - just queryParams', function (test) {
  35. var pathDef = "/blog/abc";
  36. var queryParams = {
  37. aa: "100",
  38. bb: "200"
  39. };
  40. var expectedPath = "/blog/abc?aa=100&bb=200";
  41. var path = FlowRouter.path(pathDef, null, queryParams);
  42. test.equal(path, expectedPath);
  43. });
  44. Tinytest.add('Common - Router - path - missing fields', function (test) {
  45. var pathDef = "/blog/:blogId/some/:name";
  46. var fields = {
  47. blogId: "1001",
  48. };
  49. var expectedPath = "/blog/1001/some";
  50. var path = FlowRouter.path(pathDef, fields);
  51. test.equal(path, expectedPath);
  52. });
  53. Tinytest.add('Common - Router - path - no fields', function (test) {
  54. var pathDef = "/blog/blogId/some/name";
  55. var path = FlowRouter.path(pathDef);
  56. test.equal(path, pathDef);
  57. });
  58. Tinytest.add('Common - Router - path - complex route', function (test) {
  59. var pathDef = "/blog/:blogId/some/:name(\\d*)+";
  60. var fields = {
  61. blogId: "1001",
  62. name: 20
  63. };
  64. var expectedPath = "/blog/1001/some/20";
  65. var path = FlowRouter.path(pathDef, fields);
  66. test.equal(path, expectedPath);
  67. });
  68. Tinytest.add('Common - Router - path - optional last param missing', function (test) {
  69. var pathDef = "/blog/:blogId/some/:name?";
  70. var fields = {
  71. blogId: "1001"
  72. };
  73. var expectedPath = "/blog/1001/some";
  74. var path = FlowRouter.path(pathDef, fields);
  75. test.equal(path, expectedPath);
  76. });
  77. Tinytest.add('Common - Router - path - optional last param exists', function (test) {
  78. var pathDef = "/blog/:blogId/some/:name?";
  79. var fields = {
  80. blogId: "1001",
  81. name: 20
  82. };
  83. var expectedPath = "/blog/1001/some/20";
  84. var path = FlowRouter.path(pathDef, fields);
  85. test.equal(path, expectedPath);
  86. });
  87. Tinytest.add('Common - Router - path - remove trailing slashes', function (test) {
  88. var pathDef = "/blog/:blogId/some/:name//";
  89. var fields = {
  90. blogId: "1001",
  91. name: "superb"
  92. };
  93. var expectedPath = "/blog/1001/some/superb";
  94. var path = FlowRouter.path(pathDef, fields);
  95. test.equal(path, expectedPath);
  96. });
  97. Tinytest.add('Common - Router - path - handle multiple slashes', function (test) {
  98. var pathDef = "/blog///some/hi////";
  99. var expectedPath = "/blog/some/hi";
  100. var path = FlowRouter.path(pathDef);
  101. test.equal(path, expectedPath);
  102. });
  103. Tinytest.add('Common - Router - path - keep the root slash', function (test) {
  104. var pathDef = "/";
  105. var fields = {};
  106. var expectedPath = "/";
  107. var path = FlowRouter.path(pathDef, fields);
  108. test.equal(path, expectedPath);
  109. });