groupStore.spec.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import GroupStore from 'sentry/stores/groupStore';
  2. describe('GroupStore', function () {
  3. beforeEach(function () {
  4. GroupStore.reset();
  5. });
  6. describe('add()', function () {
  7. it('should add new entries', function () {
  8. GroupStore.items = [];
  9. GroupStore.add([{id: 1}, {id: 2}]);
  10. expect(GroupStore.items).toEqual([{id: 1}, {id: 2}]);
  11. });
  12. it('should update matching existing entries', function () {
  13. GroupStore.items = [{id: 1}, {id: 2}];
  14. GroupStore.add([{id: 1, foo: 'bar'}, {id: 3}]);
  15. expect(GroupStore.items).toEqual([{id: 1, foo: 'bar'}, {id: 2}, {id: 3}]);
  16. });
  17. it('should attempt to preserve order of ids', function () {
  18. GroupStore.add([{id: 2}, {id: 1}, {id: 3}]);
  19. expect(GroupStore.getAllItemIds()).toEqual([2, 1, 3]);
  20. });
  21. });
  22. describe('remove()', function () {
  23. it('should remove entry', function () {
  24. GroupStore.items = [{id: 1}, {id: 2}];
  25. GroupStore.remove([1]);
  26. expect(GroupStore.items).toEqual([{id: 2}]);
  27. });
  28. it('should remove multiple entries', function () {
  29. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  30. GroupStore.remove([1, 2]);
  31. expect(GroupStore.items).toEqual([{id: 3}]);
  32. });
  33. it('should not remove already removed item', function () {
  34. GroupStore.items = [{id: 1}, {id: 2}];
  35. GroupStore.remove([0]);
  36. expect(GroupStore.items).toEqual([{id: 1}, {id: 2}]);
  37. });
  38. });
  39. describe('onMergeSuccess()', function () {
  40. it('should remove the non-parent merged ids', function () {
  41. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
  42. GroupStore.onMergeSuccess(
  43. null,
  44. [2, 3, 4], // items merged
  45. {merge: {parent: 3}} // merge API response
  46. );
  47. expect(GroupStore.items).toEqual([
  48. {id: 1},
  49. {id: 3}, // parent
  50. ]);
  51. });
  52. });
  53. describe('onPopulateStats()', function () {
  54. const stats = {auto: [[1611576000, 10]]};
  55. beforeAll(function () {
  56. jest.spyOn(GroupStore, 'trigger');
  57. });
  58. beforeEach(function () {
  59. GroupStore.trigger.mockReset();
  60. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  61. });
  62. it('should merge stats into existing groups', function () {
  63. GroupStore.onPopulateStats(
  64. [1, 2, 3],
  65. [
  66. {id: 1, stats},
  67. {id: 2, stats},
  68. {id: 3, stats},
  69. ]
  70. );
  71. expect(GroupStore.getAllItems()[0].stats).toEqual(stats);
  72. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  73. });
  74. it('should not change current item ids', function () {
  75. GroupStore.onPopulateStats(
  76. [2, 3],
  77. [
  78. {id: 2, stats},
  79. {id: 3, stats},
  80. ]
  81. );
  82. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  83. expect(GroupStore.getAllItems()[0].stats).toBeUndefined();
  84. expect(GroupStore.getAllItems()[1].stats).toEqual(stats);
  85. });
  86. });
  87. describe('getAllItems()', function () {
  88. it('Merges pending changes into items', function () {
  89. GroupStore.items = [];
  90. GroupStore.add([{id: 1}, {id: 2}]);
  91. GroupStore.onUpdate(1337, [1], {someChange: true});
  92. expect(GroupStore.getAllItems()).toEqual([{id: 1, someChange: true}, {id: 2}]);
  93. });
  94. });
  95. describe('update methods', function () {
  96. beforeAll(function () {
  97. jest.spyOn(GroupStore, 'trigger');
  98. });
  99. beforeEach(function () {
  100. GroupStore.trigger.mockReset();
  101. });
  102. beforeEach(function () {
  103. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  104. });
  105. describe('onUpdate()', function () {
  106. it("should treat undefined itemIds argument as 'all'", function () {
  107. GroupStore.onUpdate(1337, undefined, 'somedata');
  108. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  109. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  110. });
  111. });
  112. describe('onUpdateSuccess()', function () {
  113. it("should treat undefined itemIds argument as 'all'", function () {
  114. GroupStore.onUpdateSuccess(1337, undefined, 'somedata');
  115. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  116. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  117. });
  118. });
  119. describe('onUpdateError()', function () {
  120. it("should treat undefined itemIds argument as 'all'", function () {
  121. GroupStore.onUpdateError(1337, undefined, 'something failed', false);
  122. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  123. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  124. });
  125. });
  126. describe('onDeleteSuccess()', function () {
  127. it("should treat undefined itemIds argument as 'all'", function () {
  128. GroupStore.onDeleteSuccess(1337, undefined, 'somedata');
  129. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  130. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  131. });
  132. });
  133. });
  134. });