Debian Codegraph @

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

raw
1 /**
2 * @license
3 * Copyright 2022 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 import {expect} from '@open-wc/testing';
8 import {ignoreBenignErrors} from '../helpers.js';
9 import {
10 virtualizerFixture,
11 VirtualizerFixtureOptions,
12 observeScroll,
13 } from '../virtualizer-test-utilities.js';
14 import {BaseLayoutConfig} from '../../layouts/shared/Layout.js';
15
16 type Coordinate = 'top' | 'left';
17
18 function getCoordinate(fixtureOptions: VirtualizerFixtureOptions) {
19 const layout = fixtureOptions.layout as BaseLayoutConfig | undefined;
20 const direction = layout?.direction ?? 'vertical';
21 const coordinate: Coordinate = direction === 'vertical' ? 'top' : 'left';
22 const crossCoordinate: Coordinate = coordinate === 'top' ? 'left' : 'top';
23 return {coordinate, crossCoordinate};
24 }
25
26 function testBasicScrolling(fixtureOptions: VirtualizerFixtureOptions) {
27 ignoreBenignErrors(beforeEach, afterEach);
28
29 const {coordinate} = getCoordinate(fixtureOptions);
30
31 describe('smooth scrolling', () => {
32 it('should take some time and end up where it is supposed to', async () => {
33 const {scroller} = await virtualizerFixture(fixtureOptions);
34
35 const scrollResults = await observeScroll(scroller, () =>
36 scroller.scrollTo({[coordinate]: 2000, behavior: 'smooth'})
37 );
38 const {startPos, events, duration} = scrollResults;
39 let {endPos} = scrollResults;
40
41 /**
42 * HACK(usergenic): We are investigating a specific case where scrollWidth
43 * is intermittently being reported as 1000, when it should actually be in
44 * the 45000 range for the "direction: horizontal" and "scroller: true"
45 * scenarios, but is being calculated as 1000. To work around this in the
46 * known test cases we are going to issue two scrollTo calls. This is a
47 * temporary workaround.
48 */
49 if (coordinate === 'left' && fixtureOptions.scroller) {
50 const secondScrollResults = await observeScroll(scroller, () =>
51 scroller.scrollTo({[coordinate]: 2000, behavior: 'smooth'})
52 );
53 endPos = secondScrollResults.endPos;
54 }
55
56 expect(startPos[coordinate]).to.equal(0);
57 expect(endPos[coordinate]).to.equal(2000);
58 expect(events.length).to.be.greaterThan(1);
59 expect(duration).to.be.greaterThan(0);
60 });
61 });
62
63 describe('instant scrolling', () => {
64 it('should take no time and end up where it is supposed to', async () => {
65 const {scroller} = await virtualizerFixture(fixtureOptions);
66
67 const scrollResults = await observeScroll(scroller, () =>
68 scroller.scrollTo({[coordinate]: 2000})
69 );
70 const {startPos, events, duration} = scrollResults;
71 let {endPos} = scrollResults;
72
73 /**
74 * HACK(usergenic): We are investigating a specific case where scrollWidth
75 * is intermittently being reported as 1000, when it should actually be in
76 * the 45000 range for the "direction: horizontal" and "scroller: true"
77 * scenarios, but is being calculated as 1000. To work around this in the
78 * known test cases we are going to issue two scrollTo calls. This is a
79 * temporary workaround.
80 */
81 if (coordinate === 'left' && fixtureOptions.scroller) {
82 const secondScrollResults = await observeScroll(scroller, () =>
83 scroller.scrollTo({[coordinate]: 2000})
84 );
85 endPos = secondScrollResults.endPos;
86 }
87
88 expect(startPos[coordinate]).to.equal(0);
89 expect(endPos[coordinate]).to.equal(2000);
90 expect(events.length).to.equal(1);
91 expect(duration).to.equal(0);
92 });
93 });
94
95 describe('trying to smoothly scroll to the current position', () => {
96 it('should not do anything', async () => {
97 const {scroller} = await virtualizerFixture(fixtureOptions);
98 const {startPos, endPos, events, duration} = await observeScroll(
99 scroller,
100 () => scroller.scrollTo({[coordinate]: 0, behavior: 'smooth'})
101 );
102 expect(startPos[coordinate]).to.equal(0);
103 expect(endPos[coordinate]).to.equal(0);
104 expect(events.length).to.equal(0);
105 expect(duration).to.equal(null);
106 });
107 });
108
109 describe('trying to instantly scroll to the current position', () => {
110 it('should not do anything', async () => {
111 const {scroller} = await virtualizerFixture(fixtureOptions);
112 const {startPos, endPos, events, duration} = await observeScroll(
113 scroller,
114 () => scroller.scrollTo({[coordinate]: 0})
115 );
116 expect(startPos[coordinate]).to.equal(0);
117 expect(endPos[coordinate]).to.equal(0);
118 expect(events.length).to.equal(0);
119 expect(duration).to.equal(null);
120 });
121 });
122 }
123
124 // TODO(usergenic): The scrolling tests are currently failing due to an
125 // unknown issue, possibly related to changes in latest Chrome version.
126 // Commenting out these tests until we can investigate further, so that
127 // we can get the rest of the tests running on CI without interfering
128 // with the overall lit release pipeline.
129 describe.skipInCI('basic scrolling functionality, via scrollTo()', () => {
130 describe('vertical', () => {
131 describe('using <lit-virtualizer>...', () => {
132 describe('...and window scrolling', () => {
133 testBasicScrolling({useDirective: false, scroller: false});
134 });
135 describe('...and virtualizer scrolling', () => {
136 testBasicScrolling({useDirective: false, scroller: true});
137 });
138 });
139
140 describe('using the virtualize() directive...', () => {
141 describe('...and window scrolling', () => {
142 testBasicScrolling({useDirective: true, scroller: false});
143 });
144 describe('...and virtualizer scrolling', () => {
145 testBasicScrolling({useDirective: true, scroller: true});
146 });
147 });
148 });
149
150 describe('horizontal', () => {
151 describe('using <lit-virtualizer>...', () => {
152 describe('...and window scrolling', () => {
153 testBasicScrolling({
154 useDirective: false,
155 scroller: false,
156 layout: {direction: 'horizontal'},
157 });
158 });
159 describe('...and virtualizer scrolling', () => {
160 testBasicScrolling({
161 useDirective: false,
162 scroller: true,
163 layout: {direction: 'horizontal'},
164 });
165 });
166 });
167
168 describe('using the virtualize() directive...', () => {
169 describe('...and window scrolling', () => {
170 testBasicScrolling({
171 useDirective: true,
172 scroller: false,
173 layout: {direction: 'horizontal'},
174 });
175 });
176 describe('...and virtualizer scrolling', () => {
177 testBasicScrolling({
178 useDirective: true,
179 scroller: true,
180 layout: {direction: 'horizontal'},
181 });
182 });
183 });
184 });
185 });
186
187 function testScrollBy(fixtureOptions: VirtualizerFixtureOptions) {
188 ignoreBenignErrors(beforeEach, afterEach);
189
190 const {coordinate, crossCoordinate} = getCoordinate(fixtureOptions);
191
192 it('should work', async () => {
193 const {scroller} = await virtualizerFixture(fixtureOptions);
194 let {startPos, endPos} = await observeScroll(scroller, () =>
195 scroller.scrollBy({[coordinate]: 2000})
196 );
197 expect(startPos[coordinate]).to.equal(0);
198 expect(startPos[crossCoordinate]).to.equal(0);
199 expect(endPos[coordinate]).to.equal(2000);
200 expect(endPos[crossCoordinate]).to.equal(0);
201 ({startPos, endPos} = await observeScroll(scroller, () =>
202 scroller.scrollBy({[coordinate]: -1000})
203 ));
204 expect(startPos[coordinate]).to.equal(2000);
205 expect(endPos[coordinate]).to.equal(1000);
206 });
207 }
208
209 describe('scrollBy()', () => {
210 testScrollBy({});
211 });