createProject.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 only allow teams which the user is a team-admin', async function () {
  164. const organization = TestStubs.Organization();
  165. renderFrameworkModalMockRequests({organization, teamSlug: 'team-two'});
  166. OrganizationStore.onUpdate(organization);
  167. TeamStore.loadUserTeams([
  168. TestStubs.Team({id: 1, slug: 'team-one', access: []}),
  169. TestStubs.Team({id: 2, slug: 'team-two', access: ['team:admin']}),
  170. TestStubs.Team({id: 3, slug: 'team-three', access: ['team:admin']}),
  171. ]);
  172. render(<CreateProject />, {
  173. context: TestStubs.routerContext([{organization}]),
  174. organization,
  175. });
  176. await userEvent.type(screen.getByLabelText('Select a Team'), 'team');
  177. expect(screen.queryByText('#team-one')).not.toBeInTheDocument();
  178. expect(screen.getByText('#team-two')).toBeInTheDocument();
  179. expect(screen.getByText('#team-three')).toBeInTheDocument();
  180. });
  181. it('should fill in project name if its empty when platform is chosen', async function () {
  182. const {organization} = initializeOrg({
  183. organization: {
  184. access: ['project:admin'],
  185. },
  186. });
  187. const {container} = render(<CreateProject />, {
  188. context: TestStubs.routerContext([
  189. {
  190. organization: {
  191. id: '1',
  192. slug: 'testOrg',
  193. access: ['project:read'],
  194. },
  195. },
  196. ]),
  197. organization,
  198. });
  199. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  200. expect(screen.getByPlaceholderText('project-name')).toHaveValue('apple-ios');
  201. await userEvent.click(screen.getByTestId('platform-ruby-rails'));
  202. expect(screen.getByPlaceholderText('project-name')).toHaveValue('ruby-rails');
  203. // but not replace it when project name is something else:
  204. await userEvent.clear(screen.getByPlaceholderText('project-name'));
  205. await userEvent.type(screen.getByPlaceholderText('project-name'), 'another');
  206. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  207. expect(screen.getByPlaceholderText('project-name')).toHaveValue('another');
  208. expect(container).toSnapshot();
  209. });
  210. it('does not render framework selection modal if vanilla js is NOT selected', async function () {
  211. const {organization} = initializeOrg({
  212. organization: {
  213. features: ['onboarding-sdk-selection'],
  214. access: ['project:read', 'project:write'],
  215. },
  216. });
  217. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  218. organization,
  219. teamSlug: teamWithAccess.slug,
  220. });
  221. TeamStore.loadUserTeams([teamWithAccess]);
  222. OrganizationStore.onUpdate(organization, {replace: true});
  223. render(<CreateProject />, {
  224. organization,
  225. });
  226. // Select the React platform
  227. await userEvent.click(screen.getByTestId('platform-javascript-react'));
  228. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  229. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  230. await waitFor(() => {
  231. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  232. });
  233. renderGlobalModal();
  234. // Click on 'configure SDK' button
  235. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  236. // Modal shall not be open
  237. expect(screen.queryByText('Do you use a framework?')).not.toBeInTheDocument();
  238. expect(frameWorkModalMockRequests.projectCreationMockRequest).toHaveBeenCalled();
  239. });
  240. it('renders framework selection modal if vanilla js is selected', async function () {
  241. const {organization} = initializeOrg({
  242. organization: {
  243. features: ['onboarding-sdk-selection'],
  244. },
  245. });
  246. const frameWorkModalMockRequests = renderFrameworkModalMockRequests({
  247. organization,
  248. teamSlug: teamWithAccess.slug,
  249. });
  250. TeamStore.loadUserTeams([teamWithAccess]);
  251. OrganizationStore.onUpdate(organization, {replace: true});
  252. render(<CreateProject />, {
  253. organization,
  254. });
  255. // Select the JavaScript platform
  256. await userEvent.click(screen.getByTestId('platform-javascript'));
  257. await userEvent.type(screen.getByLabelText('Select a Team'), teamWithAccess.slug);
  258. await userEvent.click(screen.getByText(`#${teamWithAccess.slug}`));
  259. await waitFor(() => {
  260. expect(screen.getByRole('button', {name: 'Create Project'})).toBeEnabled();
  261. });
  262. renderGlobalModal();
  263. // Click on 'configure SDK' button
  264. await userEvent.click(screen.getByRole('button', {name: 'Create Project'}));
  265. // Modal is open
  266. await screen.findByText('Do you use a framework?');
  267. // Close modal
  268. await userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  269. expect(frameWorkModalMockRequests.projectCreationMockRequest).not.toHaveBeenCalled();
  270. });
  271. describe('Issue Alerts Options', function () {
  272. const organization = TestStubs.Organization();
  273. beforeEach(() => {
  274. TeamStore.loadUserTeams([teamWithAccess]);
  275. MockApiClient.addMockResponse({
  276. url: `/projects/${organization.slug}/rule-conditions/`,
  277. // @ts-ignore TODO: fix this type
  278. body: TestStubs.MOCK_RESP_VERBOSE,
  279. });
  280. });
  281. afterEach(() => {
  282. MockApiClient.clearMockResponses();
  283. });
  284. it('should enabled the submit button if and only if all the required information has been filled', async function () {
  285. render(<CreateProject />);
  286. const createProjectButton = screen.getByRole('button', {name: 'Create Project'});
  287. await userEvent.click(screen.getByText(/When there are more than/));
  288. expect(createProjectButton).toBeDisabled();
  289. await userEvent.type(screen.getByTestId('range-input'), '2');
  290. expect(screen.getByTestId('range-input')).toHaveValue(2);
  291. expect(createProjectButton).toBeDisabled();
  292. await userEvent.click(screen.getByTestId('platform-apple-ios'));
  293. expect(createProjectButton).toBeEnabled();
  294. await userEvent.clear(screen.getByTestId('range-input'));
  295. expect(createProjectButton).toBeDisabled();
  296. await userEvent.type(screen.getByTestId('range-input'), '2712');
  297. expect(createProjectButton).toBeEnabled();
  298. await userEvent.clear(screen.getByTestId('range-input'));
  299. expect(createProjectButton).toBeDisabled();
  300. await userEvent.click(screen.getByText("I'll create my own alerts later"));
  301. expect(createProjectButton).toBeEnabled();
  302. });
  303. });
  304. });