groupStore.spec.tsx 6.7 KB

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