organizationCrumb.spec.tsx 4.9 KB

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