streamManager.spec.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {createStore} from 'reflux';
  2. import StreamManager from 'sentry/utils/streamManager';
  3. describe('StreamManager', function () {
  4. let store;
  5. beforeEach(function () {
  6. store = createStore({
  7. add() {},
  8. getAllItems() {},
  9. remove() {},
  10. });
  11. });
  12. it('allows options configuration', function () {
  13. const options = {limit: 2};
  14. const mgr = new StreamManager(store, options);
  15. expect(mgr.limit).toEqual(options.limit);
  16. });
  17. describe('push()', function () {
  18. it('allows passing no items', function () {
  19. const mgr = new StreamManager(store);
  20. expect(() => mgr.push()).not.toThrow();
  21. expect(() => mgr.push([])).not.toThrow();
  22. expect(mgr.idList).toHaveLength(0);
  23. });
  24. it('adds items', function () {
  25. const storeAdd = jest.spyOn(store, 'add');
  26. const mgr = new StreamManager(store);
  27. const items = [{id: 1}];
  28. mgr.push(items);
  29. expect(mgr.idList).toHaveLength(1);
  30. expect(storeAdd).toHaveBeenCalledWith(items);
  31. });
  32. it('allows adding a single item', function () {
  33. const storeAdd = jest.spyOn(store, 'add');
  34. const mgr = new StreamManager(store);
  35. const item = {id: 1};
  36. mgr.push(item);
  37. expect(mgr.idList).toHaveLength(1);
  38. expect(storeAdd).toHaveBeenCalledWith([item]);
  39. });
  40. it('trims after adding', function () {
  41. const mgr = new StreamManager(store, {limit: 1});
  42. const storeRemove = jest.spyOn(store, 'remove');
  43. const mgrTrim = jest.spyOn(mgr, 'trim');
  44. mgr.push([{id: 1}, {id: 2}]);
  45. expect(mgr.idList).toHaveLength(1);
  46. expect(storeRemove).toHaveBeenCalledWith([2]);
  47. expect(mgrTrim).toHaveBeenCalled();
  48. });
  49. it('preserves NEW order of duplicates', function () {
  50. const mgr = new StreamManager(store);
  51. mgr.push([{id: 1}, {id: 3}]);
  52. mgr.push([{id: 1}, {id: 2}]); // New order of "1" if after "3"
  53. expect(mgr.idList).toEqual([3, 1, 2]);
  54. });
  55. });
  56. describe('trim()', function () {
  57. it('removes trailing items in excess of the limit', function () {
  58. const storeRemove = jest.spyOn(store, 'remove');
  59. const mgr = new StreamManager(store, {limit: 1});
  60. mgr.idList = [1, 2, 3];
  61. mgr.trim();
  62. expect(mgr.idList).toEqual([1]);
  63. expect(mgr.idList).toHaveLength(1);
  64. expect(storeRemove).toHaveBeenCalledWith([2, 3]);
  65. });
  66. it('does nothing with fewer items than limit', function () {
  67. const storeRemove = jest.spyOn(store, 'remove');
  68. const mgr = new StreamManager(store, {limit: 10});
  69. mgr.idList = [1, 2, 3];
  70. mgr.trim();
  71. expect(mgr.idList).toEqual([1, 2, 3]);
  72. expect(mgr.idList).toHaveLength(3);
  73. expect(storeRemove).not.toHaveBeenCalled();
  74. });
  75. });
  76. describe('getAllItems()', function () {
  77. it('retrives ordered items from store', function () {
  78. const storeGetAllItems = jest
  79. .spyOn(store, 'getAllItems')
  80. .mockImplementation(() => [{id: 1}, {id: 2}]);
  81. const mgr = new StreamManager(store);
  82. mgr.push({id: 2});
  83. mgr.push({id: 1});
  84. const items = mgr.getAllItems();
  85. expect(items).toEqual([{id: 2}, {id: 1}]);
  86. expect(storeGetAllItems).toHaveBeenCalled();
  87. });
  88. it('does not mutate store', function () {
  89. const storeItems = [{id: 1}, {id: 2}];
  90. jest.spyOn(store, 'getAllItems').mockImplementation(() => storeItems);
  91. const mgr = new StreamManager(store);
  92. mgr.push([{id: 2}, {id: 1}]);
  93. mgr.getAllItems();
  94. expect(store.getAllItems()).toEqual([{id: 1}, {id: 2}]);
  95. });
  96. });
  97. describe('unshift()', function () {
  98. it('adds items to the start of the list', function () {
  99. const storeAdd = jest.spyOn(store, 'add');
  100. const mgr = new StreamManager(store);
  101. mgr.unshift([{id: 2}]);
  102. mgr.unshift([{id: 1}]);
  103. expect(mgr.idList).toEqual([1, 2]);
  104. expect(storeAdd.mock.calls[0][0]).toEqual([{id: 2}]);
  105. expect(storeAdd.mock.calls[1][0]).toEqual([{id: 1}]);
  106. });
  107. it('moves duplicates to the start of the list', function () {
  108. const mgr = new StreamManager(store);
  109. mgr.unshift([{id: 2}, {id: 1}]);
  110. mgr.unshift([{id: 1}]);
  111. expect(mgr.idList).toEqual([1, 2]);
  112. });
  113. it('moves a duplicate array to the start of the list and preserves order', function () {
  114. const mgr = new StreamManager(store);
  115. mgr.unshift([{id: 3}, {id: 2}, {id: 1}]);
  116. mgr.unshift([{id: 2}, {id: 1}]);
  117. expect(mgr.idList).toEqual([2, 1, 3]);
  118. });
  119. it('allows adding a single item', function () {
  120. const mgr = new StreamManager(store);
  121. mgr.unshift({id: 1});
  122. expect(mgr.idList).toEqual([1]);
  123. });
  124. });
  125. });