recreateRoute.spec.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import recreateRoute from 'app/utils/recreateRoute';
  2. jest.unmock('app/utils/recreateRoute');
  3. const routes = [
  4. {path: '/', childRoutes: []},
  5. {childRoutes: []},
  6. {path: '/settings/', name: 'Settings'},
  7. {name: 'Organizations', path: ':orgId/', childRoutes: []},
  8. {childRoutes: []},
  9. {path: 'api-keys/', name: 'API Key'},
  10. ];
  11. const projectRoutes = [
  12. {path: '/', childRoutes: []},
  13. {childRoutes: []},
  14. {path: '/settings/', name: 'Settings', indexRoute: {}, childRoutes: []},
  15. {name: 'Organizations', path: ':orgId/', childRoutes: []},
  16. {name: 'Projects', path: ':projectId/', childRoutes: []},
  17. {name: 'Alerts', path: 'alerts/'},
  18. ];
  19. const params = {
  20. orgId: 'org-slug',
  21. projectId: 'project-slug',
  22. };
  23. const location = {
  24. search: '',
  25. };
  26. describe('recreateRoute', function() {
  27. it('returns correct path to a route object', function() {
  28. expect(recreateRoute(routes[0], {routes, params})).toBe('/');
  29. expect(recreateRoute(routes[1], {routes, params})).toBe('/');
  30. expect(recreateRoute(routes[2], {routes, params})).toBe('/settings/');
  31. expect(recreateRoute(routes[3], {routes, params})).toBe('/settings/org-slug/');
  32. expect(recreateRoute(routes[4], {routes, params})).toBe('/settings/org-slug/');
  33. expect(recreateRoute(routes[5], {routes, params})).toBe(
  34. '/settings/org-slug/api-keys/'
  35. );
  36. expect(
  37. recreateRoute(projectRoutes[5], {routes: projectRoutes, location, params})
  38. ).toBe('/settings/org-slug/project-slug/alerts/');
  39. });
  40. it('has correct path with route object with many roots (starts with "/")', function() {
  41. const r = [
  42. {path: '/', childRoutes: []},
  43. {childRoutes: []},
  44. {path: '/foo/', childRoutes: []},
  45. {childRoutes: []},
  46. {path: 'bar', childRoutes: []},
  47. {path: '/settings/', name: 'Settings'},
  48. {name: 'Organizations', path: ':orgId/', childRoutes: []},
  49. {childRoutes: []},
  50. {path: 'api-keys/', name: 'API Key'},
  51. ];
  52. expect(recreateRoute(r[4], {routes: r, params})).toBe('/foo/bar');
  53. });
  54. it('returns correct path to a string (at the end of the routes)', function() {
  55. expect(recreateRoute('test/', {routes, location, params})).toBe(
  56. '/settings/org-slug/api-keys/test/'
  57. );
  58. });
  59. it('returns correct path to a string after the 2nd to last route', function() {
  60. expect(recreateRoute('test/', {routes, location, params, stepBack: -2})).toBe(
  61. '/settings/org-slug/test/'
  62. );
  63. });
  64. it('stepBack needs to be less than 0', function() {
  65. expect(() => recreateRoute('', {routes, location, params, stepBack: 0})).toThrow();
  66. });
  67. it('switches to new org but keeps current route', function() {
  68. expect(recreateRoute(routes[5], {routes, location, params: {orgId: 'new-org'}})).toBe(
  69. '/settings/new-org/api-keys/'
  70. );
  71. });
  72. it('maintains the query strting', function() {
  73. const withSearch = {
  74. search: '?key1=foo&key2=bar',
  75. };
  76. expect(recreateRoute(routes[5], {routes, params, location: withSearch})).toBe(
  77. '/settings/org-slug/api-keys/?key1=foo&key2=bar'
  78. );
  79. });
  80. });