From 0b95214f2ce8a8a04ed38c2c78ab44dc039d70b9 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Sat, 3 Feb 2018 08:23:05 +0100 Subject: [PATCH] test(virtual-scroll): adds more tests --- .../test/virtual-scroll-utils.spec.ts | 19 ++++++++++++++++-- .../virtual-scroll/virtual-scroll.tsx | 20 +++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/core/src/components/virtual-scroll/test/virtual-scroll-utils.spec.ts b/packages/core/src/components/virtual-scroll/test/virtual-scroll-utils.spec.ts index 607dd9a2b2..affbbe0397 100644 --- a/packages/core/src/components/virtual-scroll/test/virtual-scroll-utils.spec.ts +++ b/packages/core/src/components/virtual-scroll/test/virtual-scroll-utils.spec.ts @@ -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 }; diff --git a/packages/core/src/components/virtual-scroll/virtual-scroll.tsx b/packages/core/src/components/virtual-scroll/virtual-scroll.tsx index 1ba62dff0c..c5aacd0f2f 100644 --- a/packages/core/src/components/virtual-scroll/virtual-scroll.tsx +++ b/packages/core/src/components/virtual-scroll/virtual-scroll.tsx @@ -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); } }