organizationCrumb.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {browserHistory} from 'react-router';
  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 {Config} from 'sentry/types';
  6. import {OrganizationCrumb} from 'sentry/views/settings/components/settingsBreadcrumb/organizationCrumb';
  7. import {RouteWithName} from './types';
  8. jest.unmock('sentry/utils/recreateRoute');
  9. describe('OrganizationCrumb', function () {
  10. let initialData: Config;
  11. const {organization, project, routerContext, routerProps} = initializeOrg();
  12. const organizations = [
  13. organization,
  14. TestStubs.Organization({
  15. id: '234',
  16. slug: 'org-slug2',
  17. }),
  18. ];
  19. beforeEach(() => {
  20. OrganizationsStore.init();
  21. OrganizationsStore.load(organizations);
  22. });
  23. const switchOrganization = async () => {
  24. await userEvent.hover(screen.getByRole('link'));
  25. await userEvent.click(screen.getAllByRole('option')[1]);
  26. };
  27. const renderComponent = (
  28. props: Partial<
  29. Pick<React.ComponentProps<typeof OrganizationCrumb>, 'route' | 'routes' | 'params'>
  30. >
  31. ) =>
  32. render(<OrganizationCrumb {...routerProps} params={{}} {...props} />, {
  33. context: routerContext,
  34. organization,
  35. });
  36. beforeEach(function () {
  37. initialData = window.__initialData;
  38. jest.mocked(browserHistory.push).mockReset();
  39. jest.mocked(window.location.assign).mockReset();
  40. });
  41. afterEach(function () {
  42. window.__initialData = 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. window.__initialData = {
  114. customerDomain: {
  115. subdomain: 'albertos-apples',
  116. organizationUrl: 'https://albertos-apples.sentry.io',
  117. sentryUrl: 'https://sentry.io',
  118. },
  119. } as Config;
  120. const routes: RouteWithName[] = [
  121. {path: '/'},
  122. {},
  123. {path: '/foo/'},
  124. {},
  125. {path: ':bar'},
  126. {path: '/settings/', name: 'Settings'},
  127. {name: 'Organizations', path: ':orgId/'},
  128. {},
  129. {path: 'api-keys/', name: 'API Key'},
  130. ];
  131. const route = routes[6];
  132. const orgs = [
  133. organization,
  134. TestStubs.Organization({
  135. id: '234',
  136. slug: 'acme',
  137. features: ['customer-domains'],
  138. links: {
  139. organizationUrl: 'https://acme.sentry.io',
  140. },
  141. }),
  142. ];
  143. OrganizationsStore.load(orgs);
  144. renderComponent({routes, route});
  145. await switchOrganization();
  146. // The double slug doesn't actually show up as we have more routing context present.
  147. expect(window.location.assign).toHaveBeenCalledWith(
  148. 'https://acme.sentry.io/settings/acme/api-keys/'
  149. );
  150. });
  151. });