Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[labs/virtualizer] Fix virtualize directive update to match lit-virtu…
…alizer update. Added test helpers, fixes and scenarios to cover. (#3133) * Added @open-wc/testing and created some helpers for cleaner tests. * Added regression test to ensure that virtualize directive will renders when non-item data changes. * Added eventually() helper to try and reduce redundancy in expect/until pairing. * Restructured the regression test to prove the observed behavior in #3052 * Fixed virtualize directive to render instead of return noChange to match lit-virtualizer behavior. * Lock selenium webdriver types because of bad WebSocket import.
- Loading branch information
Showing
8 changed files
with
301 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lit-labs/virtualizer': patch | ||
--- | ||
|
||
The virtualize directive will now correctly re-render children when data stored outside the items array has changed. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
packages/labs/virtualizer/src/test/scenarios/element-and-directive-parity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {ignoreBenignErrors, justText, until} from '../helpers.js'; | ||
import {LitVirtualizer} from '../../lit-virtualizer.js'; | ||
import {virtualize} from '../../virtualize.js'; | ||
import {css, LitElement} from 'lit'; | ||
import {customElement, property} from 'lit/decorators.js'; | ||
import {expect, html, fixture} from '@open-wc/testing'; | ||
|
||
abstract class TestElement extends LitElement { | ||
static styles = css` | ||
:host { | ||
display: block; | ||
height: 200px; | ||
} | ||
`; | ||
|
||
@property({type: Object, attribute: false}) | ||
public selected: Set<number> = new Set(); | ||
|
||
@property({type: Array, attribute: false}) | ||
public items: Array<number> = []; | ||
} | ||
|
||
@customElement('using-lit-virtualizer') | ||
class UsingLitVirtualizer extends TestElement { | ||
render() { | ||
return html` <lit-virtualizer | ||
scroller | ||
.items=${this.items} | ||
.renderItem=${(n: number) => | ||
html`<div>${n}${this.selected.has(n) ? ' selected' : ''}</div>`} | ||
></lit-virtualizer>`; | ||
} | ||
} | ||
|
||
@customElement('using-virtualize-directive') | ||
class UsingVirtualizeDirective extends TestElement { | ||
render() { | ||
return html` <div> | ||
${virtualize({ | ||
scroller: true, | ||
items: this.items, | ||
renderItem: (n) => | ||
html`<div>${n}${this.selected.has(n) ? ' selected' : ''}</div>`, | ||
})} | ||
</div>`; | ||
} | ||
} | ||
|
||
describe('test fixture classes', () => { | ||
it('are registered as custom elements', () => { | ||
expect(customElements.get('this-is-not-a-custom-element')).to.be.undefined; | ||
expect(customElements.get('lit-virtualizer')).to.eq(LitVirtualizer); | ||
expect(customElements.get('using-lit-virtualizer')).to.eq( | ||
UsingLitVirtualizer | ||
); | ||
expect(customElements.get('using-virtualize-directive')).to.eq( | ||
UsingVirtualizeDirective | ||
); | ||
}); | ||
}); | ||
|
||
describe('lit-virtualizer and virtualize directive', () => { | ||
ignoreBenignErrors(beforeEach, afterEach); | ||
|
||
/** | ||
* Regression test to cover the difference in behavior which led | ||
* to this issue: https://github.com/lit/lit/issues/3052 | ||
*/ | ||
it('both render changes based on non-item data changes', async () => { | ||
const items: Array<number> = Array.from(Array(100).keys()); | ||
const selected = new Set([2, 5]); | ||
|
||
const example = await fixture(html` | ||
<div> | ||
<using-lit-virtualizer></using-lit-virtualizer> | ||
<using-virtualize-directive></using-virtualize-directive> | ||
</div> | ||
`); | ||
await until( | ||
() => | ||
example.querySelector('using-lit-virtualizer') instanceof | ||
UsingLitVirtualizer | ||
); | ||
await until( | ||
() => | ||
example.querySelector('using-virtualize-directive') instanceof | ||
UsingVirtualizeDirective | ||
); | ||
|
||
const ulv: UsingLitVirtualizer = example.querySelector( | ||
'using-lit-virtualizer' | ||
)!; | ||
|
||
ulv.items = items; | ||
ulv.selected = selected; | ||
|
||
await until(() => | ||
justText(ulv.shadowRoot?.innerHTML).includes('2 selected') | ||
); | ||
|
||
expect(justText(ulv.shadowRoot?.innerHTML)).to.include('2 selected'); | ||
expect(justText(ulv.shadowRoot?.innerHTML)).to.include('5 selected'); | ||
|
||
const uvd: UsingVirtualizeDirective = example.querySelector( | ||
'using-virtualize-directive' | ||
)!; | ||
|
||
uvd.items = items; | ||
uvd.selected = selected; | ||
|
||
await until(() => | ||
justText(uvd.shadowRoot?.innerHTML).includes('2 selected') | ||
); | ||
|
||
expect(justText(uvd.shadowRoot?.innerHTML)).includes('2 selected'); | ||
expect(justText(uvd.shadowRoot?.innerHTML)).includes('5 selected'); | ||
|
||
const newSelected = new Set([1, 3]); | ||
|
||
ulv.selected = newSelected; | ||
|
||
await until(() => | ||
justText(ulv.shadowRoot?.innerHTML).includes('1 selected') | ||
); | ||
|
||
expect(justText(ulv.shadowRoot!.innerHTML)).to.include('1 selected'); | ||
expect(justText(ulv.shadowRoot!.innerHTML)).to.include('3 selected'); | ||
expect(justText(ulv.shadowRoot!.innerHTML)).not.to.include('2 selected'); | ||
expect(justText(ulv.shadowRoot!.innerHTML)).not.to.include('5 selected'); | ||
|
||
uvd.selected = newSelected; | ||
|
||
await until(() => | ||
justText(uvd.shadowRoot?.innerHTML).includes('1 selected') | ||
); | ||
|
||
expect(justText(uvd.shadowRoot!.innerHTML)).to.include('1 selected'); | ||
expect(justText(uvd.shadowRoot!.innerHTML)).to.include('3 selected'); | ||
expect(justText(uvd.shadowRoot!.innerHTML)).not.to.include('2 selected'); | ||
expect(justText(uvd.shadowRoot!.innerHTML)).not.to.include('5 selected'); | ||
}); | ||
}); |
Oops, something went wrong.