streamManager.spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. let options = {limit: 2};
  14. let 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. let 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. let storeAdd = sinon.spy(store, 'add');
  26. let mgr = new StreamManager(store);
  27. let items = [{id: 1}];
  28. mgr.push(items);
  29. expect(mgr.idList).toHaveLength(1);
  30. expect(storeAdd.calledWith(items)).toBe(true);
  31. });
  32. it('allows adding a single item', function() {
  33. let storeAdd = sinon.spy(store, 'add');
  34. let mgr = new StreamManager(store);
  35. let item = {id: 1};
  36. mgr.push(item);
  37. expect(mgr.idList).toHaveLength(1);
  38. expect(storeAdd.calledWith([item])).toBe(true);
  39. });
  40. it('trims after adding', function() {
  41. let mgr = new StreamManager(store, {limit: 1});
  42. let storeRemove = sinon.spy(store, 'remove');
  43. let mgrTrim = sinon.spy(mgr, 'trim');
  44. mgr.push([{id: 1}, {id: 2}]);
  45. expect(mgr.idList).toHaveLength(1);
  46. expect(storeRemove.calledWith(2)).toBe(true);
  47. expect(mgrTrim.called).toBe(true);
  48. });
  49. it('preserves NEW order of duplicates', function() {
  50. let 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. let storeRemove = sinon.spy(store, 'remove');
  59. let 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.firstCall.calledWith(2)).toBe(true);
  65. expect(storeRemove.secondCall.calledWith(3)).toBe(true);
  66. });
  67. it('does nothing with fewer items than limit', function() {
  68. let storeRemove = sinon.spy(store, 'remove');
  69. let 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.called).toBe(false);
  75. });
  76. });
  77. describe('getAllItems()', function() {
  78. it('retrives ordered items from store', function() {
  79. let storeGetAllItems = sinon.stub(store, 'getAllItems', function() {
  80. return [{id: 1}, {id: 2}];
  81. });
  82. let mgr = new StreamManager(store);
  83. mgr.push({id: 2});
  84. mgr.push({id: 1});
  85. let items = mgr.getAllItems();
  86. expect(items).toEqual([{id: 2}, {id: 1}]);
  87. expect(storeGetAllItems.called).toBe(true);
  88. });
  89. it('does not mutate store', function() {
  90. let storeItems = [{id: 1}, {id: 2}];
  91. sinon.stub(store, 'getAllItems', function() {
  92. return storeItems;
  93. });
  94. let mgr = new StreamManager(store);
  95. mgr.push([{id: 2}, {id: 1}]);
  96. mgr.getAllItems();
  97. expect(store.getAllItems()).toEqual([{id: 1}, {id: 2}]);
  98. });
  99. });
  100. describe('unshift()', function() {
  101. it('adds items to the start of the list', function() {
  102. let storeAdd = sinon.spy(store, 'add');
  103. let mgr = new StreamManager(store);
  104. mgr.unshift([{id: 2}]);
  105. mgr.unshift([{id: 1}]);
  106. expect(mgr.idList).toEqual([1, 2]);
  107. expect(storeAdd.firstCall.calledWith([{id: 2}])).toBe(true);
  108. expect(storeAdd.secondCall.calledWith([{id: 1}])).toBe(true);
  109. });
  110. it('moves duplicates to the start of the list', function() {
  111. let mgr = new StreamManager(store);
  112. mgr.unshift([{id: 2}, {id: 1}]);
  113. mgr.unshift([{id: 1}]);
  114. expect(mgr.idList).toEqual([1, 2]);
  115. });
  116. it('moves a duplicate array to the start of the list and preserves order', function() {
  117. let mgr = new StreamManager(store);
  118. mgr.unshift([{id: 3}, {id: 2}, {id: 1}]);
  119. mgr.unshift([{id: 2}, {id: 1}]);
  120. expect(mgr.idList).toEqual([2, 1, 3]);
  121. });
  122. it('allows adding a single item', function() {
  123. let mgr = new StreamManager(store);
  124. mgr.unshift({id: 1});
  125. expect(mgr.idList).toEqual([1]);
  126. });
  127. });
  128. });