createProject.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 OrganizationStore from 'sentry/stores/organizationStore';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import {Organization} from 'sentry/types';
  12. import * as useExperiment from 'sentry/utils/useExperiment';
  13. import {CreateProject} from 'sentry/views/projectInstall/createProject';
  14. function renderFrameworkModalMockRequests({
  15. organization,
  16. teamSlug,
  17. }: {
  18. organization: Organization;
  19. teamSlug: string;
  20. }) {
  21. MockApiClient.addMockResponse({
  22. url: `/projects/${organization.slug}/rule-conditions/`,
  23. body: [],
  24. });
  25. MockApiClient.addMockResponse({
  26. url: `/organizations/${organization.slug}/teams/`,
  27. body: [TestStubs.Team({slug: teamSlug})],
  28. });
  29. MockApiClient.addMockResponse({
  30. url: `/organizations/${organization.slug}/`,
  31. body: organization,
  32. });
  33. MockApiClient.addMockResponse({
  34. url: `/organizations/${organization.slug}/projects/`,
  35. body: [],
  36. });
  37. const projectCreationMockRequest = MockApiClient.addMockResponse({
  38. url: `/teams/${organization.slug}/${teamSlug}/projects/`,
  39. method: 'POST',
  40. });
  41. return {projectCreationMockRequest};
  42. }
  43. describe('CreateProject', function () {
  44. const teamNoAccess = TestStubs.Team({
  45. slug: 'test',
  46. id: '1',
  47. name: 'test',
  48. access: ['team:read'],
  49. });
  50. const teamWithAccess = TestStubs.Team({
  51. access: ['team:admin', 'team:write', 'team:read'],
  52. });
  53. beforeEach(() => {
  54. TeamStore.reset();
  55. TeamStore.loadUserTeams([teamNoAccess]);
  56. MockApiClient.addMockResponse({
  57. url: `/projects/testOrg/rule-conditions/`,
  58. body: {},
  59. // Not required for these tests
  60. statusCode: 500,
  61. });
  62. });
  63. afterEach(() => {
  64. MockApiClient.clearMockResponses();
  65. });
  66. it('should block if you have access to no teams', function () {
  67. const {container} = render(<CreateProject />, {
  68. context: TestStubs.routerContext([
  69. {organization: {id: '1', slug: 'testOrg', access: ['project:read']}},
  70. ]),
  71. });
  72. expect(container).toSnapshot();
  73. });
  74. it('can create a new team as admin', async function () {
  75. const {organization} = initializeOrg({
  76. organization: {
  77. access: ['project:admin'],
  78. },
  79. });
  80. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  81. TeamStore.loadUserTeams([
  82. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  83. ]);
  84. render(<CreateProject />, {
  85. context: TestStubs.routerContext([
  86. {
  87. organization: {
  88. id: '1',
  89. slug: 'testOrg',
  90. access: ['project:read'],
  91. },
  92. },
  93. ]),
  94. organization,
  95. });
  96. renderGlobalModal();
  97. await userEvent.click(screen.getByRole('button', {name: 'Create a team'}));
  98. expect(
  99. await screen.findByText(
  100. 'Members of a team have access to specific areas, such as a new release or a new application feature.'
  101. )
  102. ).toBeInTheDocument();
  103. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  104. });
  105. it('can create a new project without team as org member', async function () {
  106. const {organization} = initializeOrg({
  107. organization: {
  108. access: ['project:read'],
  109. features: ['team-project-creation-all'],
  110. },
  111. });
  112. jest.spyOn(useExperiment, 'useExperiment').mockReturnValue({
  113. experimentAssignment: 1,
  114. logExperiment: jest.fn(),
  115. });
  116. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  117. TeamStore.loadUserTeams([TestStubs.Team({id: 2, slug: 'team-two', access: []})]);
  118. render(<CreateProject />, {
  119. context: TestStubs.routerContext([
  120. {
  121. organization: {
  122. id: '1',
  123. slug: 'testOrg',
  124. access: ['project:read'],
  125. },
  126. },
  127. ]),
  128. organization,
  129. });
  130. renderGlobalModal();
  131. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  132. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  133. expect(createTeamButton).not.toBeInTheDocument();
  134. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  135. });
  136. it('can create a new team before project creation if org owner', async function () {
  137. const {organization} = initializeOrg({
  138. organization: {
  139. access: ['project:admin'],
  140. },
  141. });
  142. render(<CreateProject />, {
  143. context: TestStubs.routerContext([
  144. {
  145. organization: {
  146. id: '1',
  147. slug: 'testOrg',
  148. access: ['project:read'],
  149. },
  150. },
  151. ]),
  152. organization,
  153. });
  154. renderGlobalModal();
  155. await userEvent.click(screen.getByRole('button', {name: 'Create a team'}));
  156. expect(
  157. await screen.findByText(
  158. 'Members of a team have access to specific areas, such as a new release or a new application feature.'
  159. )
  160. ).toBeInTheDocument();
  161. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  162. });
  163. it('should not show create team button to team-admin with no org access', function () {
  164. const {organization} = initializeOrg({
  165. organization: {
  166. access: ['project:read'],
  167. },
  168. });
  169. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  170. OrganizationStore.onUpdate(organization);
  171. TeamStore.loadUserTeams([
  172. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  173. ]);
  174. render(<CreateProject />, {
  175. context: TestStubs.routerContext([{organization}]),
  176. organization,
  177. });
  178. const createTeamButton = screen.queryByRole('button', {name: 'Create a team'});
  179. expect(createTeamButton).not.toBeInTheDocument();
  180. });
  181. it('should only allow teams which the user is a team-admin', async function () {
  182. const organization = TestStubs.Organization();
  183. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  184. OrganizationStore.onUpdate(organization);
  185. TeamStore.loadUserTeams([
  186. TestStubs.Team({id: 1, slug: 'team-one', access: []}),
  187. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  188. TestStubs.Team({id: 3, slug: 'team-three', access: ['team:admin']}),
  189. ]);
  190. render(<CreateProject />, {
  191. context: TestStubs.routerContext([{organization}]),
  192. organization,
  193. });
  194. await userEvent.type(screen.getByLabelText('Select a Team'), 'team');
  195. expect(screen.queryByText('#team-one')).not.toBeInTheDocument();
  196. expect(screen.getByText('#team-two')).toBeInTheDocument();
  197. expect(screen.getByText('#team-three')).toBeInTheDocument();
  198. });
  199. it('should fill in project name if its empty when platform is chosen', async function () {
  200. const {organization} = initializeOrg({
  201. organization: {
  202. access: ['project:admin'],
  203. },
  204. });
  205. const {container} = render(<CreateProject />, {
  206. context: TestStubs.routerContext([
  207. {
  208. organization: {
  209. id: '1',
  210. slug: 'testOrg',
  211. access: ['project:read'],
  212. },
  213. },
  214. ]),
  215. organization,
  216. });
  217. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  218. expect(screen.getByPlaceholderText('project-name')).toHaveValue('apple-ios');
  219. await userEvent.click(screen.getByTestId('platform-ruby-rails'));
  220. expect(screen.getByPlaceholderText('project-name')).toHaveValue('ruby-rails');
  221. // but not replace it when project name is something else:
  222. await userEvent.clear(screen.getByPlaceholderText('project-name'));
  223. await userEvent.type(screen.getByPlaceholderText('project-name'), 'another');
  224. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  225. expect(screen.getByPlaceholderText('project-name')).toHaveValue('another');
  226. expect(container).toSnapshot();
  227. });
  228. it('does not render framework selection modal if vanilla js is NOT selected', async function () {
  229. const {organization} = initializeOrg({
  230. organization: {
  231. features: ['onboarding-sdk-selection'],
  232. access: ['project:read', 'project:write'],
  233. },
  234. });
  235. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  236. organization,
  237. teamSlug: teamWithAccess.slug,
  238. });
  239. TeamStore.loadUserTeams([teamWithAccess]);
  240. OrganizationStore.onUpdate(organization, {replace: true});
  241. render(<CreateProject />, {
  242. organization,
  243. });
  244. // Select the React platform
  245. await userEvent.click(screen.getByTestId('platform-javascript-react'));
  246. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  247. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  248. await waitFor(() => {
  249. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  250. });
  251. renderGlobalModal();
  252. // Click on 'configure SDK' button
  253. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  254. // Modal shall not be open
  255. expect(screen.queryByText('Do you use a framework?')).not.toBeInTheDocument();
  256. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalled();
  257. });
  258. it('renders framework selection modal if vanilla js is selected', async function () {
  259. const {organization} = initializeOrg({
  260. organization: {
  261. features: ['onboarding-sdk-selection'],
  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 JavaScript platform
  274. await userEvent.click(screen.getByTestId('platform-javascript'));
  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 is open
  284. await screen.findByText('Do you use a framework?');
  285. // Close modal
  286. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  287. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  288. });
  289. describe('Issue Alerts Options', function () {
  290. const organization = TestStubs.Organization();
  291. beforeEach(() => {
  292. TeamStore.loadUserTeams([teamWithAccess]);
  293. MockApiClient.addMockResponse({
  294. url: `/projects/${organization.slug}/rule-conditions/`,
  295. // @ts-ignore TODO: fix this type
  296. body: TestStubs.MOCK_RESP_VERBOSE,
  297. });
  298. });
  299. afterEach(() => {
  300. MockApiClient.clearMockResponses();
  301. });
  302. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  303. render(<CreateProject />);
  304. const createProjectButton = screen.getByRole('button', {name: 'Create Project'});
  305. await userEvent.click(screen.getByText(/When there are more than/));
  306. expect(createProjectButton).toBeDisabled();
  307. await userEvent.type(screen.getByTestId('range-input'), '2');
  308. expect(screen.getByTestId('range-input')).toHaveValue(2);
  309. expect(createProjectButton).toBeDisabled();
  310. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  311. expect(createProjectButton).toBeEnabled();
  312. await userEvent.clear(screen.getByTestId('range-input'));
  313. expect(createProjectButton).toBeDisabled();
  314. await userEvent.type(screen.getByTestId('range-input'), '2712');
  315. expect(createProjectButton).toBeEnabled();
  316. await userEvent.clear(screen.getByTestId('range-input'));
  317. expect(createProjectButton).toBeDisabled();
  318. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  319. expect(createProjectButton).toBeEnabled();
  320. });
  321. });
  322. });