createProject.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. waitFor,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  10. import {tct} from 'sentry/locale';
  11. import OrganizationStore from 'sentry/stores/organizationStore';
  12. import TeamStore from 'sentry/stores/teamStore';
  13. import {Organization} from 'sentry/types';
  14. import * as useExperiment from 'sentry/utils/useExperiment';
  15. import {CreateProject} from 'sentry/views/projectInstall/createProject';
  16. jest.mock('sentry/actionCreators/indicator');
  17. function renderFrameworkModalMockRequests({
  18. organization,
  19. teamSlug,
  20. }: {
  21. organization: Organization;
  22. teamSlug: string;
  23. }) {
  24. MockApiClient.addMockResponse({
  25. url: `/projects/${organization.slug}/rule-conditions/`,
  26. body: [],
  27. });
  28. MockApiClient.addMockResponse({
  29. url: `/organizations/${organization.slug}/teams/`,
  30. body: [TestStubs.Team({slug: teamSlug})],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: `/organizations/${organization.slug}/`,
  34. body: organization,
  35. });
  36. MockApiClient.addMockResponse({
  37. url: `/organizations/${organization.slug}/projects/`,
  38. body: [],
  39. });
  40. const projectCreationMockRequest = MockApiClient.addMockResponse({
  41. url: `/teams/${organization.slug}/${teamSlug}/projects/`,
  42. method: 'POST',
  43. body: {slug: 'testProj'},
  44. });
  45. const experimentalprojectCreationMockRequest = MockApiClient.addMockResponse({
  46. url: `/organizations/${organization.slug}/experimental/projects/`,
  47. method: 'POST',
  48. body: {slug: 'testProj', team_slug: 'testTeam'},
  49. });
  50. return {projectCreationMockRequest, experimentalprojectCreationMockRequest};
  51. }
  52. describe('CreateProject', function () {
  53. const teamNoAccess = TestStubs.Team({
  54. slug: 'test',
  55. id: '1',
  56. name: 'test',
  57. access: ['team:read'],
  58. });
  59. const teamWithAccess = TestStubs.Team({
  60. access: ['team:admin', 'team:write', 'team:read'],
  61. });
  62. beforeEach(() => {
  63. TeamStore.reset();
  64. TeamStore.loadUserTeams([teamNoAccess]);
  65. MockApiClient.addMockResponse({
  66. url: `/projects/testOrg/rule-conditions/`,
  67. body: {},
  68. // Not required for these tests
  69. statusCode: 500,
  70. });
  71. });
  72. afterEach(() => {
  73. MockApiClient.clearMockResponses();
  74. });
  75. it('should block if you have access to no teams', function () {
  76. render(<CreateProject />, {
  77. context: TestStubs.routerContext([
  78. {organization: {id: '1', slug: 'testOrg', access: ['project:read']}},
  79. ]),
  80. });
  81. });
  82. it('can create a new project without team as org member', async function () {
  83. const {organization} = initializeOrg({
  84. organization: {
  85. access: ['project:read'],
  86. features: ['team-project-creation-all'],
  87. },
  88. });
  89. jest.spyOn(useExperiment, 'useExperiment').mockReturnValue({
  90. experimentAssignment: 1,
  91. logExperiment: jest.fn(),
  92. });
  93. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  94. TeamStore.loadUserTeams([TestStubs.Team({id: 2, slug: 'team-two', access: []})]);
  95. render(<CreateProject />, {
  96. context: TestStubs.routerContext([
  97. {
  98. organization: {
  99. id: '1',
  100. slug: 'testOrg',
  101. access: ['project:read'],
  102. },
  103. },
  104. ]),
  105. organization,
  106. });
  107. renderGlobalModal();
  108. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  109. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  110. expect(createTeamButton).not.toBeInTheDocument();
  111. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  112. });
  113. it('should only allow teams which the user is a team-admin', async function () {
  114. const organization = TestStubs.Organization();
  115. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  116. OrganizationStore.onUpdate(organization);
  117. TeamStore.loadUserTeams([
  118. TestStubs.Team({id: 1, slug: 'team-one', access: []}),
  119. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  120. TestStubs.Team({id: 3, slug: 'team-three', access: ['team:admin']}),
  121. ]);
  122. render(<CreateProject />, {
  123. context: TestStubs.routerContext([{organization}]),
  124. organization,
  125. });
  126. await userEvent.type(screen.getByLabelText('Select a Team'), 'team');
  127. expect(screen.queryByText('#team-one')).not.toBeInTheDocument();
  128. expect(screen.getByText('#team-two')).toBeInTheDocument();
  129. expect(screen.getByText('#team-three')).toBeInTheDocument();
  130. });
  131. it('should fill in project name if its empty when platform is chosen', async function () {
  132. const {organization} = initializeOrg({
  133. organization: {
  134. access: ['project:admin'],
  135. },
  136. });
  137. render(<CreateProject />, {
  138. context: TestStubs.routerContext([
  139. {
  140. organization: {
  141. id: '1',
  142. slug: 'testOrg',
  143. access: ['project:read'],
  144. },
  145. },
  146. ]),
  147. organization,
  148. });
  149. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  150. expect(screen.getByPlaceholderText('project-name')).toHaveValue('apple-ios');
  151. await userEvent.click(screen.getByTestId('platform-ruby-rails'));
  152. expect(screen.getByPlaceholderText('project-name')).toHaveValue('ruby-rails');
  153. // but not replace it when project name is something else:
  154. await userEvent.clear(screen.getByPlaceholderText('project-name'));
  155. await userEvent.type(screen.getByPlaceholderText('project-name'), 'another');
  156. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  157. expect(screen.getByPlaceholderText('project-name')).toHaveValue('another');
  158. });
  159. it('should display success message on proj creation', async function () {
  160. const {organization} = initializeOrg({
  161. organization: {
  162. access: ['project:read'],
  163. },
  164. });
  165. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  166. organization,
  167. teamSlug: teamWithAccess.slug,
  168. });
  169. TeamStore.loadUserTeams([teamWithAccess]);
  170. render(<CreateProject />, {
  171. organization,
  172. });
  173. renderGlobalModal();
  174. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  175. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  176. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  177. 1
  178. );
  179. expect(addSuccessMessage).toHaveBeenCalledWith(
  180. tct('Created project [project]', {
  181. project: 'testProj',
  182. })
  183. );
  184. });
  185. it('should display error message on proj creation failure', async function () {
  186. const {organization} = initializeOrg({
  187. organization: {
  188. access: ['project:read'],
  189. },
  190. });
  191. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  192. organization,
  193. teamSlug: teamWithAccess.slug,
  194. });
  195. frameWorkModalMockRequests.projectCreationMockRequest = MockApiClient.addMockResponse(
  196. {
  197. url: `/teams/${organization.slug}/${teamWithAccess.slug}/projects/`,
  198. method: 'POST',
  199. body: {slug: 'testProj'},
  200. statusCode: 404,
  201. }
  202. );
  203. TeamStore.loadUserTeams([teamWithAccess]);
  204. render(<CreateProject />, {
  205. organization,
  206. });
  207. renderGlobalModal();
  208. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  209. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  210. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  211. 1
  212. );
  213. expect(addErrorMessage).toHaveBeenCalledWith(
  214. tct('Failed to create project [project]', {
  215. project: 'apple-ios',
  216. })
  217. );
  218. });
  219. it('should display success message when using experimental endpoint', async function () {
  220. const {organization} = initializeOrg({
  221. organization: {
  222. access: ['project:read'],
  223. features: ['team-project-creation-all'],
  224. },
  225. });
  226. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  227. organization,
  228. teamSlug: teamNoAccess.slug,
  229. });
  230. render(<CreateProject />, {
  231. context: TestStubs.routerContext([
  232. {
  233. organization: {
  234. id: '1',
  235. slug: 'testOrg',
  236. access: ['project:read'],
  237. },
  238. },
  239. ]),
  240. organization,
  241. });
  242. renderGlobalModal();
  243. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  244. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  245. expect(
  246. frameWorkModalMockRequests.experimentalprojectCreationMockRequest
  247. ).toHaveBeenCalledTimes(1);
  248. expect(addSuccessMessage).toHaveBeenCalledWith(
  249. tct('Created [project] under new team [team]', {
  250. project: 'testProj',
  251. team: '#testTeam',
  252. })
  253. );
  254. });
  255. it('does not render framework selection modal if vanilla js is NOT selected', async function () {
  256. const {organization} = initializeOrg({
  257. organization: {
  258. features: ['onboarding-sdk-selection'],
  259. access: ['project:read', 'project:write'],
  260. },
  261. });
  262. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  263. organization,
  264. teamSlug: teamWithAccess.slug,
  265. });
  266. TeamStore.loadUserTeams([teamWithAccess]);
  267. OrganizationStore.onUpdate(organization, {replace: true});
  268. render(<CreateProject />, {
  269. organization,
  270. });
  271. // Select the React platform
  272. await userEvent.click(screen.getByTestId('platform-javascript-react'));
  273. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  274. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  275. await waitFor(() => {
  276. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  277. });
  278. renderGlobalModal();
  279. // Click on 'configure SDK' button
  280. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  281. // Modal shall not be open
  282. expect(screen.queryByText('Do you use a framework?')).not.toBeInTheDocument();
  283. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalled();
  284. });
  285. it('renders framework selection modal if vanilla js is selected', async function () {
  286. const {organization} = initializeOrg({
  287. organization: {
  288. features: ['onboarding-sdk-selection'],
  289. },
  290. });
  291. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  292. organization,
  293. teamSlug: teamWithAccess.slug,
  294. });
  295. TeamStore.loadUserTeams([teamWithAccess]);
  296. OrganizationStore.onUpdate(organization, {replace: true});
  297. render(<CreateProject />, {
  298. organization,
  299. });
  300. // Select the JavaScript platform
  301. await userEvent.click(screen.getByTestId('platform-javascript'));
  302. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  303. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  304. await waitFor(() => {
  305. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  306. });
  307. renderGlobalModal();
  308. // Click on 'configure SDK' button
  309. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  310. // Modal is open
  311. await screen.findByText('Do you use a framework?');
  312. // Close modal
  313. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  314. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  315. });
  316. describe('Issue Alerts Options', function () {
  317. const organization = TestStubs.Organization();
  318. beforeEach(() => {
  319. TeamStore.loadUserTeams([teamWithAccess]);
  320. MockApiClient.addMockResponse({
  321. url: `/projects/${organization.slug}/rule-conditions/`,
  322. body: TestStubs.MOCK_RESP_VERBOSE,
  323. });
  324. });
  325. afterEach(() => {
  326. MockApiClient.clearMockResponses();
  327. });
  328. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  329. render(<CreateProject />);
  330. // We need to query for the submit button every time we want to access it
  331. // as re-renders can create new DOM nodes
  332. const getSubmitButton = () => screen.getByRole('button', {name: 'Create Project'});
  333. expect(getSubmitButton()).toBeDisabled();
  334. // Selecting the platform pre-fills the project name
  335. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  336. expect(getSubmitButton()).toBeEnabled();
  337. await userEvent.click(screen.getByText(/When there are more than/));
  338. expect(getSubmitButton()).toBeEnabled();
  339. await userEvent.clear(screen.getByTestId('range-input'));
  340. expect(getSubmitButton()).toBeDisabled();
  341. await userEvent.type(screen.getByTestId('range-input'), '2712');
  342. expect(getSubmitButton()).toBeEnabled();
  343. await userEvent.clear(screen.getByTestId('range-input'));
  344. expect(getSubmitButton()).toBeDisabled();
  345. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  346. expect(getSubmitButton()).toBeEnabled();
  347. });
  348. });
  349. });