Skip to content

Commit 3f2023d

Browse files
danilsomsikovDevtools-frontend LUCI CQ
authored andcommitted
Improve data grid updates detection and tests
Bug: 394287937 Change-Id: I0ffc3a7d31ed3f81fb2dbeaf5ff4b5bd2335058a Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6235027 Auto-Submit: Danil Somsikov <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]> Reviewed-by: Benedikt Meurer <[email protected]> Commit-Queue: Danil Somsikov <[email protected]>
1 parent bde807c commit 3f2023d

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

front_end/ui/legacy/components/data_grid/DataGridElement.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ describeWithEnvironment('DataGrid', () => {
5050
});
5151

5252
async function renderDataGrid(template: Lit.TemplateResult): Promise<HTMLElement> {
53-
container.style.display = 'flex';
54-
container.style.width = '640px';
55-
container.style.height = '480px';
5653
render(template, container, {host: {}});
5754
await RenderCoordinator.done({waitForWork: true});
5855
return container.querySelector('devtools-data-grid') as HTMLElement;
5956
}
6057

58+
async function renderDataGridContent(template: Lit.TemplateResult): Promise<HTMLElement> {
59+
return renderDataGrid(html`<devtools-data-grid striped name="Display Name">${template}</devtools-data-grid>`);
60+
}
61+
62+
async function renderDataGridWithData(columns: Lit.TemplateResult, rows: Lit.TemplateResult): Promise<HTMLElement> {
63+
return renderDataGridContent(html`<table>${columns}${rows}</table>`);
64+
}
65+
6166
it('can be configured from template', async () => {
6267
const element = await renderDataGrid(html`
6368
<devtools-data-grid .striped=${true} .displayName=${'Display Name'}>
@@ -84,26 +89,26 @@ describeWithEnvironment('DataGrid', () => {
8489
});
8590

8691
it('can update data from template', async () => {
87-
await renderDataGrid(html`
88-
<devtools-data-grid striped name=${'Display Name'}>
89-
<table>
92+
await renderDataGridWithData(
93+
html`
9094
<tr>
9195
<th id="column-1">Column 1</th>
9296
<th id="column-2">Column 2</th>
93-
</tr>
97+
</tr>`,
98+
html`
9499
<tr>
95100
<td>Value 1</td>
96101
<td>Value 2</td>
97102
</tr>
98103
</table>
99104
</devtools-data-grid>`);
100-
const element = await renderDataGrid(html`
101-
<devtools-data-grid striped name=${'Display Name'}>
102-
<table>
105+
const element = await renderDataGridWithData(
106+
html`
103107
<tr>
104108
<th id="column-3">Column 3</th>
105109
<th id="column-4">Column 4</th>
106-
</tr>
110+
</tr>`,
111+
html`
107112
<tr>
108113
<td>Value 3</td>
109114
<td>Value 4</td>

front_end/ui/legacy/components/data_grid/DataGridElement.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ class DataGridElement extends HTMLElement {
205205
#needUpdateColumns(mutationList: MutationRecord[]): boolean {
206206
for (const mutation of mutationList) {
207207
for (const element of [...mutation.removedNodes, ...mutation.addedNodes]) {
208-
if (element.nodeName === 'TH') {
208+
if (!(element instanceof HTMLElement)) {
209+
continue;
210+
}
211+
if (element.nodeName === 'TH' || element.querySelector('th')) {
209212
return true;
210213
}
211214
}
@@ -216,8 +219,25 @@ class DataGridElement extends HTMLElement {
216219
return false;
217220
}
218221

222+
#getDataRows(nodes: NodeList): HTMLElement[] {
223+
return [...nodes]
224+
.flatMap(node => {
225+
if (node instanceof HTMLTableRowElement) {
226+
return [node];
227+
}
228+
if (node instanceof HTMLElement) {
229+
return [...node.querySelectorAll('tr')];
230+
}
231+
return [] as HTMLElement[];
232+
})
233+
.filter(node => node.querySelector('td'));
234+
}
235+
219236
#addNodes(nodes: NodeList): void {
220-
for (const element of nodes) {
237+
for (const element of this.#getDataRows(nodes)) {
238+
if (!element.querySelector('td')) {
239+
continue;
240+
}
221241
if (element instanceof HTMLTableRowElement && element.querySelector('td')) {
222242
const parentNode = this.#dataGrid.rootNode(); // TODO(dsv): support nested nodes
223243
const nextNode = element.nextElementSibling ? DataGridElementNode.get(element.nextElementSibling) : null;
@@ -232,7 +252,10 @@ class DataGridElement extends HTMLElement {
232252
}
233253

234254
#removeNodes(nodes: NodeList): void {
235-
for (const element of nodes) {
255+
for (const element of this.#getDataRows(nodes)) {
256+
if (!element.querySelector('td')) {
257+
continue;
258+
}
236259
if (element instanceof HTMLTableRowElement && element.querySelector('td')) {
237260
const node = DataGridElementNode.get(element);
238261
if (node) {

0 commit comments

Comments
 (0)