groupStore.spec.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {Project} from 'sentry-fixture/project';
  2. import GroupStore from 'sentry/stores/groupStore';
  3. import {Group, GroupActivityType} 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('getAllItems()', function () {
  83. it('Merges pending changes into items', function () {
  84. GroupStore.items = [];
  85. GroupStore.add([g('1'), g('2')]);
  86. GroupStore.onUpdate('1337', ['1'], {someChange: true});
  87. expect(GroupStore.get('1')).toEqual(
  88. expect.objectContaining({id: '1', someChange: true})
  89. );
  90. });
  91. });
  92. describe('update methods', function () {
  93. beforeEach(function () {
  94. jest.spyOn(GroupStore, 'trigger');
  95. GroupStore.items = [g('1'), g('2'), g('3')];
  96. });
  97. afterEach(() => {
  98. jest.restoreAllMocks();
  99. });
  100. describe('onUpdate()', function () {
  101. it("should treat undefined itemIds argument as 'all'", function () {
  102. GroupStore.onUpdate('1337', undefined, {});
  103. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  104. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  105. });
  106. it('should apply optimistic updates', function () {
  107. GroupStore.items = [g('1'), g('2')];
  108. GroupStore.add([g('1'), g('2')]);
  109. // Resolve 2 issues
  110. const itemIds = ['1', '2'];
  111. const data = {status: 'resolved', statusDetails: {}};
  112. GroupStore.onUpdate('12345', itemIds, data);
  113. expect(GroupStore.pendingChanges).toEqual(new Map([['12345', {itemIds, data}]]));
  114. expect(GroupStore.get('1')).toEqual({...g('1'), ...data});
  115. expect(GroupStore.get('2')).toEqual({...g('2'), ...data});
  116. });
  117. });
  118. describe('onUpdateSuccess()', function () {
  119. it("should treat undefined itemIds argument as 'all'", function () {
  120. GroupStore.onUpdateSuccess('1337', undefined, {});
  121. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  122. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  123. });
  124. });
  125. describe('onUpdateError()', function () {
  126. it("should treat undefined itemIds argument as 'all'", function () {
  127. GroupStore.onUpdateError('1337', undefined, false);
  128. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  129. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  130. });
  131. });
  132. describe('onDeleteSuccess()', function () {
  133. it("should treat undefined itemIds argument as 'all'", function () {
  134. GroupStore.onDeleteSuccess('1337', undefined, {});
  135. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  136. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  137. });
  138. });
  139. describe('onAssignToSuccess()', function () {
  140. it("should treat undefined itemIds argument as 'all'", function () {
  141. GroupStore.items = [g('1')];
  142. const assignedGroup = g('1', {assignedTo: TestStubs.User({type: 'user'})});
  143. GroupStore.onAssignToSuccess('1337', '1', assignedGroup);
  144. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  145. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1']));
  146. expect(GroupStore.items[0]).toEqual(assignedGroup);
  147. });
  148. });
  149. describe('updateActivity()', function () {
  150. it("should update activity data text'", function () {
  151. GroupStore.items = [
  152. g('1', {
  153. activity: [
  154. {
  155. id: '1',
  156. type: GroupActivityType.NOTE,
  157. dateCreated: '',
  158. project: Project(),
  159. data: {text: 'Orginal Text'},
  160. },
  161. ],
  162. }),
  163. ];
  164. GroupStore.updateActivity('1', '1', {text: 'Updated Text'});
  165. expect(GroupStore.items[0].activity[0].data).toEqual({text: 'Updated Text'});
  166. });
  167. });
  168. });
  169. });