mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-22 13:32:54 +08:00
test(virtual-scroll): adds more tests
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { CellType, HeaderFn, ItemHeightFn, VirtualNode, calcCells, calcHeightIndex, getRange, getViewport, resizeBuffer, updateVDom, ItemRenderFn, Range, getShouldUpdate } from '../virtual-scroll-utils';
|
||||
import { CellType, HeaderFn, ItemHeightFn, VirtualNode, calcCells, calcHeightIndex, getRange, getViewport, resizeBuffer, updateVDom, ItemRenderFn, Range, getShouldUpdate, positionForIndex } from '../virtual-scroll-utils';
|
||||
|
||||
|
||||
describe('getViewport', () => {
|
||||
@ -351,6 +351,21 @@ describe('getShouldUpdate', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('positionForIndex', () => {
|
||||
it('should return the correct position', () => {
|
||||
const items = [1, 2, 3, 4];
|
||||
const {cells, heightIndex} = mockVirtualScroll(items,
|
||||
() => 40,
|
||||
(_, i) => i === 1 ? 'hola' : null,
|
||||
(_, i) => i === 2 ? 'hola' : null
|
||||
);
|
||||
expect(positionForIndex(0, cells, heightIndex)).toEqual(0);
|
||||
expect(positionForIndex(1, cells, heightIndex)).toEqual(50);
|
||||
expect(positionForIndex(2, cells, heightIndex)).toEqual(90);
|
||||
expect(positionForIndex(3, cells, heightIndex)).toEqual(140);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateVDom', () => {
|
||||
it('should initialize empty VDOM', () => {
|
||||
const vdom: VirtualNode[] = [];
|
||||
@ -491,7 +506,7 @@ function mockVirtualScroll(
|
||||
headerFn: HeaderFn = null,
|
||||
footerFn: HeaderFn = null
|
||||
) {
|
||||
const cells = calcCells(items, itemHeight, headerFn, footerFn, 10, 20, 30);
|
||||
const cells = calcCells(items, itemHeight, headerFn, footerFn, 10, 10, 30);
|
||||
const heightIndex = resizeBuffer(null, cells.length);
|
||||
calcHeightIndex(heightIndex, cells, 0);
|
||||
return { items, heightIndex, cells };
|
||||
|
@ -180,20 +180,18 @@ export class VirtualScroll {
|
||||
this.virtualDom,
|
||||
heightIndex,
|
||||
this.cells,
|
||||
range);
|
||||
range
|
||||
);
|
||||
|
||||
this.fireDomUpdate();
|
||||
// write DOM
|
||||
if (this.itemRender) {
|
||||
doRender(this.el, this.itemRender, this.virtualDom, this.updateCellHeight.bind(this), this.totalHeight);
|
||||
} else if (this.domRender) {
|
||||
this.domRender(this.virtualDom, this.totalHeight);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private fireDomUpdate() {
|
||||
if (this.itemRender) {
|
||||
doRender(this.el, this.itemRender, this.virtualDom, this.updateCellHeight.bind(this), this.totalHeight);
|
||||
} else if (this.domRender) {
|
||||
this.domRender(this.virtualDom, this.totalHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private updateCellHeight(cell: Cell, node: HTMLElement) {
|
||||
(node as any).componentOnReady(() => {
|
||||
// let's give some additional time to read the height size
|
||||
@ -219,8 +217,8 @@ export class VirtualScroll {
|
||||
console.debug(`[${cell.reads}] cell size ${cell.height} -> ${height}`);
|
||||
cell.height = height;
|
||||
clearTimeout(this.timerUpdate);
|
||||
this.indexDirty = Math.min(this.indexDirty, index);
|
||||
this.timerUpdate = setTimeout(() => this.updateVirtualScroll(), 100);
|
||||
this.indexDirty = Math.min(this.indexDirty, index);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user