Debian Codegraph @

packages/labs/virtualizer/src/test/scenarios/item-changes.test.ts

raw
1 /**
2 * @license
3 * Copyright 2022 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 import {array, ignoreBenignErrors} from '../helpers.js';
8 import {LitVirtualizer} from '../../lit-virtualizer.js';
9 import {nothing} from 'lit';
10 import {expect, html, fixture} from '@open-wc/testing';
11
12 describe('Virtualizer re-renders properly after changes to the `items` array', () => {
13 ignoreBenignErrors(beforeEach, afterEach);
14
15 it('should not produce an out-of-bounds error when the number of items is reduced', async () => {
16 interface NamedItem {
17 name: string;
18 }
19
20 const _100Items = array(100).map((_, i) => ({name: `Item ${i}`}));
21 const _50Things = array(50).map((_, i) => ({name: `Thing ${i}`}));
22
23 /**
24 * This is a repro case for
25 * https://github.com/PolymerLabs/uni-virtualizer/issues/111, included
26 * here to guard against regressions.
27 *
28 * The danger in this area of the code is that the `virtualize`
29 * directive sometimes renders its children as part of the standard
30 * directive update lifecycle (as opposed to Virtualizer's own update
31 * lifecycle), and in this case bad things can happen if the
32 * directive's cached representation of the Virtualizer's current
33 * range (the indices of the first and last items currently rendered
34 * to the DOM) has somehow gotten out of sync with the actual size
35 * of the `items` array.
36 *
37 * The (implied) contract of Virtualizer's `renderItem()` function
38 * is that the `item` argument will never be undefined, so a check
39 * like the one below should be unnecessary in application code; we
40 * do it here purely for test purposes. We track our own state and
41 * guard against an actual runtime error because `renderItem()` runs
42 * in an event handler and an error thrown in that context would not
43 * cause our test to fail.
44 */
45 let outOfBounds = false;
46 function renderItem(item: NamedItem) {
47 if (item === undefined) {
48 outOfBounds = true;
49 return nothing;
50 }
51 return html`<p>${item.name}</p>`;
52 }
53
54 const v = (await fixture(html`
55 <lit-virtualizer
56 scroller
57 .items=${_100Items}
58 .renderItem=${renderItem}
59 ></lit-virtualizer>
60 `)) as LitVirtualizer;
61
62 // Make sure we have a Virtualizer and
63 // that its initial render is correct
64 expect(v).to.be.instanceOf(LitVirtualizer);
65 await v.layoutComplete;
66 expect(v.textContent).to.contain('Item 0');
67
68 // Scroll all the way to the bottom and
69 // confirm the last item is in the DOM
70 v.scrollBy(0, 100000);
71 await v.layoutComplete;
72 expect(v.textContent).to.contain('Item 99');
73
74 // Replace the `items` array with one
75 // only half as long
76 v.items = _50Things;
77 await v.layoutComplete;
78 // Make sure we've correctly re-rendered
79 // and that the new last item is in the DOM
80 expect(v.textContent).to.contain('Thing 49');
81 // If outOfBounds is true, then the directive's
82 // state has gotten out of sync (i.e., it has
83 // tried to render an item whose index is
84 // outside the bounds of the newly set `items`
85 // array).
86 expect(outOfBounds).to.be.false;
87 });
88 });