lazy_loading_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. });