lazy_loading_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var lazyLoading = require('utils/lazy_loading');
  19. describe('lazy_loading', function () {
  20. describe('#run()', function () {
  21. var context = Em.Object.create({isLoaded: false});
  22. var options = {
  23. destination: [],
  24. source: [{'test':'test'}],
  25. context: context
  26. };
  27. it('load one item', function () {
  28. lazyLoading.run(options);
  29. expect(options.destination[0]).to.eql(options.source[0]);
  30. expect(context.get('isLoaded')).to.equal(true);
  31. });
  32. var testsInfo = [
  33. {
  34. title: 'load 11 item with initSize - 11',
  35. result: true,
  36. initSize: 11,
  37. destinationLength: 11,
  38. destination: [],
  39. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  40. context: Em.Object.create()
  41. },
  42. {
  43. title: 'load 11 item with initSize - 12',
  44. result: true,
  45. initSize: 12,
  46. destinationLength: 11,
  47. destination: [],
  48. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  49. context: Em.Object.create()
  50. },
  51. {//items will be completely loaded on next iteration of pushing chunk
  52. title: 'load 11 item with initSize - 10',
  53. result: false,
  54. initSize: 10,
  55. destinationLength: 10,
  56. destination: [],
  57. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  58. context: Em.Object.create({isLoaded: false})
  59. }
  60. ];
  61. testsInfo.forEach(function(test){
  62. it(test.title, function () {
  63. lazyLoading.run(test);
  64. expect(test.destinationLength).to.equal(test.destination.length);
  65. expect(test.context.get('isLoaded')).to.equal(test.result);
  66. });
  67. });
  68. });
  69. describe('#divideIntoChunks()', function () {
  70. var testsInfo = [
  71. {
  72. title: 'load 11 item with chunkSize - 3',
  73. chunkSize: 3,
  74. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  75. chunks: [[{i:1}, {i:2}, {i:3}], [{i:4}, {i:5}, {i:6}], [{i:7}, {i:8}, {i:9}], [{i:10},{i:11}]]
  76. },
  77. {
  78. title: 'load 11 item with chunkSize - 0',
  79. chunkSize: 0,
  80. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  81. chunks: [[{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}]]
  82. },
  83. {
  84. title: 'load 11 item with chunkSize - 1',
  85. chunkSize: 1,
  86. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  87. chunks: [[{i:1}], [{i:2}], [{i:3}], [{i:4}], [{i:5}], [{i:6}], [{i:7}], [{i:8}], [{i:9}], [{i:10}], [{i:11}]]
  88. },
  89. {
  90. title: 'load 11 item with chunkSize - 11',
  91. chunkSize: 0,
  92. source: [{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}],
  93. chunks: [[{i:1}, {i:2}, {i:3}, {i:4}, {i:5}, {i:6}, {i:7}, {i:8}, {i:9}, {i:10},{i:11}]]
  94. }
  95. ];
  96. testsInfo.forEach(function(test){
  97. it(test.title, function () {
  98. var chunks = lazyLoading.divideIntoChunks(test.source, test.chunkSize);
  99. expect(chunks).to.eql(test.chunks);
  100. });
  101. });
  102. });
  103. describe("#pushChunk()", function() {
  104. beforeEach(function () {
  105. this.clock = sinon.useFakeTimers();
  106. sinon.spy(lazyLoading, 'pushChunk');
  107. });
  108. afterEach(function () {
  109. this.clock.restore();
  110. lazyLoading.pushChunk.restore();
  111. });
  112. it("last chunk", function() {
  113. this.clock = sinon.useFakeTimers();
  114. var destination = [],
  115. chunks = [[1]],
  116. context = Em.Object.create(),
  117. instance = {
  118. context: context,
  119. timeoutRef: null,
  120. terminate: Em.K
  121. };
  122. lazyLoading.pushChunk(chunks, 0, 10, destination, instance);
  123. this.clock.tick(10);
  124. expect(destination[0]).to.equal(1);
  125. expect(context.get('isLoaded')).to.be.true;
  126. expect(lazyLoading.pushChunk.calledTwice).to.be.false;
  127. });
  128. it("two chunks", function() {
  129. this.clock = sinon.useFakeTimers();
  130. var destination = [],
  131. chunks = [[1], [2]],
  132. context = Em.Object.create(),
  133. instance = {
  134. context: context,
  135. timeoutRef: null,
  136. terminate: Em.K
  137. };
  138. lazyLoading.pushChunk(chunks, 0, 10, destination, instance);
  139. this.clock.tick(20);
  140. expect(destination.length).to.equal(2);
  141. expect(context.get('isLoaded')).to.be.true;
  142. expect(lazyLoading.pushChunk.calledTwice).to.be.true;
  143. });
  144. it("terminated chunks", function() {
  145. this.clock = sinon.useFakeTimers();
  146. var destination = [],
  147. chunks = [[1]],
  148. context = Em.Object.create({isLoaded: false}),
  149. instance = {
  150. context: context,
  151. timeoutRef: null,
  152. terminate: Em.K
  153. };
  154. lazyLoading.pushChunk(chunks, 0, 10, destination, instance);
  155. clearTimeout(instance.timeoutRef);
  156. this.clock.tick(10);
  157. expect(destination.length).to.empty;
  158. expect(context.get('isLoaded')).to.be.false;
  159. expect(lazyLoading.pushChunk.calledTwice).to.be.false;
  160. });
  161. });
  162. describe("#terminate()", function() {
  163. before(function () {
  164. sinon.spy(lazyLoading, 'pushChunk');
  165. this.clock = sinon.useFakeTimers();
  166. });
  167. after(function () {
  168. lazyLoading.pushChunk.restore();
  169. this.clock.restore();
  170. });
  171. it("loading terminated", function() {
  172. var context = Em.Object.create({isLoaded: false});
  173. var options = {
  174. destination: [],
  175. source: [1, 2],
  176. delay: 10,
  177. chunkSize: 1,
  178. initSize: 1,
  179. context: context
  180. };
  181. var ll = lazyLoading.run(options);
  182. lazyLoading.terminate(ll);
  183. this.clock.tick(10);
  184. expect(options.destination.length).to.equal(1);
  185. expect(context.get('isLoaded')).to.be.false;
  186. expect(lazyLoading.pushChunk.calledTwice).to.be.false;
  187. });
  188. });
  189. });