createProject.spec.tsx 13 KB

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