| 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, css, LitElement} from 'lit'; |
| 9 |
import {customElement, state} from 'lit/decorators.js'; |
| 10 |
import {LitVirtualizer} from '../../lit-virtualizer.js'; |
| 11 |
import '../../lit-virtualizer.js'; |
| 12 |
import {flow} from '../../layouts/flow.js'; |
| 13 |
import {grid} from '../../layouts/grid.js'; |
| 14 |
import {expect, html as testingHtml, fixture} from '@open-wc/testing'; |
| 15 |
|
| 16 |
type LayoutSpecifier = typeof LitVirtualizer.prototype.layout; |
| 17 |
|
| 18 |
@customElement('my-example') |
| 19 |
export class MyExample extends LitElement { |
| 20 |
static override styles = css` |
| 21 |
:host { |
| 22 |
display: block; |
| 23 |
} |
| 24 |
`; |
| 25 |
data: string[] = new Array(1000).fill('').map((_, idx) => `Item ${idx}`); |
| 26 |
|
| 27 |
@state() |
| 28 |
layout: LayoutSpecifier | undefined = flow(); |
| 29 |
|
| 30 |
override render() { |
| 31 |
return html` |
| 32 |
<lit-virtualizer |
| 33 |
scroller |
| 34 |
.layout=${this.hasAttribute('control') |
| 35 |
? grid({itemSize: '100px'}) |
| 36 |
: this.layout!} |
| 37 |
.items=${this.data} |
| 38 |
.renderItem=${(item: string) => html`<div>${item}</div>`} |
| 39 |
></lit-virtualizer> |
| 40 |
`; |
| 41 |
} |
| 42 |
|
| 43 |
override async firstUpdated() { |
| 44 |
if (!this.hasAttribute('control')) { |
| 45 |
this.layout = grid({itemSize: '100px'}); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// This is a new repro case for https://github.com/lit/lit/issues/3481, |
| 51 |
// which was reopened because the original fix didn't suffice when application |
| 52 |
// code caused a new layout to be specified while virtualizer was still in the |
| 53 |
// (async) process of initializing the originally specified layout. The new |
| 54 |
// fix explicitly accounts for this case; at the same time, the virtualizer |
| 55 |
// intitialization code has been signficantly simplified. |
| 56 |
describe("Don't behave badly when changing layouts before initialization is complete", () => { |
| 57 |
ignoreBenignErrors(beforeEach, afterEach); |
| 58 |
|
| 59 |
it('should render the control and repro cases identically after scrolling', async () => { |
| 60 |
const example: LitElement = await fixture( |
| 61 |
testingHtml` |
| 62 |
<my-example control></my-example> |
| 63 |
<my-example></my-example> |
| 64 |
` |
| 65 |
); |
| 66 |
// The symptom of the issue is that rendering is incorrect / unstable |
| 67 |
// after scrolling. It turns out that both the original layout and the |
| 68 |
// newly specified layout were instantiated and trying to control the |
| 69 |
// same virtualizer (oops). |
| 70 |
const [control, repro] = [example, example.nextElementSibling]; |
| 71 |
await control.updateComplete; |
| 72 |
await new Promise(requestAnimationFrame); |
| 73 |
const controlVirtualizer = |
| 74 |
control.shadowRoot!.querySelector('lit-virtualizer')!; |
| 75 |
const reproVirtualizer = |
| 76 |
repro!.shadowRoot!.querySelector('lit-virtualizer')!; |
| 77 |
controlVirtualizer.scrollBy(0, 2000); |
| 78 |
reproVirtualizer.scrollBy(0, 2000); |
| 79 |
await Promise.all([ |
| 80 |
controlVirtualizer.layoutComplete, |
| 81 |
reproVirtualizer.layoutComplete, |
| 82 |
]); |
| 83 |
expect(reproVirtualizer.textContent).to.equal( |
| 84 |
controlVirtualizer.textContent |
| 85 |
); |
| 86 |
}); |
| 87 |
}); |