createProject.spec.tsx 13 KB

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