teamKeyTransactionButton.spec.tsx 12 KB

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