createProject.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. waitFor,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  10. import {tct} from 'sentry/locale';
  11. import OrganizationStore from 'sentry/stores/organizationStore';
  12. import TeamStore from 'sentry/stores/teamStore';
  13. import {Organization} from 'sentry/types';
  14. import * as useExperiment from 'sentry/utils/useExperiment';
  15. import {CreateProject} from 'sentry/views/projectInstall/createProject';
  16. jest.mock('sentry/actionCreators/indicator');
  17. function renderFrameworkModalMockRequests({
  18. organization,
  19. teamSlug,
  20. }: {
  21. organization: Organization;
  22. teamSlug: string;
  23. }) {
  24. MockApiClient.addMockResponse({
  25. url: `/projects/${organization.slug}/rule-conditions/`,
  26. body: [],
  27. });
  28. MockApiClient.addMockResponse({
  29. url: `/organizations/${organization.slug}/teams/`,
  30. body: [TestStubs.Team({slug: teamSlug})],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: `/organizations/${organization.slug}/`,
  34. body: organization,
  35. });
  36. MockApiClient.addMockResponse({
  37. url: `/organizations/${organization.slug}/projects/`,
  38. body: [],
  39. });
  40. const projectCreationMockRequest = MockApiClient.addMockResponse({
  41. url: `/teams/${organization.slug}/${teamSlug}/projects/`,
  42. method: 'POST',
  43. body: {slug: 'testProj'},
  44. });
  45. const experimentalprojectCreationMockRequest = MockApiClient.addMockResponse({
  46. url: `/organizations/${organization.slug}/experimental/projects/`,
  47. method: 'POST',
  48. body: {slug: 'testProj', team_slug: 'testTeam'},
  49. });
  50. return {projectCreationMockRequest, experimentalprojectCreationMockRequest};
  51. }
  52. describe('CreateProject', function () {
  53. const teamNoAccess = TestStubs.Team({
  54. slug: 'test',
  55. id: '1',
  56. name: 'test',
  57. access: ['team:read'],
  58. });
  59. const teamWithAccess = TestStubs.Team({
  60. access: ['team:admin', 'team:write', 'team:read'],
  61. });
  62. beforeEach(() => {
  63. TeamStore.reset();
  64. TeamStore.loadUserTeams([teamNoAccess]);
  65. MockApiClient.addMockResponse({
  66. url: `/projects/testOrg/rule-conditions/`,
  67. body: {},
  68. // Not required for these tests
  69. statusCode: 500,
  70. });
  71. });
  72. afterEach(() => {
  73. MockApiClient.clearMockResponses();
  74. });
  75. it('should block if you have access to no teams', function () {
  76. const {container} = render(<CreateProject />, {
  77. context: TestStubs.routerContext([
  78. {organization: {id: '1', slug: 'testOrg', access: ['project:read']}},
  79. ]),
  80. });
  81. expect(container).toSnapshot();
  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. const {container} = 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. expect(container).toSnapshot();
  160. });
  161. it('should display success message on proj creation', async function () {
  162. const {organization} = initializeOrg({
  163. organization: {
  164. access: ['project:read'],
  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. },
  192. });
  193. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  194. organization,
  195. teamSlug: teamWithAccess.slug,
  196. });
  197. frameWorkModalMockRequests.projectCreationMockRequest = MockApiClient.addMockResponse(
  198. {
  199. url: `/teams/${organization.slug}/${teamWithAccess.slug}/projects/`,
  200. method: 'POST',
  201. body: {slug: 'testProj'},
  202. statusCode: 404,
  203. }
  204. );
  205. TeamStore.loadUserTeams([teamWithAccess]);
  206. render(<CreateProject />, {
  207. organization,
  208. });
  209. renderGlobalModal();
  210. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  211. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  212. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalledTimes(
  213. 1
  214. );
  215. expect(addErrorMessage).toHaveBeenCalledWith(
  216. tct('Failed to create project [project]', {
  217. project: 'apple-ios',
  218. })
  219. );
  220. });
  221. it('should display success message when using experimental endpoint', async function () {
  222. const {organization} = initializeOrg({
  223. organization: {
  224. access: ['project:read'],
  225. features: ['team-project-creation-all'],
  226. },
  227. });
  228. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  229. organization,
  230. teamSlug: teamNoAccess.slug,
  231. });
  232. render(<CreateProject />, {
  233. context: TestStubs.routerContext([
  234. {
  235. organization: {
  236. id: '1',
  237. slug: 'testOrg',
  238. access: ['project:read'],
  239. },
  240. },
  241. ]),
  242. organization,
  243. });
  244. renderGlobalModal();
  245. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  246. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  247. expect(
  248. frameWorkModalMockRequests.experimentalprojectCreationMockRequest
  249. ).toHaveBeenCalledTimes(1);
  250. expect(addSuccessMessage).toHaveBeenCalledWith(
  251. tct('Created [project] under new team [team]', {
  252. project: 'testProj',
  253. team: '#testTeam',
  254. })
  255. );
  256. });
  257. it('does not render framework selection modal if vanilla js is NOT selected', async function () {
  258. const {organization} = initializeOrg({
  259. organization: {
  260. features: ['onboarding-sdk-selection'],
  261. access: ['project:read', 'project:write'],
  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. organization: {
  290. features: ['onboarding-sdk-selection'],
  291. },
  292. });
  293. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  294. organization,
  295. teamSlug: teamWithAccess.slug,
  296. });
  297. TeamStore.loadUserTeams([teamWithAccess]);
  298. OrganizationStore.onUpdate(organization, {replace: true});
  299. render(<CreateProject />, {
  300. organization,
  301. });
  302. // Select the JavaScript platform
  303. await userEvent.click(screen.getByTestId('platform-javascript'));
  304. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  305. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  306. await waitFor(() => {
  307. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  308. });
  309. renderGlobalModal();
  310. // Click on 'configure SDK' button
  311. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  312. // Modal is open
  313. await screen.findByText('Do you use a framework?');
  314. // Close modal
  315. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  316. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  317. });
  318. describe('Issue Alerts Options', function () {
  319. const organization = TestStubs.Organization();
  320. beforeEach(() => {
  321. TeamStore.loadUserTeams([teamWithAccess]);
  322. MockApiClient.addMockResponse({
  323. url: `/projects/${organization.slug}/rule-conditions/`,
  324. body: TestStubs.MOCK_RESP_VERBOSE,
  325. });
  326. });
  327. afterEach(() => {
  328. MockApiClient.clearMockResponses();
  329. });
  330. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  331. render(<CreateProject />);
  332. // We need to query for the submit button every time we want to access it
  333. // as re-renders can create new DOM nodes
  334. const getSubmitButton = () => screen.getByRole('button', {name: 'Create Project'});
  335. expect(getSubmitButton()).toBeDisabled();
  336. // Selecting the platform pre-fills the project name
  337. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  338. expect(getSubmitButton()).toBeEnabled();
  339. await userEvent.click(screen.getByText(/When there are more than/));
  340. expect(getSubmitButton()).toBeEnabled();
  341. await userEvent.clear(screen.getByTestId('range-input'));
  342. expect(getSubmitButton()).toBeDisabled();
  343. await userEvent.type(screen.getByTestId('range-input'), '2712');
  344. expect(getSubmitButton()).toBeEnabled();
  345. await userEvent.clear(screen.getByTestId('range-input'));
  346. expect(getSubmitButton()).toBeDisabled();
  347. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  348. expect(getSubmitButton()).toBeEnabled();
  349. });
  350. });
  351. });