organizationCrumb.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 OrganizationsStore from 'sentry/stores/organizationsStore';
  5. import type {Config} from 'sentry/types/system';
  6. import {browserHistory} from 'sentry/utils/browserHistory';
  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 = window.__initialData;
  24. jest.mocked(browserHistory.push).mockReset();
  25. jest.mocked(window.location.assign).mockReset();
  26. });
  27. const switchOrganization = async () => {
  28. await userEvent.hover(screen.getByRole('link'));
  29. await userEvent.click(screen.getAllByRole('option')[1]);
  30. };
  31. const renderComponent = (
  32. props: Partial<
  33. Pick<React.ComponentProps<typeof OrganizationCrumb>, 'route' | 'routes' | 'params'>
  34. >
  35. ) =>
  36. render(<OrganizationCrumb {...routerProps} params={{}} {...props} />, {
  37. router,
  38. organization,
  39. });
  40. afterEach(function () {
  41. window.__initialData = initialData;
  42. });
  43. it('switches organizations on settings index', async function () {
  44. const routes: RouteWithName[] = [
  45. {path: '/'},
  46. {},
  47. {path: '/foo/'},
  48. {},
  49. {path: ':bar'},
  50. {path: '/settings/', name: 'Settings'},
  51. {name: 'Organizations', path: ':orgId/'},
  52. ];
  53. const route = routes[6];
  54. renderComponent({routes, route});
  55. await switchOrganization();
  56. expect(browserHistory.push).toHaveBeenCalledWith('/settings/org-slug2/');
  57. });
  58. it('switches organizations while on API Keys Details route', async function () {
  59. const routes: RouteWithName[] = [
  60. {path: '/'},
  61. {},
  62. {path: '/foo/'},
  63. {},
  64. {path: ':bar'},
  65. {path: '/settings/', name: 'Settings'},
  66. {name: 'Organizations', path: ':orgId/'},
  67. {},
  68. {path: 'api-keys/', name: 'API Key'},
  69. {path: ':apiKey/', name: 'API Key Details'},
  70. ];
  71. const route = routes[6];
  72. renderComponent({routes, route});
  73. await switchOrganization();
  74. expect(browserHistory.push).toHaveBeenCalledWith('/settings/org-slug2/api-keys/');
  75. });
  76. it('switches organizations while on API Keys List route', async function () {
  77. const routes: RouteWithName[] = [
  78. {path: '/'},
  79. {},
  80. {path: '/foo/'},
  81. {},
  82. {path: ':bar'},
  83. {path: '/settings/', name: 'Settings'},
  84. {name: 'Organizations', path: ':orgId/'},
  85. {},
  86. {path: 'api-keys/', name: 'API Key'},
  87. ];
  88. const route = routes[6];
  89. renderComponent({routes, route});
  90. await switchOrganization();
  91. expect(browserHistory.push).toHaveBeenCalledWith('/settings/org-slug2/api-keys/');
  92. });
  93. it('switches organizations while in Project Client Keys Details route', async function () {
  94. const routes: RouteWithName[] = [
  95. {path: '/'},
  96. {path: '/settings/', name: 'Settings'},
  97. {name: 'Organization', path: ':orgId/'},
  98. {name: 'Project', path: 'projects/:projectId/'},
  99. {path: 'keys/', name: 'Client Keys'},
  100. {path: ':keyId/', name: 'Details'},
  101. ];
  102. const route = routes[2];
  103. renderComponent({
  104. params: {projectId: project.slug},
  105. routes,
  106. route,
  107. });
  108. await switchOrganization();
  109. expect(browserHistory.push).toHaveBeenCalledWith('/settings/org-slug2/');
  110. });
  111. it('switches organizations for child route with customer domains', async function () {
  112. window.__initialData = {
  113. customerDomain: {
  114. subdomain: 'albertos-apples',
  115. organizationUrl: 'https://albertos-apples.sentry.io',
  116. sentryUrl: 'https://sentry.io',
  117. },
  118. } as Config;
  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. features: ['customer-domains'],
  137. links: {
  138. organizationUrl: 'https://acme.sentry.io',
  139. regionUrl: 'https://us.sentry.io',
  140. },
  141. }),
  142. ];
  143. OrganizationsStore.load(orgs);
  144. renderComponent({routes, route});
  145. await switchOrganization();
  146. expect(window.location.assign).toHaveBeenCalledWith(
  147. 'https://acme.sentry.io/settings/api-keys/'
  148. );
  149. });
  150. });