teamKeyTransactionButton.spec.tsx 12 KB

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