groupStore.spec.tsx 7.8 KB

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