recreateRoute.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import recreateRoute from 'sentry/utils/recreateRoute';
  2. jest.unmock('sentry/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. ...TestStubs.location(),
  25. search: '',
  26. };
  27. describe('recreateRoute', function () {
  28. it('returns correct path to a route object', function () {
  29. expect(recreateRoute(routes[0], {routes, params})).toBe('/');
  30. expect(recreateRoute(routes[1], {routes, params})).toBe('/');
  31. expect(recreateRoute(routes[2], {routes, params})).toBe('/settings/');
  32. expect(recreateRoute(routes[3], {routes, params})).toBe('/settings/org-slug/');
  33. expect(recreateRoute(routes[4], {routes, params})).toBe('/settings/org-slug/');
  34. expect(recreateRoute(routes[5], {routes, params})).toBe(
  35. '/settings/org-slug/api-keys/'
  36. );
  37. expect(
  38. recreateRoute(projectRoutes[5], {routes: projectRoutes, location, params})
  39. ).toBe('/settings/org-slug/project-slug/alerts/');
  40. });
  41. it('has correct path with route object with many roots (starts with "/")', function () {
  42. const r = [
  43. {path: '/', childRoutes: []},
  44. {childRoutes: []},
  45. {path: '/foo/', childRoutes: []},
  46. {childRoutes: []},
  47. {path: 'bar', childRoutes: []},
  48. {path: '/settings/', name: 'Settings'},
  49. {name: 'Organizations', path: ':orgId/', childRoutes: []},
  50. {childRoutes: []},
  51. {path: 'api-keys/', name: 'API Key'},
  52. ];
  53. expect(recreateRoute(r[4], {routes: r, params})).toBe('/foo/bar');
  54. });
  55. it('returns correct path to a string (at the end of the routes)', function () {
  56. expect(recreateRoute('test/', {routes, location, params})).toBe(
  57. '/settings/org-slug/api-keys/test/'
  58. );
  59. });
  60. it('returns correct path to a string after the 2nd to last route', function () {
  61. expect(recreateRoute('test/', {routes, location, params, stepBack: -2})).toBe(
  62. '/settings/org-slug/test/'
  63. );
  64. });
  65. it('switches to new org but keeps current route', function () {
  66. expect(recreateRoute(routes[5], {routes, location, params: {orgId: 'new-org'}})).toBe(
  67. '/settings/new-org/api-keys/'
  68. );
  69. });
  70. it('maintains the query string', function () {
  71. const withSearch = {
  72. ...TestStubs.location(),
  73. search: '?key1=foo&key2=bar',
  74. };
  75. expect(recreateRoute(routes[5], {routes, params, location: withSearch})).toBe(
  76. '/settings/org-slug/api-keys/?key1=foo&key2=bar'
  77. );
  78. });
  79. });