groupStore.spec.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. let triggerSpy: jest.SpyInstance;
  78. const stats: Record<string, TimeseriesValue[]> = {auto: [[1611576000, 10]]};
  79. beforeAll(function () {
  80. triggerSpy = jest.spyOn(GroupStore, 'trigger');
  81. });
  82. beforeEach(function () {
  83. triggerSpy.mockReset();
  84. GroupStore.items = [g('1'), g('2'), g('3')];
  85. });
  86. it('should merge stats into existing groups', function () {
  87. GroupStore.onPopulateStats(
  88. ['1', '2', '3'],
  89. [
  90. {id: '1', stats} as GroupStats,
  91. {id: '2', stats} as GroupStats,
  92. {id: '3', stats} as GroupStats,
  93. ]
  94. );
  95. const group = GroupStore.getAllItems()[0] as Group;
  96. expect(group.stats).toEqual(stats);
  97. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  98. });
  99. it('should not change current item ids', function () {
  100. GroupStore.onPopulateStats(
  101. ['2', '3'],
  102. [{id: '2', stats} as GroupStats, {id: '3', stats} as GroupStats]
  103. );
  104. const group1 = GroupStore.getAllItems()[0] as Group;
  105. const group2 = GroupStore.getAllItems()[1] as Group;
  106. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['2', '3']));
  107. expect(group1.stats).not.toEqual(stats);
  108. expect(group2.stats).toEqual(stats);
  109. });
  110. });
  111. describe('getAllItems()', function () {
  112. it('Merges pending changes into items', function () {
  113. GroupStore.items = [];
  114. GroupStore.add([g('1'), g('2')]);
  115. GroupStore.onUpdate('1337', ['1'], {someChange: true});
  116. expect(GroupStore.get('1')).toEqual(
  117. expect.objectContaining({id: '1', someChange: true})
  118. );
  119. });
  120. });
  121. describe('update methods', function () {
  122. let triggerSpy: jest.SpyInstance;
  123. beforeAll(function () {
  124. triggerSpy = jest.spyOn(GroupStore, 'trigger');
  125. });
  126. beforeEach(function () {
  127. triggerSpy.mockReset();
  128. });
  129. beforeEach(function () {
  130. GroupStore.items = [g('1'), g('2'), g('3')];
  131. });
  132. describe('onUpdate()', function () {
  133. it("should treat undefined itemIds argument as 'all'", function () {
  134. GroupStore.onUpdate('1337', undefined, {});
  135. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  136. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  137. });
  138. it('should apply optimistic updates', function () {
  139. GroupStore.items = [g('1'), g('2')];
  140. GroupStore.add([g('1'), g('2')]);
  141. // Resolve 2 issues
  142. const itemIds = ['1', '2'];
  143. const data = {status: 'resolved', statusDetails: {}};
  144. GroupStore.onUpdate('12345', itemIds, data);
  145. expect(GroupStore.pendingChanges).toEqual(new Map([['12345', {itemIds, data}]]));
  146. expect(GroupStore.get('1')).toEqual({...g('1'), ...data});
  147. expect(GroupStore.get('2')).toEqual({...g('2'), ...data});
  148. });
  149. });
  150. describe('onUpdateSuccess()', function () {
  151. it("should treat undefined itemIds argument as 'all'", function () {
  152. GroupStore.onUpdateSuccess('1337', undefined, {});
  153. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  154. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  155. });
  156. });
  157. describe('onUpdateError()', function () {
  158. it("should treat undefined itemIds argument as 'all'", function () {
  159. GroupStore.onUpdateError('1337', undefined, false);
  160. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  161. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  162. });
  163. });
  164. describe('onDeleteSuccess()', function () {
  165. it("should treat undefined itemIds argument as 'all'", function () {
  166. GroupStore.onDeleteSuccess('1337', undefined, {});
  167. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  168. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  169. });
  170. });
  171. });
  172. });