streamManager.spec.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import Reflux from 'reflux';
  2. import StreamManager from 'app/utils/streamManager';
  3. describe('StreamManager', function() {
  4. let store;
  5. beforeEach(function() {
  6. store = Reflux.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, expect.anything(), expect.anything());
  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.mock.calls[0][0]).toEqual(2);
  65. expect(storeRemove.mock.calls[1][0]).toEqual(3);
  66. });
  67. it('does nothing with fewer items than limit', function() {
  68. const storeRemove = jest.spyOn(store, 'remove');
  69. const mgr = new StreamManager(store, {limit: 10});
  70. mgr.idList = [1, 2, 3];
  71. mgr.trim();
  72. expect(mgr.idList).toEqual([1, 2, 3]);
  73. expect(mgr.idList).toHaveLength(3);
  74. expect(storeRemove).not.toHaveBeenCalled();
  75. });
  76. });
  77. describe('getAllItems()', function() {
  78. it('retrives ordered items from store', function() {
  79. const storeGetAllItems = jest
  80. .spyOn(store, 'getAllItems')
  81. .mockImplementation(() => [{id: 1}, {id: 2}]);
  82. const mgr = new StreamManager(store);
  83. mgr.push({id: 2});
  84. mgr.push({id: 1});
  85. const items = mgr.getAllItems();
  86. expect(items).toEqual([{id: 2}, {id: 1}]);
  87. expect(storeGetAllItems).toHaveBeenCalled();
  88. });
  89. it('does not mutate store', function() {
  90. const storeItems = [{id: 1}, {id: 2}];
  91. jest.spyOn(store, 'getAllItems').mockImplementation(() => storeItems);
  92. const mgr = new StreamManager(store);
  93. mgr.push([{id: 2}, {id: 1}]);
  94. mgr.getAllItems();
  95. expect(store.getAllItems()).toEqual([{id: 1}, {id: 2}]);
  96. });
  97. });
  98. describe('unshift()', function() {
  99. it('adds items to the start of the list', function() {
  100. const storeAdd = jest.spyOn(store, 'add');
  101. const mgr = new StreamManager(store);
  102. mgr.unshift([{id: 2}]);
  103. mgr.unshift([{id: 1}]);
  104. expect(mgr.idList).toEqual([1, 2]);
  105. expect(storeAdd.mock.calls[0][0]).toEqual([{id: 2}]);
  106. expect(storeAdd.mock.calls[1][0]).toEqual([{id: 1}]);
  107. });
  108. it('moves duplicates to the start of the list', function() {
  109. const mgr = new StreamManager(store);
  110. mgr.unshift([{id: 2}, {id: 1}]);
  111. mgr.unshift([{id: 1}]);
  112. expect(mgr.idList).toEqual([1, 2]);
  113. });
  114. it('moves a duplicate array to the start of the list and preserves order', function() {
  115. const mgr = new StreamManager(store);
  116. mgr.unshift([{id: 3}, {id: 2}, {id: 1}]);
  117. mgr.unshift([{id: 2}, {id: 1}]);
  118. expect(mgr.idList).toEqual([2, 1, 3]);
  119. });
  120. it('allows adding a single item', function() {
  121. const mgr = new StreamManager(store);
  122. mgr.unshift({id: 1});
  123. expect(mgr.idList).toEqual([1]);
  124. });
  125. });
  126. });