| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2022 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {expect} from '@open-wc/testing'; |
| 8 |
import {ignoreBenignErrors, last} from '../helpers.js'; |
| 9 |
import { |
| 10 |
virtualizerFixture, |
| 11 |
DefaultItem, |
| 12 |
} from '../virtualizer-test-utilities.js'; |
| 13 |
|
| 14 |
describe('keyFunction', () => { |
| 15 |
ignoreBenignErrors(beforeEach, afterEach); |
| 16 |
|
| 17 |
describe('<lit-virtualizer>', () => { |
| 18 |
it('maintains item-element correspondence with no user-provided keyFunction', async () => { |
| 19 |
const {inspector, host, virtualizer} = await virtualizerFixture(); |
| 20 |
const refEl = last(inspector.childElements); |
| 21 |
const origText = refEl.textContent; |
| 22 |
host.scrollTo({top: 2000}); |
| 23 |
await virtualizer.layoutComplete; |
| 24 |
expect(refEl.textContent).to.equal(origText); |
| 25 |
}); |
| 26 |
it('maintains item-element correspondence with an item-based keyFunction', async () => { |
| 27 |
const {inspector, host, virtualizer} = await virtualizerFixture({ |
| 28 |
keyFunction: (item: DefaultItem, _idx) => item.index, |
| 29 |
}); |
| 30 |
const refEl = last(inspector.childElements); |
| 31 |
const origText = refEl.textContent; |
| 32 |
host.scrollTo({top: 2000}); |
| 33 |
await virtualizer.layoutComplete; |
| 34 |
expect(refEl.textContent).to.equal(origText); |
| 35 |
}); |
| 36 |
it('maintains item-element correspondence with an index-based keyFunction', async () => { |
| 37 |
const {inspector, host, virtualizer} = await virtualizerFixture({ |
| 38 |
keyFunction: (_item, idx) => idx, |
| 39 |
}); |
| 40 |
const refEl = last(inspector.childElements); |
| 41 |
const origText = refEl.textContent; |
| 42 |
host.scrollTo({top: 2000}); |
| 43 |
await virtualizer.layoutComplete; |
| 44 |
expect(refEl.textContent).to.equal(origText); |
| 45 |
}); |
| 46 |
}); |
| 47 |
|
| 48 |
describe('virtualize() directive', () => { |
| 49 |
it('maintains item-element correspondence with no user-provided keyFunction', async () => { |
| 50 |
const {inspector, host, virtualizer} = await virtualizerFixture({ |
| 51 |
useDirective: true, |
| 52 |
}); |
| 53 |
const refEl = last(inspector.childElements); |
| 54 |
const origText = refEl.textContent; |
| 55 |
host.scrollTo({top: 2000}); |
| 56 |
await virtualizer.layoutComplete; |
| 57 |
expect(refEl.textContent).to.equal(origText); |
| 58 |
}); |
| 59 |
it('maintains item-element correspondence with an item-based keyFunction', async () => { |
| 60 |
const {inspector, host, virtualizer} = await virtualizerFixture({ |
| 61 |
useDirective: true, |
| 62 |
keyFunction: (item: DefaultItem, _idx) => item.index, |
| 63 |
}); |
| 64 |
const refEl = last(inspector.childElements); |
| 65 |
const origText = refEl.textContent; |
| 66 |
host.scrollTo({top: 2000}); |
| 67 |
await virtualizer.layoutComplete; |
| 68 |
expect(refEl.textContent).to.equal(origText); |
| 69 |
}); |
| 70 |
it('maintains item-element correspondence with an index-based keyFunction', async () => { |
| 71 |
const {inspector, host, virtualizer} = await virtualizerFixture({ |
| 72 |
useDirective: true, |
| 73 |
keyFunction: (_item, idx) => idx, |
| 74 |
}); |
| 75 |
const refEl = last(inspector.childElements); |
| 76 |
const origText = refEl.textContent; |
| 77 |
host.scrollTo({top: 2000}); |
| 78 |
await virtualizer.layoutComplete; |
| 79 |
expect(refEl.textContent).to.equal(origText); |
| 80 |
}); |
| 81 |
}); |
| 82 |
}); |