groupStore.spec.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {ActorFixture} from 'sentry-fixture/actor';
  2. import {GroupFixture} from 'sentry-fixture/group';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import GroupStore from 'sentry/stores/groupStore';
  5. import {Group, GroupActivityType, GroupStats, TimeseriesValue} from 'sentry/types';
  6. const MOCK_PROJECT = ProjectFixture();
  7. const g = (id: string, params?: Partial<Group>): Group => {
  8. return GroupFixture({id, project: MOCK_PROJECT, ...params});
  9. };
  10. describe('GroupStore', function () {
  11. beforeEach(function () {
  12. GroupStore.reset();
  13. });
  14. describe('add()', function () {
  15. it('should add new entries', function () {
  16. GroupStore.items = [];
  17. GroupStore.add([g('1'), g('2')]);
  18. expect(GroupStore.items).toEqual([g('1'), g('2')]);
  19. });
  20. it('should update matching existing entries', function () {
  21. GroupStore.items = [g('1'), g('2')];
  22. GroupStore.add([g('1', {count: '1337'}), g('3')]);
  23. expect(GroupStore.getAllItemIds()).toEqual(['1', '2', '3']);
  24. expect(GroupStore.items[0]).toEqual(
  25. expect.objectContaining({id: '1', count: '1337'})
  26. );
  27. });
  28. it('should attempt to preserve order of ids', function () {
  29. GroupStore.add([g('2'), g('1'), g('3')]);
  30. expect(GroupStore.getAllItemIds()).toEqual(['2', '1', '3']);
  31. });
  32. });
  33. describe('addToFront()', function () {
  34. it('should add new entries to beginning of the list', function () {
  35. GroupStore.items = [g('2')];
  36. GroupStore.addToFront([g('1'), g('3')]);
  37. expect(GroupStore.items).toEqual([g('1'), g('3'), g('2')]);
  38. });
  39. it('should update matching existing entries', function () {
  40. GroupStore.items = [g('1'), g('2')];
  41. GroupStore.addToFront([g('1', {count: '1337'}), g('3')]);
  42. expect(GroupStore.getAllItems()).toEqual([
  43. expect.objectContaining({id: '1', count: '1337'}),
  44. g('3'),
  45. g('2'),
  46. ]);
  47. });
  48. it('should attempt to preserve order of ids', function () {
  49. GroupStore.addToFront([g('2'), g('1'), g('3')]);
  50. expect(GroupStore.getAllItemIds()).toEqual(['2', '1', '3']);
  51. });
  52. });
  53. describe('remove()', function () {
  54. it('should remove entry', function () {
  55. GroupStore.items = [g('1'), g('2')];
  56. GroupStore.remove(['1']);
  57. expect(GroupStore.items).toEqual([g('2')]);
  58. });
  59. it('should remove multiple entries', function () {
  60. GroupStore.items = [g('1'), g('2'), g('3')];
  61. GroupStore.remove(['1', '2']);
  62. expect(GroupStore.items).toEqual([g('3')]);
  63. });
  64. it('should not remove already removed item', function () {
  65. GroupStore.items = [g('1'), g('2')];
  66. GroupStore.remove(['0']);
  67. expect(GroupStore.items).toEqual([g('1'), g('2')]);
  68. });
  69. });
  70. describe('onMergeSuccess()', function () {
  71. it('should remove the non-parent merged ids', function () {
  72. GroupStore.items = [g('1'), g('2'), g('3'), g('4')];
  73. GroupStore.onMergeSuccess(
  74. '',
  75. ['2', '3', '4'], // items merged
  76. {merge: {parent: '3'}} // merge API response
  77. );
  78. expect(GroupStore.items).toEqual([
  79. g('1'),
  80. g('3'), // parent
  81. ]);
  82. });
  83. });
  84. describe('onPopulateStats()', function () {
  85. const stats: Record<string, TimeseriesValue[]> = {auto: [[1611576000, 10]]};
  86. beforeEach(function () {
  87. jest.spyOn(GroupStore, 'trigger');
  88. GroupStore.items = [g('1'), g('2'), g('3')];
  89. });
  90. afterEach(() => {
  91. jest.restoreAllMocks();
  92. });
  93. it('should merge stats into existing groups', function () {
  94. GroupStore.onPopulateStats(
  95. ['1', '2', '3'],
  96. [
  97. {id: '1', stats} as GroupStats,
  98. {id: '2', stats} as GroupStats,
  99. {id: '3', stats} as GroupStats,
  100. ]
  101. );
  102. const group = GroupStore.getAllItems()[0] as Group;
  103. expect(group.stats).toEqual(stats);
  104. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  105. });
  106. it('should not change current item ids', function () {
  107. GroupStore.onPopulateStats(
  108. ['2', '3'],
  109. [{id: '2', stats} as GroupStats, {id: '3', stats} as GroupStats]
  110. );
  111. const group1 = GroupStore.getAllItems()[0] as Group;
  112. const group2 = GroupStore.getAllItems()[1] as Group;
  113. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['2', '3']));
  114. expect(group1.stats).not.toEqual(stats);
  115. expect(group2.stats).toEqual(stats);
  116. });
  117. });
  118. describe('getAllItems()', function () {
  119. it('Merges pending changes into items', function () {
  120. GroupStore.items = [];
  121. GroupStore.add([g('1'), g('2')]);
  122. GroupStore.onUpdate('1337', ['1'], {someChange: true});
  123. expect(GroupStore.get('1')).toEqual(
  124. expect.objectContaining({id: '1', someChange: true})
  125. );
  126. });
  127. });
  128. describe('update methods', function () {
  129. beforeEach(function () {
  130. jest.spyOn(GroupStore, 'trigger');
  131. GroupStore.items = [g('1'), g('2'), g('3')];
  132. });
  133. afterEach(() => {
  134. jest.restoreAllMocks();
  135. });
  136. describe('onUpdate()', function () {
  137. it("should treat undefined itemIds argument as 'all'", function () {
  138. GroupStore.onUpdate('1337', undefined, {});
  139. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  140. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  141. });
  142. it('should apply optimistic updates', function () {
  143. GroupStore.items = [g('1'), g('2')];
  144. GroupStore.add([g('1'), g('2')]);
  145. // Resolve 2 issues
  146. const itemIds = ['1', '2'];
  147. const data = {status: 'resolved', statusDetails: {}};
  148. GroupStore.onUpdate('12345', itemIds, data);
  149. expect(GroupStore.pendingChanges).toEqual(new Map([['12345', {itemIds, data}]]));
  150. expect(GroupStore.get('1')).toEqual({...g('1'), ...data});
  151. expect(GroupStore.get('2')).toEqual({...g('2'), ...data});
  152. });
  153. });
  154. describe('onUpdateSuccess()', function () {
  155. it("should treat undefined itemIds argument as 'all'", function () {
  156. GroupStore.onUpdateSuccess('1337', undefined, {});
  157. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  158. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  159. });
  160. });
  161. describe('onUpdateError()', function () {
  162. it("should treat undefined itemIds argument as 'all'", function () {
  163. GroupStore.onUpdateError('1337', undefined, false);
  164. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  165. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  166. });
  167. });
  168. describe('onDeleteSuccess()', function () {
  169. it("should treat undefined itemIds argument as 'all'", function () {
  170. GroupStore.onDeleteSuccess('1337', undefined, {});
  171. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  172. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  173. });
  174. });
  175. describe('onAssignToSuccess()', function () {
  176. it("should treat undefined itemIds argument as 'all'", function () {
  177. GroupStore.items = [g('1')];
  178. const assignedGroup = g('1', {assignedTo: ActorFixture()});
  179. GroupStore.onAssignToSuccess('1337', '1', assignedGroup);
  180. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  181. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1']));
  182. expect(GroupStore.items[0]).toEqual(assignedGroup);
  183. });
  184. });
  185. describe('updateActivity()', function () {
  186. it("should update activity data text'", function () {
  187. GroupStore.items = [
  188. g('1', {
  189. activity: [
  190. {
  191. id: '1',
  192. type: GroupActivityType.NOTE,
  193. dateCreated: '',
  194. project: ProjectFixture(),
  195. data: {text: 'Orginal Text'},
  196. },
  197. ],
  198. }),
  199. ];
  200. GroupStore.updateActivity('1', '1', {text: 'Updated Text'});
  201. expect(GroupStore.items[0].activity[0].data).toEqual({text: 'Updated Text'});
  202. });
  203. });
  204. });
  205. });