groupStore.spec.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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('remove()', function () {
  26. it('should remove entry', function () {
  27. GroupStore.items = [g('1'), g('2')];
  28. GroupStore.remove(['1']);
  29. expect(GroupStore.items).toEqual([g('2')]);
  30. });
  31. it('should remove multiple entries', function () {
  32. GroupStore.items = [g('1'), g('2'), g('3')];
  33. GroupStore.remove(['1', '2']);
  34. expect(GroupStore.items).toEqual([g('3')]);
  35. });
  36. it('should not remove already removed item', function () {
  37. GroupStore.items = [g('1'), g('2')];
  38. GroupStore.remove(['0']);
  39. expect(GroupStore.items).toEqual([g('1'), g('2')]);
  40. });
  41. });
  42. describe('onMergeSuccess()', function () {
  43. it('should remove the non-parent merged ids', function () {
  44. GroupStore.items = [g('1'), g('2'), g('3'), g('4')];
  45. GroupStore.onMergeSuccess(
  46. '',
  47. ['2', '3', '4'], // items merged
  48. {merge: {parent: '3'}} // merge API response
  49. );
  50. expect(GroupStore.items).toEqual([
  51. g('1'),
  52. g('3'), // parent
  53. ]);
  54. });
  55. });
  56. describe('onPopulateStats()', function () {
  57. let triggerSpy: jest.SpyInstance;
  58. const stats: Record<string, TimeseriesValue[]> = {auto: [[1611576000, 10]]};
  59. beforeAll(function () {
  60. triggerSpy = jest.spyOn(GroupStore, 'trigger');
  61. });
  62. beforeEach(function () {
  63. triggerSpy.mockReset();
  64. GroupStore.items = [g('1'), g('2'), g('3')];
  65. });
  66. it('should merge stats into existing groups', function () {
  67. GroupStore.onPopulateStats(
  68. ['1', '2', '3'],
  69. [
  70. {id: '1', stats} as GroupStats,
  71. {id: '2', stats} as GroupStats,
  72. {id: '3', stats} as GroupStats,
  73. ]
  74. );
  75. const group = GroupStore.getAllItems()[0] as Group;
  76. expect(group.stats).toEqual(stats);
  77. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  78. });
  79. it('should not change current item ids', function () {
  80. GroupStore.onPopulateStats(
  81. ['2', '3'],
  82. [{id: '2', stats} as GroupStats, {id: '3', stats} as GroupStats]
  83. );
  84. const group1 = GroupStore.getAllItems()[0] as Group;
  85. const group2 = GroupStore.getAllItems()[1] as Group;
  86. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['2', '3']));
  87. expect(group1.stats).not.toEqual(stats);
  88. expect(group2.stats).toEqual(stats);
  89. });
  90. });
  91. describe('getAllItems()', function () {
  92. it('Merges pending changes into items', function () {
  93. GroupStore.items = [];
  94. GroupStore.add([g('1'), g('2')]);
  95. GroupStore.onUpdate('1337', ['1'], {someChange: true});
  96. expect(GroupStore.get('1')).toEqual(
  97. expect.objectContaining({id: '1', someChange: true})
  98. );
  99. });
  100. });
  101. describe('update methods', function () {
  102. let triggerSpy: jest.SpyInstance;
  103. beforeAll(function () {
  104. triggerSpy = jest.spyOn(GroupStore, 'trigger');
  105. });
  106. beforeEach(function () {
  107. triggerSpy.mockReset();
  108. });
  109. beforeEach(function () {
  110. GroupStore.items = [g('1'), g('2'), g('3')];
  111. });
  112. describe('onUpdate()', function () {
  113. it("should treat undefined itemIds argument as 'all'", function () {
  114. GroupStore.onUpdate('1337', undefined, {});
  115. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  116. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  117. });
  118. });
  119. describe('onUpdateSuccess()', function () {
  120. it("should treat undefined itemIds argument as 'all'", function () {
  121. GroupStore.onUpdateSuccess('1337', undefined, {});
  122. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  123. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  124. });
  125. });
  126. describe('onUpdateError()', function () {
  127. it("should treat undefined itemIds argument as 'all'", function () {
  128. GroupStore.onUpdateError('1337', undefined, false);
  129. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  130. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  131. });
  132. });
  133. describe('onDeleteSuccess()', function () {
  134. it("should treat undefined itemIds argument as 'all'", function () {
  135. GroupStore.onDeleteSuccess('1337', undefined, {});
  136. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  137. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set(['1', '2', '3']));
  138. });
  139. });
  140. });
  141. });