| 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, state} from 'lit/decorators.js'; |
| 10 |
import '../../lit-virtualizer.js'; |
| 11 |
import {grid} from '../../layouts/grid.js'; |
| 12 |
import {expect, html as testingHtml, fixture} from '@open-wc/testing'; |
| 13 |
|
| 14 |
@customElement('my-example') |
| 15 |
export class MyExample extends LitElement { |
| 16 |
@state() |
| 17 |
data: number[] = []; |
| 18 |
|
| 19 |
override async firstUpdated() { |
| 20 |
this.data = [0, 1]; |
| 21 |
await new Promise(requestAnimationFrame); |
| 22 |
this.data = [2, 3]; |
| 23 |
} |
| 24 |
|
| 25 |
override render() { |
| 26 |
return html` |
| 27 |
<lit-virtualizer |
| 28 |
.layout=${grid({ |
| 29 |
itemSize: {width: '100px', height: '100px'}, |
| 30 |
gap: '0px', |
| 31 |
})} |
| 32 |
.items=${this.data} |
| 33 |
.renderItem=${(item: number) => html`<div>${item}</div>`} |
| 34 |
> |
| 35 |
</lit-virtualizer> |
| 36 |
`; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// This is one minimal repro case for https://github.com/lit/lit/issues/3243. |
| 41 |
// The issue was originally reported in a more complicated case involving the |
| 42 |
// use of lit-mobx. Other minimal repro cases are probably possible. |
| 43 |
// The fix we've identified for this issue is a low-level change to the logic |
| 44 |
// for completing the virtualizer update cycle after children have been |
| 45 |
// rendered or re-rendered. |
| 46 |
describe('Successful DOM update on immediate change to items', () => { |
| 47 |
ignoreBenignErrors(beforeEach, afterEach); |
| 48 |
|
| 49 |
it('should apply transforms to DOM children', async () => { |
| 50 |
const example = (await fixture( |
| 51 |
testingHtml`<my-example></my-example>` |
| 52 |
)) as MyExample; |
| 53 |
expect(example).to.be.instanceof(MyExample); |
| 54 |
await new Promise(requestAnimationFrame); |
| 55 |
const children = Array.from( |
| 56 |
example.shadowRoot!.querySelector('lit-virtualizer')!.children |
| 57 |
) as HTMLElement[]; |
| 58 |
const firstChild = children[0]; |
| 59 |
const secondChild = children[1]; |
| 60 |
// This test is brittle; it will break if we ever change the way we |
| 61 |
// position children in the DOM. But including it here because it's |
| 62 |
// an unambiguous way of illustrating the issue that spurred this test: |
| 63 |
// if the transforms haven't been applied, it means that the DOM update |
| 64 |
// wasn't successfully completed |
| 65 |
const translationApplied = |
| 66 |
firstChild.style.transform.indexOf('translate') !== -1 && |
| 67 |
secondChild.style.transform.indexOf('translate') !== -1; |
| 68 |
expect(translationApplied).to.equal(true); |
| 69 |
// This test doesn't rely on the aforementioned implementation detail; as |
| 70 |
// long as the grid() layout API and behavior don't change, the fact that |
| 71 |
// this test passes should indicate that the DOM update was successful |
| 72 |
const xOffset = |
| 73 |
secondChild.getBoundingClientRect().left - |
| 74 |
firstChild.getBoundingClientRect().left; |
| 75 |
expect(xOffset).to.equal(100); |
| 76 |
}); |
| 77 |
}); |