Debian Codegraph @

packages/labs/virtualizer/src/test/scenarios/slotted-virtualizer-scrolling.test.ts

raw
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} from 'lit/decorators.js';
10 import '../../lit-virtualizer.js';
11 import {LitVirtualizer} from '../../lit-virtualizer.js';
12 import {expect, html as testingHtml, fixture} from '@open-wc/testing';
13 import {last} from '../helpers.js';
14
15 interface Item {
16 text: string;
17 }
18 const data = new Array(1000).fill('').map((_, i) => ({text: `Item ${i}`}));
19
20 @customElement('slot-in-scroller')
21 class SlotInScroller extends LitElement {
22 static styles = [
23 css`
24 :host {
25 display: block;
26 height: 100vh;
27 overflow: hidden;
28 }
29
30 .child {
31 display: block;
32 height: 100%;
33 overflow: scroll;
34 }
35 `,
36 ];
37
38 render() {
39 return html` <div class="child"><slot></slot></div> `;
40 }
41 }
42
43 @customElement('slot-is-scroller')
44 class SlotIsScroller extends LitElement {
45 static styles = [
46 css`
47 :host {
48 display: block;
49 height: 100vh;
50 overflow: hidden;
51 }
52
53 slot {
54 display: block;
55 height: 100%;
56 overflow: scroll;
57 }
58 `,
59 ];
60
61 render() {
62 return html` <slot></slot> `;
63 }
64 }
65
66 @customElement('my-example')
67 export class MyExample extends LitElement {
68 render() {
69 return html`
70 <lit-virtualizer
71 .items=${data}
72 .renderItem=${(item: Item) => html` <p>${item.text}</p> `}
73 >
74 </lit-virtualizer>
75 `;
76 }
77 }
78
79 async function tryScrolling(virtualizer: LitVirtualizer) {
80 expect(virtualizer).to.be.instanceOf(LitVirtualizer);
81 await customElements.whenDefined('lit-virtualizer');
82 await virtualizer.layoutComplete;
83 const lastChild: Element = last(Array.from(virtualizer.children));
84 lastChild.scrollIntoView();
85 await virtualizer.layoutComplete;
86 const newLastChild = last(Array.from(virtualizer.children));
87 expect(newLastChild).not.to.equal(lastChild);
88 }
89
90 describe('A slotted virtualizer scrolls correctly...', () => {
91 ignoreBenignErrors(beforeEach, afterEach);
92
93 describe('...when it is directly slotted...', () => {
94 it('...and the slot is in a scroller', async () => {
95 const example = (await fixture(
96 testingHtml`
97 <slot-in-scroller>
98 <lit-virtualizer
99 .items=${data}
100 .renderItem=${(item: Item) => html` <p>${item.text}</p> `}
101 ></lit-virtualizer>
102 </slot-in-scroller>
103 <style>body {margin: 0; padding: 0;}</style>
104 `
105 )) as SlotInScroller;
106 expect(example).to.be.instanceof(SlotInScroller);
107 const virtualizer = example.querySelector('lit-virtualizer')!;
108 await tryScrolling(virtualizer);
109 });
110
111 it('...or the slot itself is a scroller', async () => {
112 const example = (await fixture(
113 testingHtml`
114 <slot-is-scroller>
115 <lit-virtualizer
116 .items=${data}
117 .renderItem=${(item: Item) => html` <p>${item.text}</p> `}
118 ></lit-virtualizer>
119 </slot-is-scroller>
120 <style>body {margin: 0; padding: 0;}</style>
121 `
122 )) as SlotIsScroller;
123 expect(example).to.be.instanceof(SlotIsScroller);
124 const virtualizer = example.querySelector('lit-virtualizer')!;
125 await tryScrolling(virtualizer);
126 });
127 });
128
129 describe('Also, when it is within a slotted parent element...', () => {
130 it('...and the slot is in a scroller', async () => {
131 const example = (await fixture(
132 testingHtml`
133 <slot-in-scroller>
134 <my-example></my-example>
135 </slot-in-scroller>
136 <style>body {margin: 0; padding: 0;}</style>
137 `
138 )) as SlotInScroller;
139 expect(example).to.be.instanceof(SlotInScroller);
140 const parent = example.querySelector('my-example');
141 const virtualizer = parent!.shadowRoot!.querySelector('lit-virtualizer')!;
142 await tryScrolling(virtualizer);
143 });
144
145 it('...or the slot itself is a scroller', async () => {
146 const example = (await fixture(
147 testingHtml`
148 <slot-is-scroller>
149 <my-example></my-example>
150 </slot-is-scroller>
151 <style>body {margin: 0; padding: 0;}</style>
152 `
153 )) as SlotIsScroller;
154 expect(example).to.be.instanceof(SlotIsScroller);
155 const parent = example.querySelector('my-example');
156 const virtualizer = parent!.shadowRoot!.querySelector('lit-virtualizer')!;
157 await tryScrolling(virtualizer);
158 });
159 });
160 });