createProject.spec.tsx 13 KB

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