index.spec.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import selectEvent from 'react-select-event';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import OrganizationCreate from 'sentry/views/organizationCreate';
  6. describe('OrganizationCreate', function () {
  7. let oldRegions: any[] = [];
  8. beforeEach(() => {
  9. ConfigStore.get('termsUrl');
  10. ConfigStore.get('privacyUrl');
  11. oldRegions = ConfigStore.get('regions');
  12. // Set only a single region in the config store by default
  13. ConfigStore.set('regions', [{name: '--monolith--', url: 'https://example.com'}]);
  14. });
  15. afterEach(() => {
  16. MockApiClient.clearMockResponses();
  17. jest.resetAllMocks();
  18. ConfigStore.set('regions', oldRegions);
  19. });
  20. it('renders without terms', function () {
  21. render(<OrganizationCreate />);
  22. });
  23. it('renders with terms', function () {
  24. ConfigStore.set('termsUrl', 'https://example.com/terms');
  25. ConfigStore.set('privacyUrl', 'https://example.com/privacy');
  26. render(<OrganizationCreate />);
  27. });
  28. it('creates a new org', async function () {
  29. const orgCreateMock = MockApiClient.addMockResponse({
  30. url: '/organizations/',
  31. method: 'POST',
  32. body: Organization(),
  33. });
  34. ConfigStore.set('termsUrl', 'https://example.com/terms');
  35. ConfigStore.set('privacyUrl', 'https://example.com/privacy');
  36. render(<OrganizationCreate />);
  37. expect(screen.getByText('Create a New Organization')).toBeInTheDocument();
  38. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  39. await userEvent.click(
  40. screen.getByRole('checkbox', {
  41. name: 'I agree to the Terms of Service and the Privacy Policy',
  42. })
  43. );
  44. await userEvent.click(screen.getByText('Create Organization'));
  45. await waitFor(() => {
  46. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  47. success: expect.any(Function),
  48. error: expect.any(Function),
  49. method: 'POST',
  50. data: {agreeTerms: true, defaultTeam: true, name: 'Good Burger'},
  51. host: undefined,
  52. });
  53. });
  54. expect(window.location.assign).toHaveBeenCalledTimes(1);
  55. expect(window.location.assign).toHaveBeenCalledWith(
  56. '/organizations/org-slug/projects/new/'
  57. );
  58. });
  59. it('creates a new org with customer domain feature', async function () {
  60. const orgCreateMock = MockApiClient.addMockResponse({
  61. url: '/organizations/',
  62. method: 'POST',
  63. body: Organization({
  64. features: ['customer-domains'],
  65. }),
  66. });
  67. ConfigStore.set('termsUrl', 'https://example.com/terms');
  68. ConfigStore.set('privacyUrl', 'https://example.com/privacy');
  69. render(<OrganizationCreate />);
  70. expect(screen.getByText('Create a New Organization')).toBeInTheDocument();
  71. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  72. await userEvent.click(
  73. screen.getByRole('checkbox', {
  74. name: 'I agree to the Terms of Service and the Privacy Policy',
  75. })
  76. );
  77. await userEvent.click(screen.getByText('Create Organization'));
  78. await waitFor(() => {
  79. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  80. data: {agreeTerms: true, defaultTeam: true, name: 'Good Burger'},
  81. method: 'POST',
  82. success: expect.any(Function),
  83. error: expect.any(Function),
  84. host: undefined,
  85. });
  86. });
  87. expect(window.location.assign).toHaveBeenCalledTimes(1);
  88. expect(window.location.assign).toHaveBeenCalledWith(
  89. 'https://org-slug.sentry.io/projects/new/'
  90. );
  91. });
  92. function multiRegionSetup() {
  93. const orgCreateMock = MockApiClient.addMockResponse({
  94. url: '/organizations/',
  95. method: 'POST',
  96. body: Organization({
  97. features: ['customer-domains'],
  98. }),
  99. });
  100. ConfigStore.set('features', new Set(['organizations:multi-region-selector']));
  101. ConfigStore.set('regions', [
  102. {url: 'https://us.example.com', name: 'us'},
  103. {
  104. url: 'https://de.example.com',
  105. name: 'de',
  106. },
  107. ]);
  108. return orgCreateMock;
  109. }
  110. it('renders without region data and submits without host when only a single region is defined', async function () {
  111. const orgCreateMock = multiRegionSetup();
  112. // Set only a single region in the config store
  113. ConfigStore.set('regions', [{name: '--monolith--', url: 'https://example.com'}]);
  114. render(<OrganizationCreate />);
  115. expect(screen.queryByLabelText('Data Storage')).not.toBeInTheDocument();
  116. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  117. await userEvent.click(screen.getByText('Create Organization'));
  118. await waitFor(() => {
  119. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  120. success: expect.any(Function),
  121. error: expect.any(Function),
  122. method: 'POST',
  123. host: undefined,
  124. data: {defaultTeam: true, name: 'Good Burger'},
  125. });
  126. });
  127. expect(window.location.assign).toHaveBeenCalledTimes(1);
  128. expect(window.location.assign).toHaveBeenCalledWith(
  129. 'https://org-slug.sentry.io/projects/new/'
  130. );
  131. });
  132. it('renders with region data and selects US region as default when the feature flag is enabled', async function () {
  133. const orgCreateMock = multiRegionSetup();
  134. render(<OrganizationCreate />);
  135. expect(screen.getByLabelText('Data Storage')).toBeInTheDocument();
  136. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  137. await userEvent.click(screen.getByText('Create Organization'));
  138. const expectedHost = 'https://us.example.com';
  139. await waitFor(() => {
  140. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  141. success: expect.any(Function),
  142. error: expect.any(Function),
  143. method: 'POST',
  144. host: expectedHost,
  145. data: {defaultTeam: true, name: 'Good Burger'},
  146. });
  147. });
  148. expect(window.location.assign).toHaveBeenCalledTimes(1);
  149. expect(window.location.assign).toHaveBeenCalledWith(
  150. 'https://org-slug.sentry.io/projects/new/'
  151. );
  152. });
  153. it('renders without region data and submits without host when the feature flag is not enabled', async function () {
  154. const orgCreateMock = multiRegionSetup();
  155. ConfigStore.set('features', new Set());
  156. render(<OrganizationCreate />);
  157. expect(screen.queryByLabelText('Data Storage')).not.toBeInTheDocument();
  158. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  159. await userEvent.click(screen.getByText('Create Organization'));
  160. await waitFor(() => {
  161. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  162. success: expect.any(Function),
  163. error: expect.any(Function),
  164. method: 'POST',
  165. host: undefined,
  166. data: {defaultTeam: true, name: 'Good Burger'},
  167. });
  168. });
  169. expect(window.location.assign).toHaveBeenCalledTimes(1);
  170. expect(window.location.assign).toHaveBeenCalledWith(
  171. 'https://org-slug.sentry.io/projects/new/'
  172. );
  173. });
  174. it('uses the host of the selected region when submitting', async function () {
  175. const orgCreateMock = multiRegionSetup();
  176. render(<OrganizationCreate />);
  177. expect(screen.getByLabelText('Data Storage')).toBeInTheDocument();
  178. await userEvent.type(screen.getByPlaceholderText('e.g. My Company'), 'Good Burger');
  179. await selectEvent.select(
  180. screen.getByRole('textbox', {name: 'Data Storage'}),
  181. '🇪🇺 European Union (EU)'
  182. );
  183. await userEvent.click(screen.getByText('Create Organization'));
  184. const expectedHost = 'https://de.example.com';
  185. await waitFor(() => {
  186. expect(orgCreateMock).toHaveBeenCalledWith('/organizations/', {
  187. success: expect.any(Function),
  188. error: expect.any(Function),
  189. method: 'POST',
  190. host: expectedHost,
  191. data: {defaultTeam: true, name: 'Good Burger'},
  192. });
  193. });
  194. expect(window.location.assign).toHaveBeenCalledTimes(1);
  195. expect(window.location.assign).toHaveBeenCalledWith(
  196. 'https://org-slug.sentry.io/projects/new/'
  197. );
  198. });
  199. });