groupStore.spec.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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('update methods', function () {
  88. beforeAll(function () {
  89. jest.spyOn(GroupStore, 'trigger');
  90. });
  91. beforeEach(function () {
  92. GroupStore.trigger.mockReset();
  93. });
  94. beforeEach(function () {
  95. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  96. });
  97. describe('onUpdate()', function () {
  98. it("should treat undefined itemIds argument as 'all'", function () {
  99. GroupStore.onUpdate(1337, undefined, 'somedata');
  100. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  101. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  102. });
  103. });
  104. describe('onUpdateSuccess()', function () {
  105. it("should treat undefined itemIds argument as 'all'", function () {
  106. GroupStore.onUpdateSuccess(1337, undefined, 'somedata');
  107. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  108. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  109. });
  110. });
  111. describe('onUpdateError()', function () {
  112. it("should treat undefined itemIds argument as 'all'", function () {
  113. GroupStore.onUpdateError(1337, undefined, 'something failed', false);
  114. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  115. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  116. });
  117. });
  118. describe('onDeleteSuccess()', function () {
  119. it("should treat undefined itemIds argument as 'all'", function () {
  120. GroupStore.onDeleteSuccess(1337, undefined, 'somedata');
  121. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  122. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  123. });
  124. });
  125. });
  126. });