createProject.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 * as useExperiment from 'sentry/utils/useExperiment';
  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: [TestStubs.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 = TestStubs.Team({
  56. slug: 'test',
  57. id: '1',
  58. name: 'test',
  59. access: ['team:read'],
  60. });
  61. const teamWithAccess = TestStubs.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', function () {
  78. render(<CreateProject />, {
  79. context: TestStubs.routerContext([
  80. {organization: {id: '1', slug: 'testOrg', access: ['project:read']}},
  81. ]),
  82. });
  83. });
  84. it('can create a new project without team as org member', async function () {
  85. const {organization} = initializeOrg({
  86. organization: {
  87. access: ['project:read'],
  88. features: ['team-project-creation-all'],
  89. },
  90. });
  91. jest.spyOn(useExperiment, 'useExperiment').mockReturnValue({
  92. experimentAssignment: 1,
  93. logExperiment: jest.fn(),
  94. });
  95. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  96. TeamStore.loadUserTeams([TestStubs.Team({id: 2, slug: 'team-two', access: []})]);
  97. render(<CreateProject />, {
  98. context: TestStubs.routerContext([
  99. {
  100. organization: {
  101. id: '1',
  102. slug: 'testOrg',
  103. access: ['project:read'],
  104. },
  105. },
  106. ]),
  107. organization,
  108. });
  109. renderGlobalModal();
  110. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  111. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  112. expect(createTeamButton).not.toBeInTheDocument();
  113. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  114. });
  115. it('should only allow teams which the user is a team-admin', async function () {
  116. const organization = Organization();
  117. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  118. OrganizationStore.onUpdate(organization);
  119. TeamStore.loadUserTeams([
  120. TestStubs.Team({id: 1, slug: 'team-one', access: []}),
  121. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  122. TestStubs.Team({id: 3, slug: 'team-three', access: ['team:admin']}),
  123. ]);
  124. render(<CreateProject />, {
  125. context: TestStubs.routerContext([{organization}]),
  126. organization,
  127. });
  128. await userEvent.type(screen.getByLabelText('Select a Team'), 'team');
  129. expect(screen.queryByText('#team-one')).not.toBeInTheDocument();
  130. expect(screen.getByText('#team-two')).toBeInTheDocument();
  131. expect(screen.getByText('#team-three')).toBeInTheDocument();
  132. });
  133. it('should fill in project name if its empty when platform is chosen', async function () {
  134. const {organization} = initializeOrg({
  135. organization: {
  136. access: ['project:admin'],
  137. },
  138. });
  139. render(<CreateProject />, {
  140. context: TestStubs.routerContext([
  141. {
  142. organization: {
  143. id: '1',
  144. slug: 'testOrg',
  145. access: ['project:read'],
  146. },
  147. },
  148. ]),
  149. organization,
  150. });
  151. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  152. expect(screen.getByPlaceholderText('project-name')).toHaveValue('apple-ios');
  153. await userEvent.click(screen.getByTestId('platform-ruby-rails'));
  154. expect(screen.getByPlaceholderText('project-name')).toHaveValue('ruby-rails');
  155. // but not replace it when project name is something else:
  156. await userEvent.clear(screen.getByPlaceholderText('project-name'));
  157. await userEvent.type(screen.getByPlaceholderText('project-name'), 'another');
  158. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  159. expect(screen.getByPlaceholderText('project-name')).toHaveValue('another');
  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 = Organization();
  320. beforeEach(() => {
  321. TeamStore.loadUserTeams([teamWithAccess]);
  322. MockApiClient.addMockResponse({
  323. url: `/projects/${organization.slug}/rule-conditions/`,
  324. body: 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. });