teamKeyTransactionButton.spec.jsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {act} 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(wrapper) {
  9. wrapper.find('Button').simulate('click');
  10. await tick();
  11. wrapper.update();
  12. }
  13. describe('TeamKeyTransactionButton', function () {
  14. const organization = TestStubs.Organization({features: ['performance-view']});
  15. const teams = [
  16. TestStubs.Team({id: '1', slug: 'team1', name: 'Team 1'}),
  17. TestStubs.Team({id: '2', slug: 'team2', name: 'Team 2'}),
  18. ];
  19. const project = TestStubs.Project({teams});
  20. const eventView = new EventView({
  21. id: '1',
  22. name: 'my query',
  23. fields: [{field: 'count()'}],
  24. sorts: [{field: 'count', kind: 'desc'}],
  25. query: '',
  26. project: [project.id],
  27. start: '2019-10-01T00:00:00',
  28. end: '2019-10-02T00:00:00',
  29. statsPeriod: '14d',
  30. environment: [],
  31. });
  32. beforeEach(function () {
  33. MockApiClient.clearMockResponses();
  34. act(() => ProjectsStore.loadInitialData([project]));
  35. act(() => void TeamStore.loadInitialData(teams, false, null));
  36. });
  37. it('fetches key transactions with project param', async function () {
  38. const getTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  39. method: 'GET',
  40. url: '/organizations/org-slug/key-transactions-list/',
  41. body: teams.map(({id}) => ({
  42. team: id,
  43. count: 1,
  44. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  45. })),
  46. match: [MockApiClient.matchQuery({project: [project.id], team: ['myteams']})],
  47. });
  48. const wrapper = mountWithTheme(
  49. <TeamKeyTransactionButton
  50. eventView={eventView}
  51. organization={organization}
  52. transactionName="transaction"
  53. />
  54. );
  55. await tick();
  56. wrapper.update();
  57. expect(getTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  58. });
  59. it('renders with all teams checked', async function () {
  60. MockApiClient.addMockResponse({
  61. method: 'GET',
  62. url: '/organizations/org-slug/key-transactions-list/',
  63. body: teams.map(({id}) => ({
  64. team: id,
  65. count: 1,
  66. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  67. })),
  68. });
  69. const wrapper = mountWithTheme(
  70. <TeamKeyTransactionButton
  71. eventView={eventView}
  72. organization={organization}
  73. transactionName="transaction"
  74. />
  75. );
  76. await tick();
  77. wrapper.update();
  78. clickTeamKeyTransactionDropdown(wrapper);
  79. // header should show the checked state
  80. expect(wrapper.find('TitleButton').exists()).toBeTruthy();
  81. const header = wrapper.find('DropdownMenuHeader');
  82. expect(header.exists()).toBeTruthy();
  83. expect(header.find('CheckboxFancy').props().isChecked).toBeTruthy();
  84. expect(header.find('CheckboxFancy').props().isIndeterminate).toBeFalsy();
  85. // all teams should be checked
  86. const entries = wrapper.find('DropdownMenuItem');
  87. expect(entries.length).toBe(2);
  88. entries.forEach((entry, i) => {
  89. expect(entry.text()).toEqual(teams[i].slug);
  90. expect(entry.find('CheckboxFancy').props().isChecked).toBeTruthy();
  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. const wrapper = mountWithTheme(
  107. <TeamKeyTransactionButton
  108. eventView={eventView}
  109. organization={organization}
  110. transactionName="transaction"
  111. />
  112. );
  113. await tick();
  114. wrapper.update();
  115. clickTeamKeyTransactionDropdown(wrapper);
  116. // header should show the indeterminate state
  117. const header = wrapper.find('DropdownMenuHeader');
  118. expect(header.exists()).toBeTruthy();
  119. expect(header.find('CheckboxFancy').props().isChecked).toBeFalsy();
  120. expect(header.find('CheckboxFancy').props().isIndeterminate).toBeTruthy();
  121. // only team 1 should be checked
  122. const entries = wrapper.find('DropdownMenuItem');
  123. expect(entries.length).toBe(2);
  124. entries.forEach((entry, i) => {
  125. expect(entry.text()).toEqual(teams[i].slug);
  126. });
  127. expect(entries.at(0).find('CheckboxFancy').props().isChecked).toBeTruthy();
  128. expect(entries.at(1).find('CheckboxFancy').props().isChecked).toBeFalsy();
  129. });
  130. it('renders with no teams checked', async function () {
  131. MockApiClient.addMockResponse({
  132. method: 'GET',
  133. url: '/organizations/org-slug/key-transactions-list/',
  134. body: teams.map(({id}) => ({
  135. team: id,
  136. count: 0,
  137. keyed: [],
  138. })),
  139. });
  140. const wrapper = mountWithTheme(
  141. <TeamKeyTransactionButton
  142. eventView={eventView}
  143. organization={organization}
  144. transactionName="transaction"
  145. />
  146. );
  147. await tick();
  148. wrapper.update();
  149. clickTeamKeyTransactionDropdown(wrapper);
  150. // header should show the unchecked state
  151. const header = wrapper.find('DropdownMenuHeader');
  152. expect(header.exists()).toBeTruthy();
  153. expect(header.find('CheckboxFancy').props().isChecked).toBeFalsy();
  154. expect(header.find('CheckboxFancy').props().isIndeterminate).toBeFalsy();
  155. // all teams should be unchecked
  156. const entries = wrapper.find('DropdownMenuItem');
  157. expect(entries.length).toBe(2);
  158. entries.forEach((entry, i) => {
  159. expect(entry.text()).toEqual(teams[i].slug);
  160. expect(entry.find('CheckboxFancy').props().isChecked).toBeFalsy();
  161. });
  162. });
  163. it('should be able to check one team', async function () {
  164. MockApiClient.addMockResponse({
  165. method: 'GET',
  166. url: '/organizations/org-slug/key-transactions-list/',
  167. body: teams.map(({id}) => ({
  168. team: id,
  169. count: 0,
  170. keyed: [],
  171. })),
  172. });
  173. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  174. method: 'POST',
  175. url: '/organizations/org-slug/key-transactions/',
  176. body: [],
  177. match: [
  178. MockApiClient.matchQuery({project: [project.id]}),
  179. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  180. ],
  181. });
  182. const wrapper = mountWithTheme(
  183. <TeamKeyTransactionButton
  184. eventView={eventView}
  185. organization={organization}
  186. transactionName="transaction"
  187. />
  188. );
  189. await tick();
  190. wrapper.update();
  191. clickTeamKeyTransactionDropdown(wrapper);
  192. wrapper.find('DropdownMenuItem CheckboxFancy').first().simulate('click');
  193. await tick();
  194. wrapper.update();
  195. const checkbox = wrapper.find('DropdownMenuItem CheckboxFancy').first();
  196. expect(checkbox.props().isChecked).toBeTruthy();
  197. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  198. });
  199. it('should be able to uncheck one team', async function () {
  200. MockApiClient.addMockResponse({
  201. method: 'GET',
  202. url: '/organizations/org-slug/key-transactions-list/',
  203. body: teams.map(({id}) => ({
  204. team: id,
  205. count: 1,
  206. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  207. })),
  208. });
  209. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  210. method: 'DELETE',
  211. url: '/organizations/org-slug/key-transactions/',
  212. body: [],
  213. match: [
  214. MockApiClient.matchQuery({project: [project.id]}),
  215. MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
  216. ],
  217. });
  218. const wrapper = mountWithTheme(
  219. <TeamKeyTransactionButton
  220. eventView={eventView}
  221. organization={organization}
  222. transactionName="transaction"
  223. />
  224. );
  225. await tick();
  226. wrapper.update();
  227. clickTeamKeyTransactionDropdown(wrapper);
  228. wrapper.find('DropdownMenuItem CheckboxFancy').first().simulate('click');
  229. await tick();
  230. wrapper.update();
  231. const checkbox = wrapper.find('DropdownMenuItem CheckboxFancy').first();
  232. expect(checkbox.props().isChecked).toBeFalsy();
  233. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  234. });
  235. it('should be able to check all with my teams', async function () {
  236. MockApiClient.addMockResponse({
  237. method: 'GET',
  238. url: '/organizations/org-slug/key-transactions-list/',
  239. body: teams.map(({id}) => ({
  240. team: id,
  241. count: 0,
  242. keyed: [],
  243. })),
  244. });
  245. const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  246. method: 'POST',
  247. url: '/organizations/org-slug/key-transactions/',
  248. body: [],
  249. match: [
  250. MockApiClient.matchQuery({project: [project.id]}),
  251. MockApiClient.matchData({
  252. team: [teams[0].id, teams[1].id],
  253. transaction: 'transaction',
  254. }),
  255. ],
  256. });
  257. const wrapper = mountWithTheme(
  258. <TeamKeyTransactionButton
  259. eventView={eventView}
  260. organization={organization}
  261. transactionName="transaction"
  262. />
  263. );
  264. await tick();
  265. wrapper.update();
  266. clickTeamKeyTransactionDropdown(wrapper);
  267. wrapper.find('DropdownMenuHeader CheckboxFancy').simulate('click');
  268. await tick();
  269. wrapper.update();
  270. // header should be checked now
  271. const headerCheckbox = wrapper.find('DropdownMenuHeader CheckboxFancy');
  272. expect(headerCheckbox.props().isChecked).toBeTruthy();
  273. expect(headerCheckbox.props().isIndeterminate).toBeFalsy();
  274. // all teams should be checked now
  275. const entries = wrapper.find('DropdownMenuItem');
  276. entries.forEach(entry => {
  277. expect(entry.find('CheckboxFancy').props().isChecked).toBeTruthy();
  278. });
  279. expect(postTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  280. });
  281. it('should be able to uncheck all with my teams', async function () {
  282. MockApiClient.addMockResponse({
  283. method: 'GET',
  284. url: '/organizations/org-slug/key-transactions-list/',
  285. body: teams.map(({id}) => ({
  286. team: id,
  287. count: 1,
  288. keyed: [{project_id: String(project.id), transaction: 'transaction'}],
  289. })),
  290. });
  291. const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
  292. method: 'DELETE',
  293. url: '/organizations/org-slug/key-transactions/',
  294. body: [],
  295. match: [
  296. MockApiClient.matchQuery({project: [project.id]}),
  297. MockApiClient.matchData({
  298. team: [teams[0].id, teams[1].id],
  299. transaction: 'transaction',
  300. }),
  301. ],
  302. });
  303. const wrapper = mountWithTheme(
  304. <TeamKeyTransactionButton
  305. eventView={eventView}
  306. organization={organization}
  307. transactionName="transaction"
  308. />
  309. );
  310. await tick();
  311. wrapper.update();
  312. clickTeamKeyTransactionDropdown(wrapper);
  313. wrapper.find('DropdownMenuHeader CheckboxFancy').simulate('click');
  314. await tick();
  315. wrapper.update();
  316. // header should be unchecked now
  317. const headerCheckbox = wrapper.find('DropdownMenuHeader CheckboxFancy');
  318. expect(headerCheckbox.props().isChecked).toBeFalsy();
  319. expect(headerCheckbox.props().isIndeterminate).toBeFalsy();
  320. // all teams should be unchecked now
  321. const entries = wrapper.find('DropdownMenuItem');
  322. entries.forEach(entry => {
  323. expect(entry.find('CheckboxFancy').props().isChecked).toBeFalsy();
  324. });
  325. expect(deleteTeamKeyTransactionsMock).toHaveBeenCalledTimes(1);
  326. });
  327. it('renders unkeyed as disabled if count exceeds max', async function () {
  328. MockApiClient.addMockResponse({
  329. method: 'GET',
  330. url: '/organizations/org-slug/key-transactions-list/',
  331. body: teams.map(({id}) => ({
  332. team: id,
  333. count: MAX_TEAM_KEY_TRANSACTIONS,
  334. keyed: Array.from({length: MAX_TEAM_KEY_TRANSACTIONS}, (_, i) => ({
  335. project_id: String(project.id),
  336. transaction: `transaction-${i}`,
  337. })),
  338. })),
  339. });
  340. const wrapper = mountWithTheme(
  341. <TeamKeyTransactionButton
  342. eventView={eventView}
  343. organization={organization}
  344. transactionName="transaction"
  345. />
  346. );
  347. await tick();
  348. wrapper.update();
  349. clickTeamKeyTransactionDropdown(wrapper);
  350. const entries = wrapper.find('DropdownMenuItem');
  351. expect(entries.length).toBe(2);
  352. entries.forEach((entry, i) => {
  353. expect(entry.props().disabled).toBeTruthy();
  354. expect(entry.text()).toEqual(`${teams[i].slug}Max ${MAX_TEAM_KEY_TRANSACTIONS}`);
  355. });
  356. });
  357. it('renders keyed as checked even if count is maxed', async function () {
  358. MockApiClient.addMockResponse({
  359. method: 'GET',
  360. url: '/organizations/org-slug/key-transactions-list/',
  361. body: teams.map(({id}) => ({
  362. team: id,
  363. count: MAX_TEAM_KEY_TRANSACTIONS,
  364. keyed: [
  365. {project_id: String(project.id), transaction: 'transaction'},
  366. ...Array.from({length: MAX_TEAM_KEY_TRANSACTIONS - 1}, (_, i) => ({
  367. project_id: String(project.id),
  368. transaction: `transaction-${i}`,
  369. })),
  370. ],
  371. })),
  372. });
  373. const wrapper = mountWithTheme(
  374. <TeamKeyTransactionButton
  375. eventView={eventView}
  376. organization={organization}
  377. transactionName="transaction"
  378. />
  379. );
  380. await tick();
  381. wrapper.update();
  382. clickTeamKeyTransactionDropdown(wrapper);
  383. const entries = wrapper.find('DropdownMenuItem');
  384. expect(entries.length).toBe(2);
  385. entries.forEach((entry, i) => {
  386. expect(entry.props().disabled).toBeFalsy();
  387. expect(entry.text()).toEqual(teams[i].slug);
  388. expect(entry.find('CheckboxFancy').props().isChecked).toBeTruthy();
  389. });
  390. });
  391. });