|
| 1 | +describe('uiScroll', function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + let datasource; |
| 5 | + beforeEach(module('ui.scroll')); |
| 6 | + beforeEach(module('ui.scroll.test.datasources')); |
| 7 | + |
| 8 | + const injectDatasource = (datasourceToken) => |
| 9 | + beforeEach( |
| 10 | + inject([datasourceToken, function (_datasource) { |
| 11 | + datasource = _datasource; |
| 12 | + }]) |
| 13 | + ); |
| 14 | + |
| 15 | + describe('buffer cleanup', function () { |
| 16 | + var getSettings = function () { |
| 17 | + return { |
| 18 | + datasource: 'myEdgeDatasource', |
| 19 | + adapter: 'adapter', |
| 20 | + viewportHeight: 60, |
| 21 | + itemHeight: 20, |
| 22 | + padding: 0.3, |
| 23 | + startIndex: 3, |
| 24 | + bufferSize: 3 |
| 25 | + }; |
| 26 | + }; |
| 27 | + |
| 28 | + injectDatasource('myEdgeDatasource'); |
| 29 | + |
| 30 | + var cleanBuffer = function (scope, applyUpdateOptions) { |
| 31 | + var get = datasource.get; |
| 32 | + var removedItems = []; |
| 33 | + // sync the datasource |
| 34 | + datasource.get = function (index, count, success) { |
| 35 | + var removedIndex = removedItems.indexOf('item' + index); |
| 36 | + if (removedIndex !== -1) { |
| 37 | + // todo consider mutable-top case |
| 38 | + index += removedItems.length;// - removedIndex; |
| 39 | + } |
| 40 | + get(index, count, success); |
| 41 | + }; |
| 42 | + // clean up the buffer |
| 43 | + scope.adapter.applyUpdates(function (item) { |
| 44 | + removedItems.push(item); |
| 45 | + return []; |
| 46 | + }, applyUpdateOptions); |
| 47 | + }; |
| 48 | + |
| 49 | + it('should be consistent on forward direction when eof with immutabeTop', function () { |
| 50 | + runTest(getSettings(), |
| 51 | + function (viewport, scope) { |
| 52 | + expect(scope.adapter.isBOF()).toBe(false); |
| 53 | + expect(scope.adapter.isEOF()).toBe(true); |
| 54 | + |
| 55 | + // remove items 0..6 items form -5..6 datasource |
| 56 | + cleanBuffer(scope, { immutableTop: true }); |
| 57 | + |
| 58 | + // result [-5..-1] |
| 59 | + expect(scope.adapter.isBOF()).toBe(true); |
| 60 | + expect(scope.adapter.isEOF()).toBe(true); |
| 61 | + expect(scope.adapter.bufferFirst).toBe('item-5'); |
| 62 | + expect(scope.adapter.bufferLast).toBe('item-1'); |
| 63 | + expect(scope.adapter.bufferLength).toBe(5); |
| 64 | + } |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should be consistent on forward direction when not eof with immutabeTop', function () { |
| 69 | + var scrollSettings = getSettings(); |
| 70 | + scrollSettings.startIndex = -1; |
| 71 | + scrollSettings.viewportHeight = 40; |
| 72 | + runTest(scrollSettings, |
| 73 | + function (viewport, scope) { |
| 74 | + expect(scope.adapter.isBOF()).toBe(false); |
| 75 | + expect(scope.adapter.isEOF()).toBe(false); |
| 76 | + |
| 77 | + // remove items -4..1 items form -5..6 datasource |
| 78 | + cleanBuffer(scope, { immutableTop: true }); |
| 79 | + |
| 80 | + // result [-5, 2, 3, 4] |
| 81 | + expect(scope.adapter.isBOF()).toBe(true); |
| 82 | + expect(scope.adapter.isEOF()).toBe(false); |
| 83 | + expect(scope.adapter.bufferFirst).toBe('item-5'); |
| 84 | + expect(scope.adapter.bufferLast).toBe('item4'); |
| 85 | + expect(scope.adapter.bufferLength).toBe(4); |
| 86 | + } |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should be consistent on backward direction when bof without immutableTop', function () { |
| 91 | + var scrollSettings = getSettings(); |
| 92 | + scrollSettings.startIndex = -3; |
| 93 | + scrollSettings.padding = 0.5; |
| 94 | + runTest(scrollSettings, |
| 95 | + function (viewport, scope) { |
| 96 | + expect(scope.adapter.isBOF()).toBe(true); |
| 97 | + expect(scope.adapter.isEOF()).toBe(false); |
| 98 | + |
| 99 | + // remove items -5..1 items form -5..6 datasource |
| 100 | + cleanBuffer(scope); |
| 101 | + |
| 102 | + // result [2..6] |
| 103 | + expect(scope.adapter.isBOF()).toBe(true); |
| 104 | + expect(scope.adapter.isEOF()).toBe(true); |
| 105 | + expect(scope.adapter.bufferFirst).toBe('item2'); |
| 106 | + expect(scope.adapter.bufferLast).toBe('item6'); |
| 107 | + expect(scope.adapter.bufferLength).toBe(5); |
| 108 | + } |
| 109 | + ); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | +}); |
0 commit comments