teamKeyTransactionButton.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {UserFixture} from 'sentry-fixture/user';
  5. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. import TeamStore from 'sentry/stores/teamStore';
  8. import EventView from 'sentry/utils/discover/eventView';
  9. import {MAX_TEAM_KEY_TRANSACTIONS} from 'sentry/utils/performance/constants';
  10. import TeamKeyTransactionButton from 'sentry/views/performance/transactionSummary/teamKeyTransactionButton';
  11. async function clickTeamKeyTransactionDropdown() {
  12. await waitFor(() =>
  13. expect(screen.getByRole('button', {expanded: false})).toBeEnabled()
  14. );
  15. await userEvent.click(screen.getByRole('button', {expanded: false}));
  16. }
  17. describe('TeamKeyTransactionButton', function () {
  18. const organization = OrganizationFixture({features: ['performance-view']});
  19. const teams = [
  20. TeamFixture({id: '1', slug: 'team1', name: 'Team 1'}),
  21. TeamFixture({id: '2', slug: 'team2', name: 'Team 2'}),
  22. ];
  23. const project = ProjectFixture({teams});
  24. const eventView = new EventView({
  25. id: '1',
  26. name: 'my query',
  27. fields: [{field: 'count()'}],
  28. sorts: [{field: 'count', kind: 'desc'}],
  29. query: '',
  30. project: [parseInt(project.id, 10)],
  31. start: '2019-10-01T00:00:00',
  32. end: '2019-10-02T00:00:00',
  33. statsPeriod: '14d',
  34. environment: [],
  35. createdBy: UserFixture(),
  36. display: 'line',
  37. team: ['myteams'],
  38. topEvents: '5',
  39. });
  40. beforeEach(function () {
  41. MockApiClient.clearMockResponses();
  42. act(() => ProjectsStore.loadInitialData([project]));
  43. act(() => void TeamStore.loadInitialData(teams, false, null));
  44. });
  45. it('fetches key transactions with project param', async function () {
  46. const getTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  47. method: 'GET',
  48. url: '/organizations/org-slug/key-transactions-list/',
  49. body: teams.map(({id}) => ({
  50. team: id,
  51. count: 1,
  52. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  53. })),
  54. match: [MockApiClient.matchQuery({project: [project.id], team: ['myteams']})],
  55. });
  56. render(
  57. <TeamKeyTransactionButton
  58. eventView={eventView}
  59. organization={organization}
  60. transactionName="transaction"
  61. />
  62. );
  63. await waitFor(() => {
  64. expect(getTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  65. });
  66. });
  67. it('renders with all teams checked', async function () {
  68. MockApiClient.addMockResponse({
  69. method: 'GET',
  70. url: '/organizations/org-slug/key-transactions-list/',
  71. body: teams.map(({id}) => ({
  72. team: id,
  73. count: 1,
  74. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  75. })),
  76. });
  77. render(
  78. <TeamKeyTransactionButton
  79. eventView={eventView}
  80. organization={organization}
  81. transactionName="transaction"
  82. />
  83. );
  84. await clickTeamKeyTransactionDropdown();
  85. // all teams should be checked
  86. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  87. 'aria-selected',
  88. 'true'
  89. );
  90. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  91. 'aria-selected',
  92. 'true'
  93. );
  94. });
  95. it('renders with some teams checked', async function () {
  96. MockApiClient.addMockResponse({
  97. method: 'GET',
  98. url: '/organizations/org-slug/key-transactions-list/',
  99. body: teams.map(({id}) => ({
  100. team: id,
  101. count: id === teams[0].id ? 1 : 0,
  102. keyed:
  103. id === teams[0].id
  104. ? [{project_id: String(project.id), transaction: 'transaction'}]
  105. : [],
  106. })),
  107. });
  108. render(
  109. <TeamKeyTransactionButton
  110. eventView={eventView}
  111. organization={organization}
  112. transactionName="transaction"
  113. />
  114. );
  115. await clickTeamKeyTransactionDropdown();
  116. // only team 1 should be checked
  117. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  118. 'aria-selected',
  119. 'true'
  120. );
  121. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  122. 'aria-selected',
  123. 'false'
  124. );
  125. });
  126. it('renders with no teams checked', async function () {
  127. MockApiClient.addMockResponse({
  128. method: 'GET',
  129. url: '/organizations/org-slug/key-transactions-list/',
  130. body: teams.map(({id}) => ({
  131. team: id,
  132. count: 0,
  133. keyed: [],
  134. })),
  135. });
  136. render(
  137. <TeamKeyTransactionButton
  138. eventView={eventView}
  139. organization={organization}
  140. transactionName="transaction"
  141. />
  142. );
  143. await clickTeamKeyTransactionDropdown();
  144. // all teams should be unchecked
  145. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  146. 'aria-selected',
  147. 'false'
  148. );
  149. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  150. 'aria-selected',
  151. 'false'
  152. );
  153. });
  154. it('should be able to check one team', async function () {
  155. MockApiClient.addMockResponse({
  156. method: 'GET',
  157. url: '/organizations/org-slug/key-transactions-list/',
  158. body: teams.map(({id}) => ({
  159. team: id,
  160. count: 0,
  161. keyed: [],
  162. })),
  163. });
  164. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  165. method: 'POST',
  166. url: '/organizations/org-slug/key-transactions/',
  167. body: [],
  168. match: [
  169. MockApiClient.matchQuery({project: [project.id]}),
  170. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  171. ],
  172. });
  173. render(
  174. <TeamKeyTransactionButton
  175. eventView={eventView}
  176. organization={organization}
  177. transactionName="transaction"
  178. />
  179. );
  180. await clickTeamKeyTransactionDropdown();
  181. await userEvent.click(screen.getByRole('option', {name: `#${teams[0].slug}`}));
  182. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  183. });
  184. it('should be able to uncheck one team', async function () {
  185. MockApiClient.addMockResponse({
  186. method: 'GET',
  187. url: '/organizations/org-slug/key-transactions-list/',
  188. body: teams.map(({id}) => ({
  189. team: id,
  190. count: 1,
  191. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  192. })),
  193. });
  194. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  195. method: 'DELETE',
  196. url: '/organizations/org-slug/key-transactions/',
  197. body: [],
  198. match: [
  199. MockApiClient.matchQuery({project: [project.id]}),
  200. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  201. ],
  202. });
  203. render(
  204. <TeamKeyTransactionButton
  205. eventView={eventView}
  206. organization={organization}
  207. transactionName="transaction"
  208. />
  209. );
  210. await clickTeamKeyTransactionDropdown();
  211. await userEvent.click(screen.getByRole('option', {name: `#${teams[0].slug}`}));
  212. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  213. });
  214. it('should be able to check all with my teams', async function () {
  215. MockApiClient.addMockResponse({
  216. method: 'GET',
  217. url: '/organizations/org-slug/key-transactions-list/',
  218. body: teams.map(({id}) => ({
  219. team: id,
  220. count: 0,
  221. keyed: [],
  222. })),
  223. });
  224. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  225. method: 'POST',
  226. url: '/organizations/org-slug/key-transactions/',
  227. body: [],
  228. match: [
  229. MockApiClient.matchQuery({project: [project.id]}),
  230. MockApiClient.matchData({
  231. team: [teams[0].id, teams[1].id],
  232. transaction: 'transaction',
  233. }),
  234. ],
  235. });
  236. render(
  237. <TeamKeyTransactionButton
  238. eventView={eventView}
  239. organization={organization}
  240. transactionName="transaction"
  241. />
  242. );
  243. await clickTeamKeyTransactionDropdown();
  244. await userEvent.click(screen.getByRole('button', {name: 'Select All in My Teams'}));
  245. // all teams should be checked now
  246. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  247. 'aria-selected',
  248. 'true'
  249. );
  250. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  251. 'aria-selected',
  252. 'true'
  253. );
  254. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  255. });
  256. it('should be able to uncheck all with my teams', async function () {
  257. MockApiClient.addMockResponse({
  258. method: 'GET',
  259. url: '/organizations/org-slug/key-transactions-list/',
  260. body: teams.map(({id}) => ({
  261. team: id,
  262. count: 1,
  263. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  264. })),
  265. });
  266. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  267. method: 'DELETE',
  268. url: '/organizations/org-slug/key-transactions/',
  269. body: [],
  270. match: [
  271. MockApiClient.matchQuery({project: [project.id]}),
  272. MockApiClient.matchData({
  273. team: [teams[0].id, teams[1].id],
  274. transaction: 'transaction',
  275. }),
  276. ],
  277. });
  278. render(
  279. <TeamKeyTransactionButton
  280. eventView={eventView}
  281. organization={organization}
  282. transactionName="transaction"
  283. />
  284. );
  285. await clickTeamKeyTransactionDropdown();
  286. await userEvent.click(screen.getByRole('button', {name: 'Unselect All in My Teams'}));
  287. // all teams should be checked now
  288. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  289. 'aria-selected',
  290. 'false'
  291. );
  292. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  293. 'aria-selected',
  294. 'false'
  295. );
  296. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  297. });
  298. it('renders unkeyed as disabled if count exceeds max', async function () {
  299. MockApiClient.addMockResponse({
  300. method: 'GET',
  301. url: '/organizations/org-slug/key-transactions-list/',
  302. body: teams.map(({id}) => ({
  303. team: id,
  304. count: MAX_TEAM_KEY_TRANSACTIONS,
  305. keyed: Array.from({length: MAX_TEAM_KEY_TRANSACTIONS}, (_, i) => ({
  306. project_id: String(project.id),
  307. transaction: `transaction-${i}`,
  308. })),
  309. })),
  310. });
  311. render(
  312. <TeamKeyTransactionButton
  313. eventView={eventView}
  314. organization={organization}
  315. transactionName="transaction"
  316. />
  317. );
  318. await clickTeamKeyTransactionDropdown();
  319. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  320. 'aria-disabled',
  321. 'true'
  322. );
  323. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  324. 'aria-disabled',
  325. 'true'
  326. );
  327. });
  328. it('renders keyed as checked even if count is maxed', async function () {
  329. MockApiClient.addMockResponse({
  330. method: 'GET',
  331. url: '/organizations/org-slug/key-transactions-list/',
  332. body: teams.map(({id}) => ({
  333. team: id,
  334. count: MAX_TEAM_KEY_TRANSACTIONS,
  335. keyed: [
  336. {project_id: String(project.id), transaction: 'transaction'},
  337. ...Array.from({length: MAX_TEAM_KEY_TRANSACTIONS - 1}, (_, i) => ({
  338. project_id: String(project.id),
  339. transaction: `transaction-${i}`,
  340. })),
  341. ],
  342. })),
  343. });
  344. render(
  345. <TeamKeyTransactionButton
  346. eventView={eventView}
  347. organization={organization}
  348. transactionName="transaction"
  349. />
  350. );
  351. await clickTeamKeyTransactionDropdown();
  352. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  353. 'aria-selected',
  354. 'true'
  355. );
  356. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  357. 'aria-selected',
  358. 'true'
  359. );
  360. });
  361. });