groupStore.spec.tsx 7.1 KB

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