createProject.spec.tsx 13 KB

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