recreateRoute.spec.jsx 2.1 KB

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