test(virtual-scroll): adds more tests

This commit is contained in:
Manu Mtz.-Almeida
2018-02-03 08:23:05 +01:00
parent 3dd2d20a5f
commit 0b95214f2c
2 changed files with 26 additions and 13 deletions

View File

@ -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 };

View File

@ -180,20 +180,18 @@ export class VirtualScroll {
this.virtualDom, this.virtualDom,
heightIndex, heightIndex,
this.cells, 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) { private updateCellHeight(cell: Cell, node: HTMLElement) {
(node as any).componentOnReady(() => { (node as any).componentOnReady(() => {
// let's give some additional time to read the height size // 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}`); 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);
} }
} }