Debian Codegraph @

packages/labs/virtualizer/src/test/scenarios/masonry-gotchas.test.ts

raw
1 /**
2 * @license
3 * Copyright 2023 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, property} from 'lit/decorators.js';
10 import '../../lit-virtualizer.js';
11 import {masonry} from '../../layouts/masonry.js';
12 import {expect, html as testingHtml, fixture} from '@open-wc/testing';
13
14 /*
15 This file contains regression tests for https://github.com/lit/lit/issues/3815
16 */
17
18 type Item = {aspectRatio: number};
19
20 @customElement('masonry-gotchas')
21 export class MasonryGotchas extends LitElement {
22 static styles = css`
23 div {
24 background: blue;
25 }
26 lit-virtualizer {
27 background: pink;
28 }
29 :host {
30 width: 400px;
31 display: block;
32 }
33 `;
34
35 @property()
36 itemSize: `${number}px` = '150px';
37
38 @property({type: Array})
39 items: Item[] = [{aspectRatio: 1}, {aspectRatio: 2}];
40
41 render() {
42 return html`
43 <lit-virtualizer
44 .layout=${masonry({
45 itemSize: this.itemSize,
46 flex: false,
47 gap: '8px',
48 getAspectRatio: (item) => (item as unknown as Item).aspectRatio,
49 })}
50 .items=${this.items}
51 .renderItem=${(_: Item) => html` <div></div> `}
52 ></lit-virtualizer>
53 `;
54 }
55 }
56
57 describe("Size virtualizer properly even if last item placed doesn't extend the furthest", () => {
58 ignoreBenignErrors(beforeEach, afterEach);
59
60 it('should size the virtualizer to contain the first (largest) child', async () => {
61 const el = await fixture<MasonryGotchas>(
62 testingHtml`
63 <masonry-gotchas></masonry-gotchas>
64 `
65 );
66 const virtualizer = el.shadowRoot!.querySelector('lit-virtualizer')!;
67 const virtualizerHeight = virtualizer.getBoundingClientRect().height;
68 const firstChildHeight =
69 virtualizer.children[0].getBoundingClientRect().height;
70 // First child height plus leading and trailing 8px gaps
71 expect(virtualizerHeight).to.equal(firstChildHeight + 16);
72 });
73 });
74
75 describe("Calculate range properly even if last item placed doesn't extend the furthest", () => {
76 ignoreBenignErrors(beforeEach, afterEach);
77
78 it('should render both children', async () => {
79 const el = await fixture<MasonryGotchas>(
80 testingHtml`
81 <masonry-gotchas itemSize="180px"></masonry-gotchas>
82 `
83 );
84 const virtualizer = el.shadowRoot!.querySelector('lit-virtualizer')!;
85 expect(virtualizer.children.length).to.equal(2);
86 });
87
88 it('should render all three children', async () => {
89 const el = await fixture<MasonryGotchas>(
90 testingHtml`
91 <masonry-gotchas></masonry-gotchas>
92 `
93 );
94 el.items = [{aspectRatio: 1}, {aspectRatio: 0.5}, {aspectRatio: 2}];
95 const virtualizer = el.shadowRoot!.querySelector('lit-virtualizer')!;
96 await virtualizer.layoutComplete;
97 expect(virtualizer.children.length).to.equal(3);
98 });
99 });