groupingStore.spec.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. import * as GroupActionCreators from 'sentry/actionCreators/group';
  2. import GroupingStore from 'sentry/stores/groupingStore';
  3. describe('Grouping Store', function () {
  4. let trigger;
  5. beforeAll(function () {
  6. MockApiClient.asyncDelay = 1;
  7. });
  8. afterAll(function () {
  9. MockApiClient.asyncDelay = undefined;
  10. });
  11. beforeEach(function () {
  12. GroupingStore.init();
  13. trigger = jest.spyOn(GroupingStore, 'trigger');
  14. MockApiClient.addMockResponse({
  15. url: '/issues/groupId/hashes/',
  16. body: [
  17. {
  18. latestEvent: {
  19. eventID: 'event-1',
  20. },
  21. state: 'locked',
  22. id: '1',
  23. },
  24. {
  25. latestEvent: {
  26. eventID: 'event-2',
  27. },
  28. state: 'unlocked',
  29. id: '2',
  30. },
  31. {
  32. latestEvent: {
  33. eventID: 'event-3',
  34. },
  35. state: 'unlocked',
  36. id: '3',
  37. },
  38. {
  39. latestEvent: {
  40. eventID: 'event-4',
  41. },
  42. state: 'unlocked',
  43. id: '4',
  44. },
  45. {
  46. latestEvent: {
  47. eventID: 'event-5',
  48. },
  49. state: 'locked',
  50. id: '5',
  51. },
  52. ],
  53. });
  54. MockApiClient.addMockResponse({
  55. url: '/issues/groupId/similar/',
  56. body: [
  57. [
  58. {
  59. id: '274',
  60. },
  61. {
  62. 'exception:stacktrace:pairs': 0.375,
  63. 'exception:stacktrace:application-chunks': 0.175,
  64. 'message:message:character-shingles': 0.775,
  65. },
  66. ],
  67. [
  68. {
  69. id: '275',
  70. },
  71. {'exception:stacktrace:pairs': 1.0},
  72. ],
  73. [
  74. {
  75. id: '216',
  76. },
  77. {
  78. 'exception:stacktrace:application-chunks': 0.000235,
  79. 'exception:stacktrace:pairs': 0.001488,
  80. },
  81. ],
  82. [
  83. {
  84. id: '217',
  85. },
  86. {
  87. 'exception:message:character-shingles': null,
  88. 'exception:stacktrace:application-chunks': 0.25,
  89. 'exception:stacktrace:pairs': 0.25,
  90. 'message:message:character-shingles': 0.7,
  91. },
  92. ],
  93. ],
  94. });
  95. });
  96. afterEach(function () {
  97. MockApiClient.clearMockResponses();
  98. jest.resetAllMocks();
  99. jest.restoreAllMocks();
  100. });
  101. describe('onFetch()', function () {
  102. beforeEach(() => GroupingStore.init());
  103. it('initially gets called with correct state values', function () {
  104. GroupingStore.onFetch([]);
  105. expect(trigger).toHaveBeenCalled();
  106. expect(trigger).toHaveBeenCalledWith(
  107. expect.objectContaining({
  108. error: false,
  109. filteredSimilarItems: [],
  110. loading: true,
  111. mergeState: new Map(),
  112. mergedItems: [],
  113. mergedLinks: '',
  114. similarItems: [],
  115. similarLinks: '',
  116. unmergeState: new Map(),
  117. })
  118. );
  119. });
  120. it('fetches list of similar items', async function () {
  121. await GroupingStore.onFetch([
  122. {dataKey: 'similar', endpoint: '/issues/groupId/similar/'},
  123. ]);
  124. expect(trigger).toHaveBeenCalled();
  125. const calls = trigger.mock.calls;
  126. const arg = calls[calls.length - 1][0];
  127. expect(arg.filteredSimilarItems).toHaveLength(1);
  128. expect(arg.similarItems).toHaveLength(3);
  129. expect(arg).toMatchObject({
  130. loading: false,
  131. error: false,
  132. mergeState: new Map(),
  133. mergedItems: [],
  134. similarItems: [
  135. {
  136. isBelowThreshold: false,
  137. issue: {
  138. id: '274',
  139. },
  140. },
  141. {
  142. isBelowThreshold: false,
  143. issue: {
  144. id: '275',
  145. },
  146. },
  147. {
  148. isBelowThreshold: false,
  149. issue: {
  150. id: '217',
  151. },
  152. },
  153. ],
  154. filteredSimilarItems: [
  155. {
  156. isBelowThreshold: true,
  157. issue: {
  158. id: '216',
  159. },
  160. },
  161. ],
  162. unmergeState: new Map(),
  163. });
  164. });
  165. it('unsuccessfully fetches list of similar items', function () {
  166. MockApiClient.clearMockResponses();
  167. MockApiClient.addMockResponse({
  168. url: '/issues/groupId/similar/',
  169. statusCode: 500,
  170. body: {message: 'failed'},
  171. });
  172. const promise = GroupingStore.onFetch([
  173. {dataKey: 'similar', endpoint: '/issues/groupId/similar/'},
  174. ]);
  175. expect(trigger).toHaveBeenCalled();
  176. const calls = trigger.mock.calls;
  177. return promise.then(() => {
  178. const arg = calls[calls.length - 1][0];
  179. expect(arg).toMatchObject({
  180. loading: false,
  181. error: true,
  182. mergeState: new Map(),
  183. mergedItems: [],
  184. unmergeState: new Map(),
  185. });
  186. });
  187. });
  188. it('ignores null scores in aggregate', async function () {
  189. await GroupingStore.onFetch([
  190. {dataKey: 'similar', endpoint: '/issues/groupId/similar/'},
  191. ]);
  192. expect(trigger).toHaveBeenCalled();
  193. const calls = trigger.mock.calls;
  194. const arg = calls[calls.length - 1][0];
  195. const item = arg.similarItems.find(({issue}) => issue.id === '217');
  196. expect(item.aggregate.exception).toBe(0.25);
  197. expect(item.aggregate.message).toBe(0.7);
  198. });
  199. it('fetches list of hashes', function () {
  200. const promise = GroupingStore.onFetch([
  201. {dataKey: 'merged', endpoint: '/issues/groupId/hashes/'},
  202. ]);
  203. expect(trigger).toHaveBeenCalled();
  204. const calls = trigger.mock.calls;
  205. return promise.then(() => {
  206. const arg = calls[calls.length - 1][0];
  207. expect(arg.mergedItems).toHaveLength(5);
  208. expect(arg).toMatchObject({
  209. loading: false,
  210. error: false,
  211. similarItems: [],
  212. filteredSimilarItems: [],
  213. mergeState: new Map(),
  214. unmergeState: new Map([
  215. ['1', {busy: true}],
  216. ['2', {busy: false}],
  217. ['3', {busy: false}],
  218. ['4', {busy: false}],
  219. ['5', {busy: true}],
  220. ]),
  221. });
  222. });
  223. });
  224. it('unsuccessfully fetches list of hashes items', function () {
  225. MockApiClient.clearMockResponses();
  226. MockApiClient.addMockResponse({
  227. url: '/issues/groupId/hashes/',
  228. statusCode: 500,
  229. body: {message: 'failed'},
  230. });
  231. const promise = GroupingStore.onFetch([
  232. {dataKey: 'merged', endpoint: '/issues/groupId/hashes/'},
  233. ]);
  234. expect(trigger).toHaveBeenCalled();
  235. const calls = trigger.mock.calls;
  236. return promise.then(() => {
  237. const arg = calls[calls.length - 1][0];
  238. expect(arg).toMatchObject({
  239. loading: false,
  240. error: true,
  241. mergeState: new Map(),
  242. mergedItems: [],
  243. unmergeState: new Map(),
  244. });
  245. });
  246. });
  247. });
  248. describe('Similar Issues list (to be merged)', function () {
  249. let mergeList;
  250. let mergeState;
  251. beforeEach(function () {
  252. GroupingStore.init();
  253. mergeList = [];
  254. mergeState = new Map();
  255. return GroupingStore.onFetch([
  256. {dataKey: 'similar', endpoint: '/issues/groupId/similar/'},
  257. ]);
  258. });
  259. describe('onToggleMerge (checkbox state)', function () {
  260. beforeEach(() => GroupingStore.init());
  261. // Attempt to check first item but its "locked" so should not be able to do anything
  262. it('can check and uncheck item', function () {
  263. GroupingStore.onToggleMerge('1');
  264. mergeList = ['1'];
  265. mergeState.set('1', {checked: true});
  266. expect(GroupingStore.getState().mergeList).toEqual(mergeList);
  267. expect(GroupingStore.getState().mergeState).toEqual(mergeState);
  268. // Uncheck
  269. GroupingStore.onToggleMerge('1');
  270. mergeList = mergeList.filter(item => item !== '1');
  271. mergeState.set('1', {checked: false});
  272. // Check all
  273. GroupingStore.onToggleMerge('1');
  274. GroupingStore.onToggleMerge('2');
  275. GroupingStore.onToggleMerge('3');
  276. mergeList = ['1', '2', '3'];
  277. mergeState.set('1', {checked: true});
  278. mergeState.set('2', {checked: true});
  279. mergeState.set('3', {checked: true});
  280. expect(GroupingStore.getState().mergeList).toEqual(mergeList);
  281. expect(GroupingStore.getState().mergeState).toEqual(mergeState);
  282. expect(trigger).toHaveBeenLastCalledWith({
  283. mergeDisabled: false,
  284. mergeList,
  285. mergeState,
  286. });
  287. });
  288. });
  289. describe('onMerge', function () {
  290. beforeEach(function () {
  291. MockApiClient.clearMockResponses();
  292. MockApiClient.addMockResponse({
  293. method: 'PUT',
  294. url: '/projects/orgId/projectId/issues/',
  295. });
  296. GroupingStore.init();
  297. });
  298. it('disables rows to be merged', async function () {
  299. const mergeMock = jest.spyOn(GroupActionCreators, 'mergeGroups');
  300. trigger.mockReset();
  301. GroupingStore.onToggleMerge('1');
  302. mergeList = ['1'];
  303. mergeState.set('1', {checked: true});
  304. expect(trigger).toHaveBeenLastCalledWith({
  305. mergeDisabled: false,
  306. mergeList,
  307. mergeState,
  308. });
  309. trigger.mockReset();
  310. // Everything is sync so trigger will have been called multiple times
  311. const promise = GroupingStore.onMerge({
  312. params: {
  313. orgId: 'orgId',
  314. groupId: 'groupId',
  315. },
  316. projectId: 'projectId',
  317. });
  318. mergeState.set('1', {checked: true, busy: true});
  319. expect(trigger).toHaveBeenCalledWith({
  320. mergeDisabled: true,
  321. mergeList,
  322. mergeState,
  323. });
  324. await promise;
  325. expect(mergeMock).toHaveBeenCalledWith(
  326. expect.anything(),
  327. {
  328. orgId: 'orgId',
  329. projectId: 'projectId',
  330. itemIds: ['1', 'groupId'],
  331. query: undefined,
  332. },
  333. {
  334. error: expect.any(Function),
  335. success: expect.any(Function),
  336. complete: expect.any(Function),
  337. }
  338. );
  339. // Should be removed from mergeList after merged
  340. mergeList = mergeList.filter(item => item !== '1');
  341. mergeState.set('1', {checked: false, busy: true});
  342. expect(trigger).toHaveBeenLastCalledWith({
  343. mergeDisabled: false,
  344. mergeList,
  345. mergeState,
  346. });
  347. });
  348. it('keeps rows in "busy" state and unchecks after successfully adding to merge queue', async function () {
  349. GroupingStore.onToggleMerge('1');
  350. mergeList = ['1'];
  351. mergeState.set('1', {checked: true});
  352. // Expect checked
  353. expect(trigger).toHaveBeenCalledWith({
  354. mergeDisabled: false,
  355. mergeList,
  356. mergeState,
  357. });
  358. trigger.mockReset();
  359. // Start unmerge
  360. const promise = GroupingStore.onMerge({
  361. params: {
  362. orgId: 'orgId',
  363. groupId: 'groupId',
  364. },
  365. projectId: 'projectId',
  366. });
  367. mergeState.set('1', {checked: true, busy: true});
  368. // Expect checked to remain the same, but is now busy
  369. expect(trigger).toHaveBeenCalledWith({
  370. mergeDisabled: true,
  371. mergeList,
  372. mergeState,
  373. });
  374. await promise;
  375. mergeState.set('1', {checked: false, busy: true});
  376. // After promise, reset checked to false, but keep busy
  377. expect(trigger).toHaveBeenLastCalledWith({
  378. mergeDisabled: false,
  379. mergeList: [],
  380. mergeState,
  381. });
  382. });
  383. it('resets busy state and has same items checked after error when trying to merge', async function () {
  384. MockApiClient.clearMockResponses();
  385. MockApiClient.addMockResponse({
  386. method: 'PUT',
  387. url: '/projects/orgId/projectId/issues/',
  388. statusCode: 500,
  389. body: {},
  390. });
  391. GroupingStore.onToggleMerge('1');
  392. mergeList = ['1'];
  393. mergeState.set('1', {checked: true});
  394. const promise = GroupingStore.onMerge({
  395. params: {
  396. orgId: 'orgId',
  397. groupId: 'groupId',
  398. },
  399. projectId: 'projectId',
  400. });
  401. mergeState.set('1', {checked: true, busy: true});
  402. expect(trigger).toHaveBeenCalledWith({
  403. mergeDisabled: true,
  404. mergeList,
  405. mergeState,
  406. });
  407. await promise;
  408. // Error state
  409. mergeState.set('1', {checked: true, busy: false});
  410. expect(trigger).toHaveBeenLastCalledWith({
  411. mergeDisabled: false,
  412. mergeList,
  413. mergeState,
  414. });
  415. });
  416. });
  417. });
  418. describe('Hashes list (to be unmerged)', function () {
  419. let unmergeList;
  420. let unmergeState;
  421. beforeEach(async function () {
  422. GroupingStore.init();
  423. unmergeList = new Map();
  424. unmergeState = new Map();
  425. await GroupingStore.onFetch([
  426. {dataKey: 'merged', endpoint: '/issues/groupId/hashes/'},
  427. ]);
  428. trigger.mockClear();
  429. unmergeState = new Map([...GroupingStore.getState().unmergeState]);
  430. });
  431. // WARNING: all the tests in this describe block are not running in isolated state.
  432. // There is a good chance that moving them around will break them. To simulate an isolated state,
  433. // add a beforeEach(() => GroupingStore.init())
  434. describe('onToggleUnmerge (checkbox state for hashes)', function () {
  435. // Attempt to check first item but its "locked" so should not be able to do anything
  436. it('can not check locked item', function () {
  437. GroupingStore.onToggleUnmerge('1');
  438. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  439. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  440. expect(trigger).not.toHaveBeenCalled();
  441. });
  442. it('can check and uncheck unlocked items', function () {
  443. // Check
  444. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  445. unmergeList.set('2', 'event-2');
  446. unmergeState.set('2', {busy: false, checked: true});
  447. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  448. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  449. // Uncheck
  450. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  451. unmergeList.delete('2');
  452. unmergeState.set('2', {busy: false, checked: false});
  453. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  454. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  455. // Check
  456. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  457. unmergeList.set('2', 'event-2');
  458. unmergeState.set('2', {busy: false, checked: true});
  459. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  460. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  461. expect(trigger).toHaveBeenLastCalledWith({
  462. enableFingerprintCompare: false,
  463. unmergeLastCollapsed: false,
  464. unmergeDisabled: false,
  465. unmergeList,
  466. unmergeState,
  467. });
  468. });
  469. it('should have Compare button enabled only when two fingerprints are checked', function () {
  470. expect(GroupingStore.getState().enableFingerprintCompare).toBe(false);
  471. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  472. GroupingStore.onToggleUnmerge(['3', 'event-3']);
  473. expect(GroupingStore.getState().enableFingerprintCompare).toBe(true);
  474. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  475. expect(GroupingStore.getState().enableFingerprintCompare).toBe(false);
  476. });
  477. it('selecting all available checkboxes should disable the unmerge button and re-enable when unchecking', function () {
  478. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  479. GroupingStore.onToggleUnmerge(['3', 'event-3']);
  480. GroupingStore.onToggleUnmerge(['4', 'event-4']);
  481. unmergeList.set('2', 'event-2');
  482. unmergeList.set('3', 'event-3');
  483. unmergeList.set('4', 'event-4');
  484. unmergeState.set('2', {busy: false, checked: true});
  485. unmergeState.set('3', {busy: false, checked: true});
  486. unmergeState.set('4', {busy: false, checked: true});
  487. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  488. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  489. expect(GroupingStore.getState().unmergeDisabled).toBe(true);
  490. // Unchecking
  491. GroupingStore.onToggleUnmerge(['4', 'event-4']);
  492. unmergeList.delete('4');
  493. unmergeState.set('4', {busy: false, checked: false});
  494. expect(GroupingStore.getState().unmergeList).toEqual(unmergeList);
  495. expect(GroupingStore.getState().unmergeState).toEqual(unmergeState);
  496. expect(GroupingStore.getState().unmergeDisabled).toBe(false);
  497. expect(trigger).toHaveBeenLastCalledWith({
  498. enableFingerprintCompare: true,
  499. unmergeLastCollapsed: false,
  500. unmergeDisabled: false,
  501. unmergeList,
  502. unmergeState,
  503. });
  504. });
  505. });
  506. // WARNING: all the tests in this describe block are not running in isolated state.
  507. // There is a good chance that moving them around will break them. To simulate an isolated state,
  508. // add a beforeEach(() => GroupingStore.init())
  509. describe('onUnmerge', function () {
  510. beforeEach(function () {
  511. MockApiClient.clearMockResponses();
  512. MockApiClient.addMockResponse({
  513. method: 'DELETE',
  514. url: '/issues/groupId/hashes/',
  515. });
  516. });
  517. it('can not toggle unmerge for a locked item', function () {
  518. // Event 1 is locked
  519. GroupingStore.onToggleUnmerge(['1', 'event-1']);
  520. unmergeState.set('1', {busy: true});
  521. // trigger does NOT get called because an item returned via API is in a "locked" state
  522. expect(trigger).not.toHaveBeenCalled();
  523. GroupingStore.onUnmerge({
  524. groupId: 'groupId',
  525. });
  526. expect(trigger).toHaveBeenCalledWith({
  527. enableFingerprintCompare: false,
  528. unmergeLastCollapsed: false,
  529. unmergeDisabled: true,
  530. unmergeList,
  531. unmergeState,
  532. });
  533. });
  534. it('disables rows to be merged', async function () {
  535. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  536. unmergeList.set('2', 'event-2');
  537. unmergeState.set('2', {checked: true, busy: false});
  538. // trigger does NOT get called because an item returned via API is in a "locked" state
  539. expect(trigger).toHaveBeenCalledWith({
  540. enableFingerprintCompare: false,
  541. unmergeLastCollapsed: false,
  542. unmergeDisabled: false,
  543. unmergeList,
  544. unmergeState,
  545. });
  546. const promise = GroupingStore.onUnmerge({
  547. groupId: 'groupId',
  548. });
  549. unmergeState.set('2', {checked: false, busy: true});
  550. expect(trigger).toHaveBeenCalledWith({
  551. enableFingerprintCompare: false,
  552. unmergeLastCollapsed: false,
  553. unmergeDisabled: true,
  554. unmergeList,
  555. unmergeState,
  556. });
  557. await promise;
  558. // Success
  559. unmergeState.set('2', {checked: false, busy: true});
  560. unmergeList.delete('2');
  561. expect(trigger).toHaveBeenLastCalledWith({
  562. enableFingerprintCompare: false,
  563. unmergeLastCollapsed: false,
  564. unmergeDisabled: false,
  565. unmergeList,
  566. unmergeState,
  567. });
  568. });
  569. it('keeps rows in "busy" state and unchecks after successfully adding to unmerge queue', async function () {
  570. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  571. unmergeList.set('2', 'event-2');
  572. unmergeState.set('2', {checked: true, busy: false});
  573. const promise = GroupingStore.onUnmerge({
  574. groupId: 'groupId',
  575. });
  576. unmergeState.set('2', {checked: false, busy: true});
  577. expect(trigger).toHaveBeenCalledWith({
  578. enableFingerprintCompare: false,
  579. unmergeLastCollapsed: false,
  580. unmergeDisabled: true,
  581. unmergeList,
  582. unmergeState,
  583. });
  584. await promise;
  585. expect(trigger).toHaveBeenLastCalledWith({
  586. enableFingerprintCompare: false,
  587. unmergeLastCollapsed: false,
  588. unmergeDisabled: false,
  589. unmergeList: new Map(),
  590. unmergeState,
  591. });
  592. });
  593. it('resets busy state and has same items checked after error when trying to merge', async function () {
  594. MockApiClient.clearMockResponses();
  595. MockApiClient.addMockResponse({
  596. method: 'DELETE',
  597. url: '/issues/groupId/hashes/',
  598. statusCode: 500,
  599. body: {},
  600. });
  601. GroupingStore.onToggleUnmerge(['2', 'event-2']);
  602. unmergeList.set('2', 'event-2');
  603. const promise = GroupingStore.onUnmerge({
  604. groupId: 'groupId',
  605. });
  606. unmergeState.set('2', {checked: false, busy: true});
  607. expect(trigger).toHaveBeenCalledWith({
  608. enableFingerprintCompare: false,
  609. unmergeLastCollapsed: false,
  610. unmergeDisabled: true,
  611. unmergeList,
  612. unmergeState,
  613. });
  614. await promise;
  615. unmergeState.set('2', {checked: true, busy: false});
  616. expect(trigger).toHaveBeenLastCalledWith({
  617. enableFingerprintCompare: false,
  618. unmergeLastCollapsed: false,
  619. unmergeDisabled: false,
  620. unmergeList,
  621. unmergeState,
  622. });
  623. });
  624. });
  625. });
  626. });