groupStore.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. });
  18. describe('remove()', function () {
  19. it('should remove entry', function () {
  20. GroupStore.items = [{id: 1}, {id: 2}];
  21. GroupStore.remove([1]);
  22. expect(GroupStore.items).toEqual([{id: 2}]);
  23. });
  24. it('should remove multiple entries', function () {
  25. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  26. GroupStore.remove([1, 2]);
  27. expect(GroupStore.items).toEqual([{id: 3}]);
  28. });
  29. it('should not remove already removed item', function () {
  30. GroupStore.items = [{id: 1}, {id: 2}];
  31. GroupStore.remove([0]);
  32. expect(GroupStore.items).toEqual([{id: 1}, {id: 2}]);
  33. });
  34. });
  35. describe('onMergeSuccess()', function () {
  36. it('should remove the non-parent merged ids', function () {
  37. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
  38. GroupStore.onMergeSuccess(
  39. null,
  40. [2, 3, 4], // items merged
  41. {merge: {parent: 3}} // merge API response
  42. );
  43. expect(GroupStore.items).toEqual([
  44. {id: 1},
  45. {id: 3}, // parent
  46. ]);
  47. });
  48. });
  49. describe('onPopulateStats()', function () {
  50. const stats = {auto: [[1611576000, 10]]};
  51. beforeAll(function () {
  52. jest.spyOn(GroupStore, 'trigger');
  53. });
  54. beforeEach(function () {
  55. GroupStore.trigger.mockReset();
  56. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  57. });
  58. it('should merge stats into existing groups', function () {
  59. GroupStore.onPopulateStats(
  60. [1, 2, 3],
  61. [
  62. {id: 1, stats},
  63. {id: 2, stats},
  64. {id: 3, stats},
  65. ]
  66. );
  67. expect(GroupStore.getAllItems()[0].stats).toEqual(stats);
  68. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  69. });
  70. it('should not change current item ids', function () {
  71. GroupStore.onPopulateStats(
  72. [2, 3],
  73. [
  74. {id: 2, stats},
  75. {id: 3, stats},
  76. ]
  77. );
  78. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  79. expect(GroupStore.getAllItems()[0].stats).toBeUndefined();
  80. expect(GroupStore.getAllItems()[1].stats).toEqual(stats);
  81. });
  82. });
  83. describe('update methods', function () {
  84. beforeAll(function () {
  85. jest.spyOn(GroupStore, 'trigger');
  86. });
  87. beforeEach(function () {
  88. GroupStore.trigger.mockReset();
  89. });
  90. beforeEach(function () {
  91. GroupStore.items = [{id: 1}, {id: 2}, {id: 3}];
  92. });
  93. describe('onUpdate()', function () {
  94. it("should treat undefined itemIds argument as 'all'", function () {
  95. GroupStore.onUpdate(1337, undefined, 'somedata');
  96. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  97. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  98. });
  99. });
  100. describe('onUpdateSuccess()', function () {
  101. it("should treat undefined itemIds argument as 'all'", function () {
  102. GroupStore.onUpdateSuccess(1337, undefined, 'somedata');
  103. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  104. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  105. });
  106. });
  107. describe('onUpdateError()', function () {
  108. it("should treat undefined itemIds argument as 'all'", function () {
  109. GroupStore.onUpdateError(1337, undefined, 'something failed', false);
  110. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  111. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  112. });
  113. });
  114. describe('onDeleteSuccess()', function () {
  115. it("should treat undefined itemIds argument as 'all'", function () {
  116. GroupStore.onDeleteSuccess(1337, undefined, 'somedata');
  117. expect(GroupStore.trigger).toHaveBeenCalledTimes(1);
  118. expect(GroupStore.trigger).toHaveBeenCalledWith(new Set([1, 2, 3]));
  119. });
  120. });
  121. });
  122. });