teamKeyTransactionButton.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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', 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. expect(getTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  64. });
  65. it('renders with all teams checked', async function () {
  66. MockApiClient.addMockResponse({
  67. method: 'GET',
  68. url: '/organizations/org-slug/key-transactions-list/',
  69. body: teams.map(({id}) => ({
  70. team: id,
  71. count: 1,
  72. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  73. })),
  74. });
  75. render(
  76. <TeamKeyTransactionButton
  77. eventView={eventView}
  78. organization={organization}
  79. transactionName="transaction"
  80. />
  81. );
  82. await clickTeamKeyTransactionDropdown();
  83. // all teams should be checked
  84. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  85. 'aria-selected',
  86. 'true'
  87. );
  88. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  89. 'aria-selected',
  90. 'true'
  91. );
  92. });
  93. it('renders with some teams checked', async function () {
  94. MockApiClient.addMockResponse({
  95. method: 'GET',
  96. url: '/organizations/org-slug/key-transactions-list/',
  97. body: teams.map(({id}) => ({
  98. team: id,
  99. count: id === teams[0].id ? 1 : 0,
  100. keyed:
  101. id === teams[0].id
  102. ? [{project_id: String(project.id), transaction: 'transaction'}]
  103. : [],
  104. })),
  105. });
  106. render(
  107. <TeamKeyTransactionButton
  108. eventView={eventView}
  109. organization={organization}
  110. transactionName="transaction"
  111. />
  112. );
  113. await clickTeamKeyTransactionDropdown();
  114. // only team 1 should be checked
  115. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  116. 'aria-selected',
  117. 'true'
  118. );
  119. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  120. 'aria-selected',
  121. 'false'
  122. );
  123. });
  124. it('renders with no teams checked', async function () {
  125. MockApiClient.addMockResponse({
  126. method: 'GET',
  127. url: '/organizations/org-slug/key-transactions-list/',
  128. body: teams.map(({id}) => ({
  129. team: id,
  130. count: 0,
  131. keyed: [],
  132. })),
  133. });
  134. render(
  135. <TeamKeyTransactionButton
  136. eventView={eventView}
  137. organization={organization}
  138. transactionName="transaction"
  139. />
  140. );
  141. await clickTeamKeyTransactionDropdown();
  142. // all teams should be unchecked
  143. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  144. 'aria-selected',
  145. 'false'
  146. );
  147. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  148. 'aria-selected',
  149. 'false'
  150. );
  151. });
  152. it('should be able to check one team', async function () {
  153. MockApiClient.addMockResponse({
  154. method: 'GET',
  155. url: '/organizations/org-slug/key-transactions-list/',
  156. body: teams.map(({id}) => ({
  157. team: id,
  158. count: 0,
  159. keyed: [],
  160. })),
  161. });
  162. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  163. method: 'POST',
  164. url: '/organizations/org-slug/key-transactions/',
  165. body: [],
  166. match: [
  167. MockApiClient.matchQuery({project: [project.id]}),
  168. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  169. ],
  170. });
  171. render(
  172. <TeamKeyTransactionButton
  173. eventView={eventView}
  174. organization={organization}
  175. transactionName="transaction"
  176. />
  177. );
  178. await clickTeamKeyTransactionDropdown();
  179. await userEvent.click(screen.getByRole('option', {name: `#${teams[0].slug}`}));
  180. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  181. });
  182. it('should be able to uncheck one team', async function () {
  183. MockApiClient.addMockResponse({
  184. method: 'GET',
  185. url: '/organizations/org-slug/key-transactions-list/',
  186. body: teams.map(({id}) => ({
  187. team: id,
  188. count: 1,
  189. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  190. })),
  191. });
  192. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  193. method: 'DELETE',
  194. url: '/organizations/org-slug/key-transactions/',
  195. body: [],
  196. match: [
  197. MockApiClient.matchQuery({project: [project.id]}),
  198. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  199. ],
  200. });
  201. render(
  202. <TeamKeyTransactionButton
  203. eventView={eventView}
  204. organization={organization}
  205. transactionName="transaction"
  206. />
  207. );
  208. await clickTeamKeyTransactionDropdown();
  209. await userEvent.click(screen.getByRole('option', {name: `#${teams[0].slug}`}));
  210. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  211. });
  212. it('should be able to check all with my teams', async function () {
  213. MockApiClient.addMockResponse({
  214. method: 'GET',
  215. url: '/organizations/org-slug/key-transactions-list/',
  216. body: teams.map(({id}) => ({
  217. team: id,
  218. count: 0,
  219. keyed: [],
  220. })),
  221. });
  222. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  223. method: 'POST',
  224. url: '/organizations/org-slug/key-transactions/',
  225. body: [],
  226. match: [
  227. MockApiClient.matchQuery({project: [project.id]}),
  228. MockApiClient.matchData({
  229. team: [teams[0].id, teams[1].id],
  230. transaction: 'transaction',
  231. }),
  232. ],
  233. });
  234. render(
  235. <TeamKeyTransactionButton
  236. eventView={eventView}
  237. organization={organization}
  238. transactionName="transaction"
  239. />
  240. );
  241. await clickTeamKeyTransactionDropdown();
  242. await userEvent.click(screen.getByRole('button', {name: 'Select All in My Teams'}));
  243. // all teams should be checked now
  244. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  245. 'aria-selected',
  246. 'true'
  247. );
  248. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  249. 'aria-selected',
  250. 'true'
  251. );
  252. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  253. });
  254. it('should be able to uncheck all with my teams', async function () {
  255. MockApiClient.addMockResponse({
  256. method: 'GET',
  257. url: '/organizations/org-slug/key-transactions-list/',
  258. body: teams.map(({id}) => ({
  259. team: id,
  260. count: 1,
  261. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  262. })),
  263. });
  264. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  265. method: 'DELETE',
  266. url: '/organizations/org-slug/key-transactions/',
  267. body: [],
  268. match: [
  269. MockApiClient.matchQuery({project: [project.id]}),
  270. MockApiClient.matchData({
  271. team: [teams[0].id, teams[1].id],
  272. transaction: 'transaction',
  273. }),
  274. ],
  275. });
  276. render(
  277. <TeamKeyTransactionButton
  278. eventView={eventView}
  279. organization={organization}
  280. transactionName="transaction"
  281. />
  282. );
  283. await clickTeamKeyTransactionDropdown();
  284. await userEvent.click(screen.getByRole('button', {name: 'Unselect All in My Teams'}));
  285. // all teams should be checked now
  286. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  287. 'aria-selected',
  288. 'false'
  289. );
  290. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  291. 'aria-selected',
  292. 'false'
  293. );
  294. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  295. });
  296. it('renders unkeyed as disabled if count exceeds max', async function () {
  297. MockApiClient.addMockResponse({
  298. method: 'GET',
  299. url: '/organizations/org-slug/key-transactions-list/',
  300. body: teams.map(({id}) => ({
  301. team: id,
  302. count: MAX_TEAM_KEY_TRANSACTIONS,
  303. keyed: Array.from({length: MAX_TEAM_KEY_TRANSACTIONS}, (_, i) => ({
  304. project_id: String(project.id),
  305. transaction: `transaction-${i}`,
  306. })),
  307. })),
  308. });
  309. render(
  310. <TeamKeyTransactionButton
  311. eventView={eventView}
  312. organization={organization}
  313. transactionName="transaction"
  314. />
  315. );
  316. await clickTeamKeyTransactionDropdown();
  317. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  318. 'aria-disabled',
  319. 'true'
  320. );
  321. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  322. 'aria-disabled',
  323. 'true'
  324. );
  325. });
  326. it('renders keyed as checked even if count is maxed', async function () {
  327. MockApiClient.addMockResponse({
  328. method: 'GET',
  329. url: '/organizations/org-slug/key-transactions-list/',
  330. body: teams.map(({id}) => ({
  331. team: id,
  332. count: MAX_TEAM_KEY_TRANSACTIONS,
  333. keyed: [
  334. {project_id: String(project.id), transaction: 'transaction'},
  335. ...Array.from({length: MAX_TEAM_KEY_TRANSACTIONS - 1}, (_, i) => ({
  336. project_id: String(project.id),
  337. transaction: `transaction-${i}`,
  338. })),
  339. ],
  340. })),
  341. });
  342. render(
  343. <TeamKeyTransactionButton
  344. eventView={eventView}
  345. organization={organization}
  346. transactionName="transaction"
  347. />
  348. );
  349. await clickTeamKeyTransactionDropdown();
  350. expect(screen.getByRole('option', {name: `#${teams[0].slug}`})).toHaveAttribute(
  351. 'aria-selected',
  352. 'true'
  353. );
  354. expect(screen.getByRole('option', {name: `#${teams[1].slug}`})).toHaveAttribute(
  355. 'aria-selected',
  356. 'true'
  357. );
  358. });
  359. });