router-branch.spec.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {expect} from 'chai';
  2. import {ROOT_PATH} from '../constants.js';
  3. import {TrieRouter} from '../trie-router.js';
  4. import {RouterBranch} from './router-branch.js';
  5. import {HttpMethod, Route} from '../route/route.js';
  6. describe('RouterBranch', function () {
  7. describe('constructor', function () {
  8. it('should use a service container from a given router', function () {
  9. const router = new TrieRouter();
  10. const S = new RouterBranch(router, {path: ROOT_PATH});
  11. expect(S.container).to.be.eq(router.container);
  12. });
  13. it('should merge a parent definition with a given definition', function () {
  14. const router = new TrieRouter();
  15. const parent = router.createBranch({path: 'foo'});
  16. const S = new RouterBranch(router, {path: 'bar'}, parent);
  17. expect(S.getDefinition().path).to.be.eq('/foo/bar');
  18. });
  19. });
  20. describe('getRouter', function () {
  21. it('should return the router instance that was provided to the constructor', function () {
  22. const router = new TrieRouter();
  23. const S = new RouterBranch(router, {path: ROOT_PATH});
  24. expect(S.getRouter()).to.be.eq(router);
  25. });
  26. });
  27. describe('getDefinition', function () {
  28. it('should return the branch definition that was provided to the constructor', function () {
  29. const router = new TrieRouter();
  30. const branchDef = {path: ROOT_PATH};
  31. const S = new RouterBranch(router, branchDef);
  32. expect(S.getDefinition()).to.be.eql(branchDef);
  33. });
  34. });
  35. describe('getParentBranch', function () {
  36. it('should return the parent branch that was provided to the constructor', function () {
  37. const router = new TrieRouter();
  38. const parent = router.createBranch({path: ROOT_PATH});
  39. const S = new RouterBranch(router, {path: ROOT_PATH}, parent);
  40. expect(S.getParentBranch()).to.be.eq(parent);
  41. });
  42. });
  43. describe('defineRoute', function () {
  44. it('should return a Route instance', function () {
  45. const router = new TrieRouter();
  46. const S = new RouterBranch(router, {path: 'foo'});
  47. const res = S.defineRoute({
  48. method: HttpMethod.GET,
  49. path: 'bar',
  50. handler: () => undefined,
  51. });
  52. expect(res).to.be.instanceOf(Route);
  53. });
  54. it('should combine a branch path with a route path', function () {
  55. const router = new TrieRouter();
  56. const S = new RouterBranch(router, {path: 'foo'});
  57. const res = S.defineRoute({
  58. method: HttpMethod.GET,
  59. path: 'bar',
  60. handler: () => undefined,
  61. });
  62. expect(res.path).to.be.eq('/foo/bar');
  63. });
  64. });
  65. describe('createBranch', function () {
  66. it('should return a RouterBranch instance', function () {
  67. const router = new TrieRouter();
  68. const S = new RouterBranch(router, {path: 'foo'});
  69. const res = S.createBranch({path: 'bar'});
  70. expect(res).to.be.instanceOf(RouterBranch);
  71. });
  72. it('should combine a current path with a new path', function () {
  73. const router = new TrieRouter();
  74. const S = new RouterBranch(router, {path: 'foo'});
  75. const res = S.createBranch({path: 'bar'});
  76. expect(res.getDefinition().path).to.be.eq('/foo/bar');
  77. });
  78. });
  79. });