|
1 | 1 | import { expect } from '@esm-bundle/chai'; |
2 | | -import { aTimeout, fixtureSync } from '@vaadin/testing-helpers'; |
| 2 | +import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
3 | 4 | import { Virtualizer } from '../src/virtualizer.js'; |
4 | 5 |
|
5 | 6 | describe('virtualizer - item height', () => { |
@@ -181,3 +182,62 @@ describe('virtualizer - item height - sub-pixel', () => { |
181 | 182 | expect(containerHeight).to.equal(Math.ceil(ITEM_HEIGHT)); |
182 | 183 | }); |
183 | 184 | }); |
| 185 | + |
| 186 | +describe('virtualizer - item height - initial render', () => { |
| 187 | + let virtualizer, elementsContainer, itemHeight, createElements; |
| 188 | + |
| 189 | + beforeEach(() => { |
| 190 | + const scrollTarget = fixtureSync(` |
| 191 | + <div style="height: 300px;"> |
| 192 | + <div class="container"></div> |
| 193 | + </div> |
| 194 | + `); |
| 195 | + const scrollContainer = scrollTarget.firstElementChild; |
| 196 | + elementsContainer = scrollContainer; |
| 197 | + |
| 198 | + createElements = sinon.spy((count) => Array.from({ length: count }, () => document.createElement('div'))); |
| 199 | + |
| 200 | + virtualizer = new Virtualizer({ |
| 201 | + createElements, |
| 202 | + updateElement: (el, index) => { |
| 203 | + el.style.width = '100%'; |
| 204 | + el.style.height = itemHeight; |
| 205 | + el.textContent = `Item ${index}`; |
| 206 | + }, |
| 207 | + scrollTarget, |
| 208 | + scrollContainer, |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + describe('large item height', () => { |
| 213 | + beforeEach(async () => { |
| 214 | + itemHeight = '100px'; |
| 215 | + virtualizer.size = 100; |
| 216 | + await nextFrame(); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should have the expected amount of physical elements', () => { |
| 220 | + expect(elementsContainer.childElementCount).to.equal(5); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should have created the items in the expected amount of batches', () => { |
| 224 | + expect(createElements.callCount).to.equal(2); |
| 225 | + }); |
| 226 | + }); |
| 227 | + |
| 228 | + describe('small item height', () => { |
| 229 | + beforeEach(async () => { |
| 230 | + itemHeight = '20px'; |
| 231 | + virtualizer.size = 100; |
| 232 | + await nextFrame(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should have the expected amount of physical elements', () => { |
| 236 | + expect(elementsContainer.childElementCount).to.equal(20); |
| 237 | + }); |
| 238 | + |
| 239 | + it('should have created the items in the expected amount of batches', () => { |
| 240 | + expect(createElements.callCount).to.equal(2); |
| 241 | + }); |
| 242 | + }); |
| 243 | +}); |
0 commit comments