createProject.spec.tsx 12 KB

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