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', () => {
|
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', () => {
|
describe('updateVDom', () => {
|
||||||
it('should initialize empty VDOM', () => {
|
it('should initialize empty VDOM', () => {
|
||||||
const vdom: VirtualNode[] = [];
|
const vdom: VirtualNode[] = [];
|
||||||
@ -491,7 +506,7 @@ function mockVirtualScroll(
|
|||||||
headerFn: HeaderFn = null,
|
headerFn: HeaderFn = null,
|
||||||
footerFn: 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);
|
const heightIndex = resizeBuffer(null, cells.length);
|
||||||
calcHeightIndex(heightIndex, cells, 0);
|
calcHeightIndex(heightIndex, cells, 0);
|
||||||
return { items, heightIndex, cells };
|
return { items, heightIndex, cells };
|
||||||
|
@ -180,18 +180,16 @@ export class VirtualScroll {
|
|||||||
this.virtualDom,
|
this.virtualDom,
|
||||||
heightIndex,
|
heightIndex,
|
||||||
this.cells,
|
this.cells,
|
||||||
range);
|
range
|
||||||
|
);
|
||||||
|
|
||||||
this.fireDomUpdate();
|
// write DOM
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private fireDomUpdate() {
|
|
||||||
if (this.itemRender) {
|
if (this.itemRender) {
|
||||||
doRender(this.el, this.itemRender, this.virtualDom, this.updateCellHeight.bind(this), this.totalHeight);
|
doRender(this.el, this.itemRender, this.virtualDom, this.updateCellHeight.bind(this), this.totalHeight);
|
||||||
} else if (this.domRender) {
|
} else if (this.domRender) {
|
||||||
this.domRender(this.virtualDom, this.totalHeight);
|
this.domRender(this.virtualDom, this.totalHeight);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateCellHeight(cell: Cell, node: HTMLElement) {
|
private updateCellHeight(cell: Cell, node: HTMLElement) {
|
||||||
@ -219,8 +217,8 @@ export class VirtualScroll {
|
|||||||
console.debug(`[${cell.reads}] cell size ${cell.height} -> ${height}`);
|
console.debug(`[${cell.reads}] cell size ${cell.height} -> ${height}`);
|
||||||
cell.height = height;
|
cell.height = height;
|
||||||
clearTimeout(this.timerUpdate);
|
clearTimeout(this.timerUpdate);
|
||||||
this.indexDirty = Math.min(this.indexDirty, index);
|
|
||||||
this.timerUpdate = setTimeout(() => this.updateVirtualScroll(), 100);
|
this.timerUpdate = setTimeout(() => this.updateVirtualScroll(), 100);
|
||||||
|
this.indexDirty = Math.min(this.indexDirty, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user