createProject.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import {MOCK_RESP_VERBOSE} from 'sentry-fixture/ruleConditions';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  11. import {tct} from 'sentry/locale';
  12. import OrganizationStore from 'sentry/stores/organizationStore';
  13. import TeamStore from 'sentry/stores/teamStore';
  14. import {Organization} from 'sentry/types';
  15. import * as useExperiment from 'sentry/utils/useExperiment';
  16. import {CreateProject} from 'sentry/views/projectInstall/createProject';
  17. jest.mock('sentry/actionCreators/indicator');
  18. function renderFrameworkModalMockRequests({
  19. organization,
  20. teamSlug,
  21. }: {
  22. organization: Organization;
  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', 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 without team as org member', async function () {
  84. const {organization} = initializeOrg({
  85. organization: {
  86. access: ['project:read'],
  87. features: ['team-project-creation-all'],
  88. },
  89. });
  90. jest.spyOn(useExperiment, 'useExperiment').mockReturnValue({
  91. experimentAssignment: 1,
  92. logExperiment: jest.fn(),
  93. });
  94. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  95. TeamStore.loadUserTeams([TestStubs.Team({id: 2, slug: 'team-two', access: []})]);
  96. render(<CreateProject />, {
  97. context: TestStubs.routerContext([
  98. {
  99. organization: {
  100. id: '1',
  101. slug: 'testOrg',
  102. access: ['project:read'],
  103. },
  104. },
  105. ]),
  106. organization,
  107. });
  108. renderGlobalModal();
  109. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  110. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  111. expect(createTeamButton).not.toBeInTheDocument();
  112. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  113. });
  114. it('should only allow teams which the user is a team-admin', async function () {
  115. const organization = TestStubs.Organization();
  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. },
  165. });
  166. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  167. organization,
  168. teamSlug: teamWithAccess.slug,
  169. });
  170. TeamStore.loadUserTeams([teamWithAccess]);
  171. render(<CreateProject />, {
  172. organization,
  173. });
  174. renderGlobalModal();
  175. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  176. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  177. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  178. 1
  179. );
  180. expect(addSuccessMessage).toHaveBeenCalledWith(
  181. tct('Created project [project]', {
  182. project: 'testProj',
  183. })
  184. );
  185. });
  186. it('should display error message on proj creation failure', async function () {
  187. const {organization} = initializeOrg({
  188. organization: {
  189. access: ['project:read'],
  190. },
  191. });
  192. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  193. organization,
  194. teamSlug: teamWithAccess.slug,
  195. });
  196. frameWorkModalMockRequests.projectCreationMockRequest = MockApiClient.addMockResponse(
  197. {
  198. url: `/teams/${organization.slug}/${teamWithAccess.slug}/projects/`,
  199. method: 'POST',
  200. body: {slug: 'testProj'},
  201. statusCode: 404,
  202. }
  203. );
  204. TeamStore.loadUserTeams([teamWithAccess]);
  205. render(<CreateProject />, {
  206. organization,
  207. });
  208. renderGlobalModal();
  209. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  210. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  211. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  212. 1
  213. );
  214. expect(addErrorMessage).toHaveBeenCalledWith(
  215. tct('Failed to create project [project]', {
  216. project: 'apple-ios',
  217. })
  218. );
  219. });
  220. it('should display success message when using experimental endpoint', async function () {
  221. const {organization} = initializeOrg({
  222. organization: {
  223. access: ['project:read'],
  224. features: ['team-project-creation-all'],
  225. },
  226. });
  227. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  228. organization,
  229. teamSlug: teamNoAccess.slug,
  230. });
  231. render(<CreateProject />, {
  232. context: TestStubs.routerContext([
  233. {
  234. organization: {
  235. id: '1',
  236. slug: 'testOrg',
  237. access: ['project:read'],
  238. },
  239. },
  240. ]),
  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: ['onboarding-sdk-selection'],
  260. access: ['project:read', 'project:write'],
  261. },
  262. });
  263. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  264. organization,
  265. teamSlug: teamWithAccess.slug,
  266. });
  267. TeamStore.loadUserTeams([teamWithAccess]);
  268. OrganizationStore.onUpdate(organization, {replace: true});
  269. render(<CreateProject />, {
  270. organization,
  271. });
  272. // Select the React platform
  273. await userEvent.click(screen.getByTestId('platform-javascript-react'));
  274. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  275. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  276. await waitFor(() => {
  277. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  278. });
  279. renderGlobalModal();
  280. // Click on 'configure SDK' button
  281. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  282. // Modal shall not be open
  283. expect(screen.queryByText('Do you use a framework?')).not.toBeInTheDocument();
  284. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalled();
  285. });
  286. it('renders framework selection modal if vanilla js is selected', async function () {
  287. const {organization} = initializeOrg({
  288. organization: {
  289. features: ['onboarding-sdk-selection'],
  290. },
  291. });
  292. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  293. organization,
  294. teamSlug: teamWithAccess.slug,
  295. });
  296. TeamStore.loadUserTeams([teamWithAccess]);
  297. OrganizationStore.onUpdate(organization, {replace: true});
  298. render(<CreateProject />, {
  299. organization,
  300. });
  301. // Select the JavaScript platform
  302. await userEvent.click(screen.getByTestId('platform-javascript'));
  303. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  304. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  305. await waitFor(() => {
  306. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  307. });
  308. renderGlobalModal();
  309. // Click on 'configure SDK' button
  310. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  311. // Modal is open
  312. await screen.findByText('Do you use a framework?');
  313. // Close modal
  314. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  315. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  316. });
  317. describe('Issue Alerts Options', function () {
  318. const organization = TestStubs.Organization();
  319. beforeEach(() => {
  320. TeamStore.loadUserTeams([teamWithAccess]);
  321. MockApiClient.addMockResponse({
  322. url: `/projects/${organization.slug}/rule-conditions/`,
  323. body: MOCK_RESP_VERBOSE,
  324. });
  325. });
  326. afterEach(() => {
  327. MockApiClient.clearMockResponses();
  328. });
  329. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  330. render(<CreateProject />);
  331. // We need to query for the submit button every time we want to access it
  332. // as re-renders can create new DOM nodes
  333. const getSubmitButton = () => screen.getByRole('button', {name: 'Create Project'});
  334. expect(getSubmitButton()).toBeDisabled();
  335. // Selecting the platform pre-fills the project name
  336. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  337. expect(getSubmitButton()).toBeEnabled();
  338. await userEvent.click(screen.getByText(/When there are more than/));
  339. expect(getSubmitButton()).toBeEnabled();
  340. await userEvent.clear(screen.getByTestId('range-input'));
  341. expect(getSubmitButton()).toBeDisabled();
  342. await userEvent.type(screen.getByTestId('range-input'), '2712');
  343. expect(getSubmitButton()).toBeEnabled();
  344. await userEvent.clear(screen.getByTestId('range-input'));
  345. expect(getSubmitButton()).toBeDisabled();
  346. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  347. expect(getSubmitButton()).toBeEnabled();
  348. });
  349. });
  350. });