createProject.spec.tsx 14 KB

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