| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2022 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {ignoreBenignErrors} from '../helpers.js'; |
| 8 |
import {html, LitElement} from 'lit'; |
| 9 |
import {customElement} from 'lit/decorators.js'; |
| 10 |
import {LitVirtualizer} from '../../lit-virtualizer.js'; |
| 11 |
import '../../lit-virtualizer.js'; |
| 12 |
import {expect, html as testingHtml, fixture} from '@open-wc/testing'; |
| 13 |
|
| 14 |
interface ExampleItem { |
| 15 |
text: string; |
| 16 |
} |
| 17 |
|
| 18 |
@customElement('my-example') |
| 19 |
export class MyExample extends LitElement { |
| 20 |
items: ExampleItem[] = new Array(100) |
| 21 |
.fill('') |
| 22 |
.map((_, i) => ({text: `Item ${i}`})); |
| 23 |
|
| 24 |
render() { |
| 25 |
return html` |
| 26 |
<lit-virtualizer |
| 27 |
.items=${this.items} |
| 28 |
.renderItem=${(item: ExampleItem) => html`<p>${item.text}</p>`} |
| 29 |
></lit-virtualizer> |
| 30 |
`; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
describe('Basic layoutComplete functionality works', () => { |
| 35 |
ignoreBenignErrors(beforeEach, afterEach); |
| 36 |
|
| 37 |
it('resolves as soon as virtualizer has rendered, not before', async () => { |
| 38 |
const example = await fixture(testingHtml`<my-example></my-example>`); |
| 39 |
await customElements.whenDefined('lit-virtualizer'); |
| 40 |
const virtualizer = example.shadowRoot!.querySelector('lit-virtualizer')!; |
| 41 |
expect(virtualizer).to.be.instanceOf(LitVirtualizer); |
| 42 |
// Before layoutComplete, we shouldn't have rendered |
| 43 |
expect(virtualizer.children.length).to.equal(0); |
| 44 |
await virtualizer.layoutComplete; |
| 45 |
// After layoutComplete, we should have rendered children |
| 46 |
expect(virtualizer.children.length).to.be.greaterThan(0); |
| 47 |
// And our children should have been explicitly positioned. |
| 48 |
// This test depends on an implementation detail; will need to be |
| 49 |
// updated if we ever change the way we position children in the DOM. |
| 50 |
expect( |
| 51 |
(virtualizer.children[0] as HTMLElement).style.transform.indexOf( |
| 52 |
'translate' |
| 53 |
) |
| 54 |
).to.be.greaterThan(-1); |
| 55 |
}); |
| 56 |
|
| 57 |
it('returns the same promise when layoutComplete is accessed repeatedly during a single cycle', async () => { |
| 58 |
const example = await fixture(testingHtml`<my-example></my-example>`); |
| 59 |
await customElements.whenDefined('lit-virtualizer'); |
| 60 |
const virtualizer = example.shadowRoot!.querySelector('lit-virtualizer')!; |
| 61 |
const lc1 = virtualizer.layoutComplete; |
| 62 |
const lc2 = virtualizer.layoutComplete; |
| 63 |
await Promise.resolve(); |
| 64 |
const lc3 = virtualizer.layoutComplete; |
| 65 |
await new Promise((resolve) => setTimeout(resolve, 0)); |
| 66 |
const lc4 = virtualizer.layoutComplete; |
| 67 |
expect(lc1 === lc2 && lc2 === lc3 && lc3 === lc4).to.equal(true); |
| 68 |
}); |
| 69 |
}); |