1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import {location} from 'fixtures/js-stubs/location';
- import recreateRoute from 'sentry/utils/recreateRoute';
- jest.unmock('sentry/utils/recreateRoute');
- const routes = [
- {path: '/', childRoutes: []},
- {childRoutes: []},
- {path: '/settings/', name: 'Settings'},
- {name: 'Organizations', path: ':orgId/', childRoutes: []},
- {childRoutes: []},
- {path: 'api-keys/', name: 'API Key'},
- ];
- const projectRoutes = [
- {path: '/', childRoutes: []},
- {childRoutes: []},
- {path: '/settings/', name: 'Settings', indexRoute: {}, childRoutes: []},
- {name: 'Organizations', path: ':orgId/', childRoutes: []},
- {name: 'Projects', path: ':projectId/', childRoutes: []},
- {name: 'Alerts', path: 'alerts/'},
- ];
- const params = {
- orgId: 'org-slug',
- projectId: 'project-slug',
- };
- const location = {
- ...location(),
- search: '',
- };
- describe('recreateRoute', function () {
- it('returns correct path to a route object', function () {
- expect(recreateRoute(routes[0], {routes, params})).toBe('/');
- expect(recreateRoute(routes[1], {routes, params})).toBe('/');
- expect(recreateRoute(routes[2], {routes, params})).toBe('/settings/');
- expect(recreateRoute(routes[3], {routes, params})).toBe('/settings/org-slug/');
- expect(recreateRoute(routes[4], {routes, params})).toBe('/settings/org-slug/');
- expect(recreateRoute(routes[5], {routes, params})).toBe(
- '/settings/org-slug/api-keys/'
- );
- expect(
- recreateRoute(projectRoutes[5], {routes: projectRoutes, location, params})
- ).toBe('/settings/org-slug/project-slug/alerts/');
- });
- it('has correct path with route object with many roots (starts with "/")', function () {
- const r = [
- {path: '/', childRoutes: []},
- {childRoutes: []},
- {path: '/foo/', childRoutes: []},
- {childRoutes: []},
- {path: 'bar', childRoutes: []},
- {path: '/settings/', name: 'Settings'},
- {name: 'Organizations', path: ':orgId/', childRoutes: []},
- {childRoutes: []},
- {path: 'api-keys/', name: 'API Key'},
- ];
- expect(recreateRoute(r[4], {routes: r, params})).toBe('/foo/bar');
- });
- it('returns correct path to a string (at the end of the routes)', function () {
- expect(recreateRoute('test/', {routes, location, params})).toBe(
- '/settings/org-slug/api-keys/test/'
- );
- });
- it('returns correct path to a string after the 2nd to last route', function () {
- expect(recreateRoute('test/', {routes, location, params, stepBack: -2})).toBe(
- '/settings/org-slug/test/'
- );
- });
- it('switches to new org but keeps current route', function () {
- expect(recreateRoute(routes[5], {routes, location, params: {orgId: 'new-org'}})).toBe(
- '/settings/new-org/api-keys/'
- );
- });
- it('maintains the query string', function () {
- const withSearch = {
- ...location(),
- search: '?key1=foo&key2=bar',
- };
- expect(recreateRoute(routes[5], {routes, params, location: withSearch})).toBe(
- '/settings/org-slug/api-keys/?key1=foo&key2=bar'
- );
- });
- });
|