organizationCrumb.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import OrganizationsStore from 'sentry/stores/organizationsStore';
  6. import type {Config} from 'sentry/types/system';
  7. import {OrganizationCrumb} from './organizationCrumb';
  8. import type {RouteWithName} from './types';
  9. jest.unmock('sentry/utils/recreateRoute');
  10. describe('OrganizationCrumb', function () {
  11. let initialData: Config;
  12. const {organization, project, router, routerProps} = initializeOrg();
  13. const organizations = [
  14. organization,
  15. OrganizationFixture({
  16. id: '234',
  17. slug: 'org-slug2',
  18. }),
  19. ];
  20. beforeEach(() => {
  21. OrganizationsStore.init();
  22. OrganizationsStore.load(organizations);
  23. initialData = ConfigStore.getState();
  24. jest.mocked(window.location.assign).mockReset();
  25. });
  26. const switchOrganization = async () => {
  27. await userEvent.hover(screen.getByRole('link'));
  28. await userEvent.click(screen.getAllByRole('option')[1]!);
  29. };
  30. const renderComponent = (
  31. props: Partial<
  32. Pick<React.ComponentProps<typeof OrganizationCrumb>, 'route' | 'routes' | 'params'>
  33. >
  34. ) =>
  35. render(<OrganizationCrumb {...routerProps} params={{}} {...props} />, {
  36. router,
  37. organization,
  38. });
  39. afterEach(function () {
  40. ConfigStore.loadInitialData(initialData);
  41. });
  42. it('switches organizations on settings index', async function () {
  43. const routes: RouteWithName[] = [
  44. {path: '/'},
  45. {},
  46. {path: '/foo/'},
  47. {},
  48. {path: ':bar'},
  49. {path: '/settings/', name: 'Settings'},
  50. {name: 'Organizations', path: ':orgId/'},
  51. ];
  52. const route = routes[6];
  53. renderComponent({routes, route});
  54. await switchOrganization();
  55. expect(router.push).toHaveBeenCalledWith({pathname: '/settings/org-slug2/'});
  56. });
  57. it('switches organizations while on API Keys Details route', async function () {
  58. const routes: RouteWithName[] = [
  59. {path: '/'},
  60. {},
  61. {path: '/foo/'},
  62. {},
  63. {path: ':bar'},
  64. {path: '/settings/', name: 'Settings'},
  65. {name: 'Organizations', path: ':orgId/'},
  66. {},
  67. {path: 'api-keys/', name: 'API Key'},
  68. {path: ':apiKey/', name: 'API Key Details'},
  69. ];
  70. const route = routes[6];
  71. renderComponent({routes, route});
  72. await switchOrganization();
  73. expect(router.push).toHaveBeenCalledWith({pathname: '/settings/org-slug2/api-keys/'});
  74. });
  75. it('switches organizations while on API Keys List route', async function () {
  76. const routes: RouteWithName[] = [
  77. {path: '/'},
  78. {},
  79. {path: '/foo/'},
  80. {},
  81. {path: ':bar'},
  82. {path: '/settings/', name: 'Settings'},
  83. {name: 'Organizations', path: ':orgId/'},
  84. {},
  85. {path: 'api-keys/', name: 'API Key'},
  86. ];
  87. const route = routes[6];
  88. renderComponent({routes, route});
  89. await switchOrganization();
  90. expect(router.push).toHaveBeenCalledWith({pathname: '/settings/org-slug2/api-keys/'});
  91. });
  92. it('switches organizations while in Project Client Keys Details route', async function () {
  93. const routes: RouteWithName[] = [
  94. {path: '/'},
  95. {path: '/settings/', name: 'Settings'},
  96. {name: 'Organization', path: ':orgId/'},
  97. {name: 'Project', path: 'projects/:projectId/'},
  98. {path: 'keys/', name: 'Client Keys'},
  99. {path: ':keyId/', name: 'Details'},
  100. ];
  101. const route = routes[2];
  102. renderComponent({
  103. params: {projectId: project.slug},
  104. routes,
  105. route,
  106. });
  107. await switchOrganization();
  108. expect(router.push).toHaveBeenCalledWith({pathname: '/settings/org-slug2/'});
  109. });
  110. it('switches organizations for child route with customer domains', async function () {
  111. ConfigStore.set('customerDomain', {
  112. subdomain: 'albertos-apples',
  113. organizationUrl: 'https://albertos-apples.sentry.io',
  114. sentryUrl: 'https://sentry.io',
  115. });
  116. ConfigStore.set('features', new Set(['system:multi-region']));
  117. const routes: RouteWithName[] = [
  118. {path: '/'},
  119. {},
  120. {path: '/foo/'},
  121. {},
  122. {path: ':bar'},
  123. {path: '/settings/', name: 'Settings'},
  124. {name: 'Organizations', path: ':orgId/'},
  125. {},
  126. {path: 'api-keys/', name: 'API Key'},
  127. ];
  128. const route = routes[6];
  129. const orgs = [
  130. organization,
  131. OrganizationFixture({
  132. id: '234',
  133. slug: 'acme',
  134. links: {
  135. organizationUrl: 'https://acme.sentry.io',
  136. regionUrl: 'https://us.sentry.io',
  137. },
  138. }),
  139. ];
  140. OrganizationsStore.load(orgs);
  141. renderComponent({routes, route});
  142. await switchOrganization();
  143. expect(window.location.assign).toHaveBeenCalledWith(
  144. 'https://acme.sentry.io/settings/api-keys/'
  145. );
  146. });
  147. });