createProject.spec.tsx 13 KB

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