organizationCrumb.spec.tsx 4.9 KB

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