recreateRoute.spec.tsx 3.0 KB

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