createProject.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. const {container} = render(<CreateProject />, {
  77. context: TestStubs.routerContext([
  78. {organization: {id: '1', slug: 'testOrg', access: ['project:read']}},
  79. ]),
  80. });
  81. expect(container).toSnapshot();
  82. });
  83. it('can create a new team as admin', async function () {
  84. const {organization} = initializeOrg({
  85. organization: {
  86. access: ['project:admin'],
  87. },
  88. });
  89. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  90. TeamStore.loadUserTeams([
  91. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  92. ]);
  93. render(<CreateProject />, {
  94. context: TestStubs.routerContext([
  95. {
  96. organization: {
  97. id: '1',
  98. slug: 'testOrg',
  99. access: ['project:read'],
  100. },
  101. },
  102. ]),
  103. organization,
  104. });
  105. renderGlobalModal();
  106. await userEvent.click(screen.getByRole('button', {name: 'Create a team'}));
  107. expect(
  108. await screen.findByText(
  109. 'Members of a team have access to specific areas, such as a new release or a new application feature.'
  110. )
  111. ).toBeInTheDocument();
  112. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  113. });
  114. it('can create a new project without team as org member', async function () {
  115. const {organization} = initializeOrg({
  116. organization: {
  117. access: ['project:read'],
  118. features: ['team-project-creation-all'],
  119. },
  120. });
  121. jest.spyOn(useExperiment, 'useExperiment').mockReturnValue({
  122. experimentAssignment: 1,
  123. logExperiment: jest.fn(),
  124. });
  125. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  126. TeamStore.loadUserTeams([TestStubs.Team({id: 2, slug: 'team-two', access: []})]);
  127. render(<CreateProject />, {
  128. context: TestStubs.routerContext([
  129. {
  130. organization: {
  131. id: '1',
  132. slug: 'testOrg',
  133. access: ['project:read'],
  134. },
  135. },
  136. ]),
  137. organization,
  138. });
  139. renderGlobalModal();
  140. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  141. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  142. expect(createTeamButton).not.toBeInTheDocument();
  143. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  144. });
  145. it('can create a new team before project creation if org owner', async function () {
  146. const {organization} = initializeOrg({
  147. organization: {
  148. access: ['project:admin'],
  149. },
  150. });
  151. render(<CreateProject />, {
  152. context: TestStubs.routerContext([
  153. {
  154. organization: {
  155. id: '1',
  156. slug: 'testOrg',
  157. access: ['project:read'],
  158. },
  159. },
  160. ]),
  161. organization,
  162. });
  163. renderGlobalModal();
  164. await userEvent.click(screen.getByRole('button', {name: 'Create a team'}));
  165. expect(
  166. await screen.findByText(
  167. 'Members of a team have access to specific areas, such as a new release or a new application feature.'
  168. )
  169. ).toBeInTheDocument();
  170. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  171. });
  172. it('should not show create team button to team-admin with no org access', function () {
  173. const {organization} = initializeOrg({
  174. organization: {
  175. access: ['project:read'],
  176. },
  177. });
  178. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  179. OrganizationStore.onUpdate(organization);
  180. TeamStore.loadUserTeams([
  181. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  182. ]);
  183. render(<CreateProject />, {
  184. context: TestStubs.routerContext([{organization}]),
  185. organization,
  186. });
  187. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  188. expect(createTeamButton).not.toBeInTheDocument();
  189. });
  190. it('should only allow teams which the user is a team-admin', async function () {
  191. const organization = TestStubs.Organization();
  192. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  193. OrganizationStore.onUpdate(organization);
  194. TeamStore.loadUserTeams([
  195. TestStubs.Team({id: 1, slug: 'team-one', access: []}),
  196. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  197. TestStubs.Team({id: 3, slug: 'team-three', access: ['team:admin']}),
  198. ]);
  199. render(<CreateProject />, {
  200. context: TestStubs.routerContext([{organization}]),
  201. organization,
  202. });
  203. await userEvent.type(screen.getByLabelText('Select a Team'), 'team');
  204. expect(screen.queryByText('#team-one')).not.toBeInTheDocument();
  205. expect(screen.getByText('#team-two')).toBeInTheDocument();
  206. expect(screen.getByText('#team-three')).toBeInTheDocument();
  207. });
  208. it('should fill in project name if its empty when platform is chosen', async function () {
  209. const {organization} = initializeOrg({
  210. organization: {
  211. access: ['project:admin'],
  212. },
  213. });
  214. const {container} = render(<CreateProject />, {
  215. context: TestStubs.routerContext([
  216. {
  217. organization: {
  218. id: '1',
  219. slug: 'testOrg',
  220. access: ['project:read'],
  221. },
  222. },
  223. ]),
  224. organization,
  225. });
  226. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  227. expect(screen.getByPlaceholderText('project-name')).toHaveValue('apple-ios');
  228. await userEvent.click(screen.getByTestId('platform-ruby-rails'));
  229. expect(screen.getByPlaceholderText('project-name')).toHaveValue('ruby-rails');
  230. // but not replace it when project name is something else:
  231. await userEvent.clear(screen.getByPlaceholderText('project-name'));
  232. await userEvent.type(screen.getByPlaceholderText('project-name'), 'another');
  233. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  234. expect(screen.getByPlaceholderText('project-name')).toHaveValue('another');
  235. expect(container).toSnapshot();
  236. });
  237. it('should display success message on proj creation', async function () {
  238. const {organization} = initializeOrg({
  239. organization: {
  240. access: ['project:read'],
  241. },
  242. });
  243. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  244. organization,
  245. teamSlug: teamWithAccess.slug,
  246. });
  247. TeamStore.loadUserTeams([teamWithAccess]);
  248. render(<CreateProject />, {
  249. organization,
  250. });
  251. renderGlobalModal();
  252. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  253. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  254. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  255. 1
  256. );
  257. expect(addSuccessMessage).toHaveBeenCalledWith(
  258. tct('Created project [project]', {
  259. project: 'testProj',
  260. })
  261. );
  262. });
  263. it('should display error message on proj creation failure', async function () {
  264. const {organization} = initializeOrg({
  265. organization: {
  266. access: ['project:read'],
  267. },
  268. });
  269. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  270. organization,
  271. teamSlug: teamWithAccess.slug,
  272. });
  273. frameWorkModalMockRequests.projectCreationMockRequest = MockApiClient.addMockResponse(
  274. {
  275. url: `/teams/${organization.slug}/${teamWithAccess.slug}/projects/`,
  276. method: 'POST',
  277. body: {slug: 'testProj'},
  278. statusCode: 404,
  279. }
  280. );
  281. TeamStore.loadUserTeams([teamWithAccess]);
  282. render(<CreateProject />, {
  283. organization,
  284. });
  285. renderGlobalModal();
  286. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  287. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  288. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  289. 1
  290. );
  291. expect(addErrorMessage).toHaveBeenCalledWith(
  292. tct('Failed to create project [project]', {
  293. project: 'apple-ios',
  294. })
  295. );
  296. });
  297. it('should display success message when using experimental endpoint', async function () {
  298. const {organization} = initializeOrg({
  299. organization: {
  300. access: ['project:read'],
  301. features: ['team-project-creation-all'],
  302. },
  303. });
  304. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  305. organization,
  306. teamSlug: teamNoAccess.slug,
  307. });
  308. render(<CreateProject />, {
  309. context: TestStubs.routerContext([
  310. {
  311. organization: {
  312. id: '1',
  313. slug: 'testOrg',
  314. access: ['project:read'],
  315. },
  316. },
  317. ]),
  318. organization,
  319. });
  320. renderGlobalModal();
  321. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  322. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  323. expect(
  324. frameWorkModalMockRequests.experimentalprojectCreationMockRequest
  325. ).toHaveBeenCalledTimes(1);
  326. expect(addSuccessMessage).toHaveBeenCalledWith(
  327. tct('Created [project] under new team [team]', {
  328. project: 'testProj',
  329. team: '#testTeam',
  330. })
  331. );
  332. });
  333. it('does not render framework selection modal if vanilla js is NOT selected', async function () {
  334. const {organization} = initializeOrg({
  335. organization: {
  336. features: ['onboarding-sdk-selection'],
  337. access: ['project:read', 'project:write'],
  338. },
  339. });
  340. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  341. organization,
  342. teamSlug: teamWithAccess.slug,
  343. });
  344. TeamStore.loadUserTeams([teamWithAccess]);
  345. OrganizationStore.onUpdate(organization, {replace: true});
  346. render(<CreateProject />, {
  347. organization,
  348. });
  349. // Select the React platform
  350. await userEvent.click(screen.getByTestId('platform-javascript-react'));
  351. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  352. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  353. await waitFor(() => {
  354. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  355. });
  356. renderGlobalModal();
  357. // Click on 'configure SDK' button
  358. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  359. // Modal shall not be open
  360. expect(screen.queryByText('Do you use a framework?')).not.toBeInTheDocument();
  361. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalled();
  362. });
  363. it('renders framework selection modal if vanilla js is selected', async function () {
  364. const {organization} = initializeOrg({
  365. organization: {
  366. features: ['onboarding-sdk-selection'],
  367. },
  368. });
  369. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  370. organization,
  371. teamSlug: teamWithAccess.slug,
  372. });
  373. TeamStore.loadUserTeams([teamWithAccess]);
  374. OrganizationStore.onUpdate(organization, {replace: true});
  375. render(<CreateProject />, {
  376. organization,
  377. });
  378. // Select the JavaScript platform
  379. await userEvent.click(screen.getByTestId('platform-javascript'));
  380. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  381. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  382. await waitFor(() => {
  383. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  384. });
  385. renderGlobalModal();
  386. // Click on 'configure SDK' button
  387. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  388. // Modal is open
  389. await screen.findByText('Do you use a framework?');
  390. // Close modal
  391. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  392. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  393. });
  394. describe('Issue Alerts Options', function () {
  395. const organization = TestStubs.Organization();
  396. beforeEach(() => {
  397. TeamStore.loadUserTeams([teamWithAccess]);
  398. MockApiClient.addMockResponse({
  399. url: `/projects/${organization.slug}/rule-conditions/`,
  400. body: TestStubs.MOCK_RESP_VERBOSE,
  401. });
  402. });
  403. afterEach(() => {
  404. MockApiClient.clearMockResponses();
  405. });
  406. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  407. render(<CreateProject />);
  408. const createProjectButton = screen.getByRole('button', {name: 'Create Project'});
  409. await userEvent.click(screen.getByText(/When there are more than/));
  410. expect(createProjectButton).toBeDisabled();
  411. await userEvent.type(screen.getByTestId('range-input'), '2');
  412. expect(screen.getByTestId('range-input')).toHaveValue(2);
  413. expect(createProjectButton).toBeDisabled();
  414. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  415. expect(createProjectButton).toBeEnabled();
  416. await userEvent.clear(screen.getByTestId('range-input'));
  417. expect(createProjectButton).toBeDisabled();
  418. await userEvent.type(screen.getByTestId('range-input'), '2712');
  419. expect(createProjectButton).toBeEnabled();
  420. await userEvent.clear(screen.getByTestId('range-input'));
  421. expect(createProjectButton).toBeDisabled();
  422. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  423. expect(createProjectButton).toBeEnabled();
  424. });
  425. });
  426. });