Debian Codegraph @

packages/labs/virtualizer/src/test/scenarios/fixed-position-ancestor.test.ts

raw
1 /**
2 * @license
3 * Copyright 2022 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 import {LitElement, html} from 'lit';
8 import {customElement} from 'lit/decorators.js';
9 import {array, ignoreBenignErrors} from '../helpers.js';
10 import {LitVirtualizer} from '../../lit-virtualizer.js';
11 import {grid} from '../../layouts/grid.js';
12 import {styleMap} from 'lit/directives/style-map.js';
13 import {expect, fixture} from '@open-wc/testing';
14
15 interface NamedItem {
16 name: string;
17 }
18
19 const _100Items = array(100).map((_, i) => ({name: `Item ${i}`}));
20
21 describe('Virtualizer behaves properly when it has a position: fixed ancestor', () => {
22 ignoreBenignErrors(beforeEach, afterEach);
23
24 // Regression test for https://github.com/lit/lit/issues/3904
25 it('should calculate its viewport correctly', async () => {
26 // Our outer container is overflow: hidden, so would normally be
27 // a "clipping ancestor" of our virtualizer. Since it has a
28 // width of zero, it would cause the virtualizer to be invisible.
29 const containerStyles = {
30 overflow: 'hidden',
31 width: '0',
32 };
33
34 // But since this scrolling element is position: fixed, it will
35 // be positioned relative to the document, essentially "popping out"
36 // of its zero-width parent. This means the virtualizer will be
37 // visible and have some space to work with, after all.
38 const scrollerStyles = {
39 position: 'fixed',
40 width: '200px',
41 height: '200px',
42 };
43
44 // We use the grid layout here because it conveniently will not
45 // render any items if it calculates that it doesn't have sufficient
46 // space, making our testing job easier.
47 const container = await fixture(html`
48 <div id="container" style=${styleMap(containerStyles)}>
49 <div id="scroller" style=${styleMap(scrollerStyles)}>
50 <lit-virtualizer
51 .layout=${grid({
52 itemSize: '100px',
53 })}
54 .items=${_100Items}
55 .renderItem=${({name}: NamedItem) => html`<div>${name}</div>`}
56 ></lit-virtualizer>
57 </div>
58 </div>
59 `);
60
61 const scroller = container.querySelector('#scroller')!;
62 expect(scroller).to.be.instanceOf(HTMLDivElement);
63 const virtualizer = container.querySelector('lit-virtualizer')!;
64 expect(virtualizer).to.be.instanceOf(LitVirtualizer);
65
66 // If virtualizer has properly ignored the zero-width ancestor of our
67 // fixed-position scroller, some children will be rendered; otherwise, not.
68 //
69 // In practice, we'll time out if we fail here because the `layoutComplete`
70 // promise will never be fulfilled.
71 await virtualizer.layoutComplete;
72 expect(virtualizer.textContent).to.contain('Item 0');
73 });
74
75 // Regression test for https://github.com/lit/lit/issues/4125
76 it('should respond to scroll events from a fixed-position scroller', async () => {
77 const containerStyles = {
78 overflow: 'hidden',
79 };
80
81 // Our scroller is position: fixed. While its ancestors (if any) should
82 // therefore be ignored for the purpose of calculating virtualizer's
83 // viewport, the scroller itself should be considered a clipping ancestor,
84 // and virtualizer should listen to any `scroll` events it emits.
85 const scrollerStyles = {
86 position: 'fixed',
87 width: '200px',
88 height: '200px',
89 overflow: 'auto',
90 };
91
92 const container = await fixture(html`
93 <div id="container" style=${styleMap(containerStyles)}>
94 <div id="scroller" style=${styleMap(scrollerStyles)}>
95 <lit-virtualizer
96 .items=${_100Items}
97 .renderItem=${({name}: NamedItem) => html`<p>${name}</p>`}
98 ></lit-virtualizer>
99 </div>
100 </div>
101 `);
102
103 const scroller = container.querySelector('#scroller')!;
104 expect(scroller).to.be.instanceOf(HTMLDivElement);
105 const virtualizer = container.querySelector('lit-virtualizer')!;
106 expect(virtualizer).to.be.instanceOf(LitVirtualizer);
107
108 await virtualizer.layoutComplete;
109 expect(virtualizer.textContent).to.contain('Item 0');
110
111 // If the position: fixed scroller has properly been recognized as
112 // a clipping ancestor, then virtualizer will re-render as scrolling
113 // occurs; otherwise, not.
114 //
115 // In practice, we'll time out if we fail here because the `layoutComplete`
116 // promise will never be fulfilled.
117 scroller.scrollTo(0, scroller.scrollHeight);
118 await virtualizer.layoutComplete;
119 expect(virtualizer.textContent).to.contain('Item 99');
120 });
121 });
122
123 @customElement('simple-dialog')
124 export class SimpleDialog extends LitElement {
125 render() {
126 return html`<div style="position: fixed; width: 200px;">
127 <slot></slot>
128 </div> `;
129 }
130 }
131
132 describe('Virtualizer renders properly when it is slotted into a position: fixed ancestor', () => {
133 ignoreBenignErrors(beforeEach, afterEach);
134
135 // Regression test for https://github.com/lit/lit/issues/4346
136 it('should render children', async () => {
137 const container = await fixture(html`
138 <simple-dialog>
139 <lit-virtualizer
140 .items=${_100Items}
141 .renderItem=${({name}: NamedItem) => html`<p>${name}</p>`}
142 ></lit-virtualizer>
143 </simple-dialog>
144 `);
145
146 const virtualizer = container.querySelector('lit-virtualizer')!;
147 expect(virtualizer).to.be.instanceOf(LitVirtualizer);
148
149 // In practice, we'll time out if we fail here because the `layoutComplete`
150 // promise will never be fulfilled.
151 await virtualizer.layoutComplete;
152 expect(virtualizer.textContent).to.contain('Item 0');
153 });
154 });
155
156 describe('Virtualizer renders properly when it is position: fixed', () => {
157 ignoreBenignErrors(beforeEach, afterEach);
158
159 it('should render children', async () => {
160 const virtualizer = (await fixture(html`
161 <lit-virtualizer
162 style="position: fixed; width: 200px;"
163 .items=${_100Items}
164 .renderItem=${({name}: NamedItem) => html`<p>${name}</p>`}
165 ></lit-virtualizer>
166 `)) as LitVirtualizer;
167
168 expect(virtualizer).to.be.instanceOf(LitVirtualizer);
169
170 // In practice, we'll time out if we fail here because the `layoutComplete`
171 // promise will never be fulfilled.
172 await virtualizer.layoutComplete;
173 expect(virtualizer.textContent).to.contain('Item 0');
174 });
175 });