|
1 | 1 | import { expect } from '@esm-bundle/chai'; |
2 | 2 | import { aTimeout, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
3 | 4 | import { Virtualizer } from '../src/virtualizer.js'; |
4 | 5 |
|
5 | 6 | describe('virtualizer', () => { |
@@ -171,6 +172,42 @@ describe('virtualizer', () => { |
171 | 172 | expect(item.getBoundingClientRect().top).to.equal(scrollTarget.getBoundingClientRect().top - 10); |
172 | 173 | }); |
173 | 174 |
|
| 175 | + it('should not request a different set of items on size increase', () => { |
| 176 | + const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`)); |
| 177 | + init({ size: 100, updateElement }); |
| 178 | + |
| 179 | + // Scroll halfway down the list |
| 180 | + updateElement.resetHistory(); |
| 181 | + virtualizer.scrollToIndex(50); |
| 182 | + const updatedIndexes = updateElement.getCalls().map((call) => call.args[1]); |
| 183 | + |
| 184 | + // Increase the size so it shouldn't affect the current viewport items |
| 185 | + updateElement.resetHistory(); |
| 186 | + virtualizer.size = 200; |
| 187 | + const postResizeUpdatedIndexes = updateElement.getCalls().map((call) => call.args[1]); |
| 188 | + |
| 189 | + expect(postResizeUpdatedIndexes).to.eql(updatedIndexes); |
| 190 | + expect(postResizeUpdatedIndexes).not.to.include(0); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should request a different set of items on size decrease', () => { |
| 194 | + const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`)); |
| 195 | + init({ size: 100, updateElement }); |
| 196 | + |
| 197 | + // Scroll halfway down the list |
| 198 | + updateElement.resetHistory(); |
| 199 | + virtualizer.scrollToIndex(50); |
| 200 | + const updatedIndexes = updateElement.getCalls().map((call) => call.args[1]); |
| 201 | + |
| 202 | + // Decrease the size so that we end up at the top of the list |
| 203 | + updateElement.resetHistory(); |
| 204 | + virtualizer.size = 10; |
| 205 | + const postResizeUpdatedIndexes = updateElement.getCalls().map((call) => call.args[1]); |
| 206 | + |
| 207 | + expect(postResizeUpdatedIndexes).not.to.include.members(updatedIndexes); |
| 208 | + expect(postResizeUpdatedIndexes).to.include(0); |
| 209 | + }); |
| 210 | + |
174 | 211 | it('should have physical items once visible', async () => { |
175 | 212 | init({ size: 0 }); |
176 | 213 | // Wait for possibly active resize observers to flush |
|
0 commit comments