Skip to content

Commit 6833a5c

Browse files
asynclizcopybara-github
authored andcommitted
fix(testing): render test table templates in light DOM
PiperOrigin-RevId: 404307093
1 parent d2eefac commit 6833a5c

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

components/testing/environment.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ import {ReactiveElement, render as litRender, TemplateResult} from 'lit';
1313
*/
1414
export class Environment {
1515
/**
16-
* The root container for rendering screenshot test elements.
16+
* An array of root containers for rendering screenshot test elements.
1717
*/
18-
readonly root = document.createElement('div');
18+
private readonly roots: HTMLElement[] = [];
1919

2020
constructor() {
21-
beforeAll(() => {
22-
this.root.id = 'root';
23-
this.root.style.display = 'inline-flex';
24-
document.body.appendChild(this.root);
25-
});
26-
2721
afterAll(() => {
28-
document.body.removeChild(this.root);
22+
for (const root of this.roots) {
23+
root.style.display = 'inline-flex';
24+
}
2925
});
3026
}
3127

@@ -35,7 +31,12 @@ export class Environment {
3531
* a Lit element to render.
3632
*/
3733
async waitForStability() {
38-
await this.waitForLitRender(this.root);
34+
if (!this.roots.length) {
35+
return;
36+
}
37+
38+
const currentRoot = this.roots[this.roots.length - 1];
39+
await this.waitForLitRender(currentRoot);
3940
}
4041

4142
/**
@@ -69,6 +70,30 @@ export class Environment {
6970
* @param template a Lit `TemplateResult` to render.
7071
*/
7172
render(template: TemplateResult) {
72-
litRender(template, this.root);
73+
litRender(template, this.createNewRoot());
74+
}
75+
76+
/**
77+
* Creates a new root container for screenshot rendering and adds it to the
78+
* body.
79+
*
80+
* Previous root containers will be hidden and displayed at the end of
81+
* testing for easier debugging.
82+
*
83+
* @return A new root container.
84+
*/
85+
private createNewRoot() {
86+
if (this.roots.length) {
87+
const currentRoot = this.roots[this.roots.length - 1];
88+
currentRoot.id = '';
89+
currentRoot.style.display = 'none';
90+
}
91+
92+
const root = document.createElement('div');
93+
root.id = 'root';
94+
root.style.display = 'inline-flex';
95+
document.body.appendChild(root);
96+
this.roots.push(root);
97+
return root;
7398
}
7499
}

components/testing/table/lib/_test-table.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
@mixin host() {
3131
display: flex;
32-
margin: 1rem;
3332
}
3433

3534
@mixin table() {

components/testing/table/lib/test-table.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {html, LitElement, TemplateResult} from 'lit';
7+
import {html, LitElement, render, TemplateResult} from 'lit';
88
import {property} from 'lit/decorators';
99

1010
export type TestTableTemplate<S extends string = string> = (state: S) =>
@@ -41,11 +41,21 @@ export class TestTable<S extends string = string> extends LitElement {
4141

4242
/** @soyTemplate */
4343
protected renderTemplates(): TemplateResult {
44+
// Render templates in the light DOM for easier styling access
45+
render(
46+
this.templates.map(
47+
(template, rowIndex) => this.states.map((state, colIndex) => html`
48+
<div slot="${`${rowIndex}-${colIndex}`}">${template(state)}</div>
49+
`)),
50+
this);
51+
4452
return html`
45-
${this.templates.map(template => html`
53+
${this.templates.map((template, rowIndex) => html`
4654
<tr>
47-
${this.states.map(state => html`
48-
<td class="md-test-table__cell">${template(state)}</td>
55+
${this.states.map((state, colIndex) => html`
56+
<td class="md-test-table__cell">
57+
<slot name="${`${rowIndex}-${colIndex}`}"></slot>
58+
</td>
4959
`)}
5060
</tr>
5161
`)}

0 commit comments

Comments
 (0)