withDomainRequired.spec.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import type {RouteComponentProps} from 'react-router';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen} from 'sentry-test/reactTestingLibrary';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import type {Config} from 'sentry/types/system';
  7. import withDomainRequired from 'sentry/utils/withDomainRequired';
  8. const originalLocation = window.location;
  9. describe('withDomainRequired', function () {
  10. type Props = RouteComponentProps<{orgId: string}, {}>;
  11. function MyComponent(props: Props) {
  12. const {params} = props;
  13. return <div>Org slug: {params.orgId ?? 'no org slug'}</div>;
  14. }
  15. let configState: Config;
  16. beforeEach(function () {
  17. Object.defineProperty(window, 'location', {
  18. writable: true,
  19. value: {
  20. replace: jest.fn(),
  21. pathname: '/organizations/albertos-apples/issues/',
  22. search: '?q=123',
  23. hash: '#hash',
  24. },
  25. });
  26. configState = ConfigStore.getState();
  27. ConfigStore.loadInitialData({
  28. ...configState,
  29. customerDomain: {
  30. subdomain: 'albertos-apples',
  31. organizationUrl: 'https://albertos-apples.sentry.io',
  32. sentryUrl: 'https://sentry.io',
  33. },
  34. links: {
  35. organizationUrl: undefined,
  36. regionUrl: undefined,
  37. sentryUrl: 'https://sentry.io',
  38. },
  39. });
  40. });
  41. afterEach(function () {
  42. window.location = originalLocation;
  43. ConfigStore.loadInitialData(configState);
  44. });
  45. it('redirects to sentryUrl in non-customer domain world', function () {
  46. ConfigStore.loadInitialData({
  47. ...configState,
  48. customerDomain: null,
  49. features: new Set(['system:multi-region']),
  50. links: {
  51. organizationUrl: undefined,
  52. regionUrl: undefined,
  53. sentryUrl: 'https://sentry.io',
  54. },
  55. });
  56. const organization = OrganizationFixture({
  57. slug: 'albertos-apples',
  58. features: [],
  59. });
  60. const params = {
  61. orgId: 'albertos-apples',
  62. };
  63. const {router} = initializeOrg({
  64. organization,
  65. router: {
  66. params,
  67. },
  68. });
  69. const WrappedComponent = withDomainRequired(MyComponent);
  70. const {container} = render(
  71. <WrappedComponent
  72. router={router}
  73. location={router.location}
  74. params={params}
  75. routes={router.routes}
  76. routeParams={router.params}
  77. route={{}}
  78. />,
  79. {router}
  80. );
  81. expect(container).toBeEmptyDOMElement();
  82. expect(window.location.replace).toHaveBeenCalledTimes(1);
  83. expect(window.location.replace).toHaveBeenCalledWith(
  84. 'https://sentry.io/organizations/albertos-apples/issues/?q=123#hash'
  85. );
  86. });
  87. it('redirects to sentryUrl if multi-region feature is omitted', function () {
  88. ConfigStore.loadInitialData({
  89. ...configState,
  90. customerDomain: {
  91. subdomain: 'albertos-apples',
  92. organizationUrl: 'https://albertos-apples.sentry.io',
  93. sentryUrl: 'https://sentry.io',
  94. },
  95. features: new Set(),
  96. links: {
  97. organizationUrl: undefined,
  98. regionUrl: undefined,
  99. sentryUrl: 'https://sentry.io',
  100. },
  101. });
  102. const organization = OrganizationFixture({
  103. slug: 'albertos-apples',
  104. features: [],
  105. });
  106. const params = {
  107. orgId: 'albertos-apples',
  108. };
  109. const {router} = initializeOrg({
  110. organization,
  111. router: {
  112. params,
  113. },
  114. });
  115. const WrappedComponent = withDomainRequired(MyComponent);
  116. const {container} = render(
  117. <WrappedComponent
  118. router={router}
  119. location={router.location}
  120. params={params}
  121. routes={router.routes}
  122. routeParams={router.params}
  123. route={{}}
  124. />,
  125. {router}
  126. );
  127. expect(container).toBeEmptyDOMElement();
  128. expect(window.location.replace).toHaveBeenCalledTimes(1);
  129. expect(window.location.replace).toHaveBeenCalledWith(
  130. 'https://sentry.io/organizations/albertos-apples/issues/?q=123#hash'
  131. );
  132. });
  133. it('renders when window.__initialData.customerDomain and multi-region feature is present', function () {
  134. ConfigStore.loadInitialData({
  135. ...configState,
  136. customerDomain: {
  137. subdomain: 'albertos-apples',
  138. organizationUrl: 'https://albertos-apples.sentry.io',
  139. sentryUrl: 'https://sentry.io',
  140. },
  141. features: new Set(['system:multi-region']),
  142. links: {
  143. organizationUrl: 'https://albertos-apples.sentry.io',
  144. regionUrl: 'https://eu.sentry.io',
  145. sentryUrl: 'https://sentry.io',
  146. },
  147. });
  148. const organization = OrganizationFixture({
  149. slug: 'albertos-apples',
  150. features: [],
  151. });
  152. const params = {
  153. orgId: 'albertos-apples',
  154. };
  155. const {router} = initializeOrg({
  156. organization,
  157. router: {
  158. params,
  159. },
  160. });
  161. const WrappedComponent = withDomainRequired(MyComponent);
  162. render(
  163. <WrappedComponent
  164. router={router}
  165. location={router.location}
  166. params={params}
  167. routes={router.routes}
  168. routeParams={router.params}
  169. route={{}}
  170. />,
  171. {router}
  172. );
  173. expect(screen.getByText('Org slug: albertos-apples')).toBeInTheDocument();
  174. expect(window.location.replace).toHaveBeenCalledTimes(0);
  175. });
  176. });