Skip to content

Commit cf99098

Browse files
committed
refactor(ui): simplify chip [class] bindings, remove dead enum fields, memoize aria labels, and add unit tests
- Bind `<mat-chip [class]="statusColor()">` in `status_chip.ng.html` and `<mat-chip [class]="chipColor()">` backed by `RESOURCE_COLOR_MAP` in `resource_chip.ts` / `resource_chip.ng.html`. - Remove unused `StatusOption` / `ResourceType` template helper fields, replace `getRemoveRowAriaLabel(...)` template method call with a memoized `removeRowAriaLabels` `computed()` signal in `smart_resource_finder.ts`, guard `(stateCount.state ?? '')` in `work_units_status_bar.ng.html`, and note missing `.light-green`/`.grey` CSS rules in `status_chip.ts`. - Add `status_chip_test.ts` and expand `resource_chip_test.ts` and `smart_resource_finder_test.ts`. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
1 parent 0166241 commit cf99098

10 files changed

Lines changed: 155 additions & 35 deletions

File tree

‎cmd/ui/app/common/components/resource_chip/resource_chip.ng.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
-->
16-
<mat-chip [class.grey]="isGreyChip()" [class.green]="isGreenChip()">
16+
<mat-chip [class]="chipColor()">
1717
{{ text() }}
1818
</mat-chip>

‎cmd/ui/app/common/components/resource_chip/resource_chip.ts‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export enum ResourceType {
2323
UNKNOWN = 'UNKNOWN',
2424
}
2525

26+
const RESOURCE_COLOR_MAP: Readonly<Record<ResourceType, string>> = {
27+
[ResourceType.CPU]: 'grey',
28+
[ResourceType.MEMORY]: 'grey',
29+
[ResourceType.UNKNOWN]: 'grey',
30+
[ResourceType.GPU]: 'green',
31+
[ResourceType.TPU]: 'green',
32+
};
33+
2634
/** The resource chip component. */
2735
@Component({
2836
selector: 'xmc-resource-chip',
@@ -36,17 +44,5 @@ export class ResourceChip {
3644
readonly resourceType = input.required<ResourceType>();
3745
readonly text = input.required<string>();
3846

39-
readonly ResourceType = ResourceType;
40-
41-
protected readonly isGreyChip = computed(() => {
42-
const type = this.resourceType();
43-
return (
44-
type === ResourceType.CPU || type === ResourceType.MEMORY || type === ResourceType.UNKNOWN
45-
);
46-
});
47-
48-
protected readonly isGreenChip = computed(() => {
49-
const type = this.resourceType();
50-
return type === ResourceType.GPU || type === ResourceType.TPU;
51-
});
47+
protected readonly chipColor = computed(() => RESOURCE_COLOR_MAP[this.resourceType()]);
5248
}

‎cmd/ui/app/common/components/resource_chip/resource_chip_test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
import { getHarness } from '@angular/cdk/testing/catalyst/async';
15+
import { TestBed } from '@angular/core/testing';
1516
import { MatChipHarness } from '@angular/material/chips/testing';
1617
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1718

@@ -37,7 +38,9 @@ describe('ResourceChip Component', () => {
3738
const testCases = [
3839
{ name: 'CPU', type: ResourceType.CPU, expectedClass: 'grey' },
3940
{ name: 'MEMORY', type: ResourceType.MEMORY, expectedClass: 'grey' },
41+
{ name: 'UNKNOWN', type: ResourceType.UNKNOWN, expectedClass: 'grey' },
4042
{ name: 'GPU', type: ResourceType.GPU, expectedClass: 'green' },
43+
{ name: 'TPU', type: ResourceType.TPU, expectedClass: 'green' },
4144
];
4245

4346
for (const tc of testCases) {
@@ -47,9 +50,30 @@ describe('ResourceChip Component', () => {
4750
const chipHarness = await getHarness(MatChipHarness);
4851
const host = await chipHarness.host();
4952

53+
expect(await host.hasClass('mat-mdc-chip'))
54+
.withContext('Expected host Material chip class to be preserved')
55+
.toBeTrue();
5056
expect(await host.hasClass(tc.expectedClass))
5157
.withContext(`Expected to have class "${tc.expectedClass}"`)
5258
.toBeTrue();
5359
});
5460
}
61+
62+
it('updates chip class cleanly when resourceType changes without dropping host classes', () => {
63+
const fixture = TestBed.createComponent(ResourceChip);
64+
fixture.componentRef.setInput('resourceType', ResourceType.CPU);
65+
fixture.componentRef.setInput('text', 'CPU');
66+
fixture.detectChanges();
67+
68+
const chipEl = fixture.nativeElement.querySelector('mat-chip') as HTMLElement;
69+
expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue();
70+
expect(chipEl.classList.contains('grey')).toBeTrue();
71+
72+
fixture.componentRef.setInput('resourceType', ResourceType.GPU);
73+
fixture.detectChanges();
74+
75+
expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue();
76+
expect(chipEl.classList.contains('grey')).toBeFalse();
77+
expect(chipEl.classList.contains('green')).toBeTrue();
78+
});
5579
});

‎cmd/ui/app/common/components/status_chip/status_chip.ng.html‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,4 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
-->
16-
<mat-chip
17-
[class.green]="statusColor() === 'green'"
18-
[class.light-green]="statusColor() === 'light-green'"
19-
[class.red]="statusColor() === 'red'"
20-
[class.blue]="statusColor() === 'blue'"
21-
[class.yellow]="statusColor() === 'yellow'"
22-
[class.grey]="statusColor() === 'grey'"
23-
[class.orange]="statusColor() === 'orange'"
24-
[class.black]="statusColor() === 'black'"
25-
>
26-
{{ replicas() }} {{ status() }}
27-
</mat-chip>
16+
<mat-chip [class]="statusColor()">{{ replicas() }} {{ status() }}</mat-chip>

‎cmd/ui/app/common/components/status_chip/status_chip.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export enum StatusOption {
3838
const STATUS_COLOR_MAP: Readonly<Record<StatusOption, string>> = {
3939
[StatusOption.SUCCESS]: 'green',
4040
[StatusOption.COMPLETED]: 'green',
41+
// TODO: Add .light-green and .grey CSS rules in status_chip.scss so STARTING/DESCHEDULING/SCHEDULING chips render distinct colors.
4142
[StatusOption.STARTING]: 'light-green',
4243
[StatusOption.FAILED]: 'red',
4344
[StatusOption.RUNNING]: 'blue',
@@ -71,7 +72,5 @@ export class StatusChip {
7172
// jobs in a work unit.
7273
readonly replicas = input<number | undefined>(undefined);
7374

74-
readonly StatusOption = StatusOption;
75-
7675
protected readonly statusColor = computed(() => STATUS_COLOR_MAP[this.status()]);
7776
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
import { getHarness } from '@angular/cdk/testing/catalyst/async';
15+
import { TestBed } from '@angular/core/testing';
16+
import { MatChipHarness } from '@angular/material/chips/testing';
17+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
18+
19+
import { bootstrap, setupModule } from 'app/testing/test';
20+
21+
import { StatusChip, StatusOption } from './status_chip';
22+
23+
describe('StatusChip Component', () => {
24+
beforeEach(() => {
25+
setupModule({
26+
imports: [StatusChip, NoopAnimationsModule],
27+
});
28+
});
29+
30+
it('displays status with optional replica count', async () => {
31+
bootstrap(StatusChip, { status: StatusOption.RUNNING, replicas: 4 });
32+
33+
const chipHarness = await getHarness(MatChipHarness);
34+
expect(await chipHarness.getText()).toBe('4 Running');
35+
});
36+
37+
const expectedColors: ReadonlyArray<{ status: StatusOption; expectedClass: string }> = [
38+
{ status: StatusOption.SUCCESS, expectedClass: 'green' },
39+
{ status: StatusOption.COMPLETED, expectedClass: 'green' },
40+
{ status: StatusOption.STARTING, expectedClass: 'light-green' },
41+
{ status: StatusOption.FAILED, expectedClass: 'red' },
42+
{ status: StatusOption.RUNNING, expectedClass: 'blue' },
43+
{ status: StatusOption.NOT_RUNNING, expectedClass: 'blue' },
44+
{ status: StatusOption.PENDING, expectedClass: 'yellow' },
45+
{ status: StatusOption.INITIALIZING, expectedClass: 'yellow' },
46+
{ status: StatusOption.UNKNOWN, expectedClass: 'yellow' },
47+
{ status: StatusOption.RESTARTING, expectedClass: 'yellow' },
48+
{ status: StatusOption.DESCHEDULING, expectedClass: 'grey' },
49+
{ status: StatusOption.SCHEDULING, expectedClass: 'grey' },
50+
{ status: StatusOption.SCHEDULING_PAUSED, expectedClass: 'orange' },
51+
{ status: StatusOption.STOPPING, expectedClass: 'black' },
52+
{ status: StatusOption.STOPPED, expectedClass: 'black' },
53+
];
54+
55+
for (const tc of expectedColors) {
56+
it(`applies ${tc.expectedClass} class and keeps mat-mdc-chip for ${tc.status}`, async () => {
57+
bootstrap(StatusChip, { status: tc.status });
58+
59+
const chipHarness = await getHarness(MatChipHarness);
60+
const host = await chipHarness.host();
61+
62+
expect(await host.hasClass('mat-mdc-chip'))
63+
.withContext('Expected host Material chip class to be preserved')
64+
.toBeTrue();
65+
expect(await host.hasClass(tc.expectedClass))
66+
.withContext(`Expected status "${tc.status}" to apply class "${tc.expectedClass}"`)
67+
.toBeTrue();
68+
});
69+
}
70+
71+
it('updates chip color class dynamically when status changes without dropping host classes', () => {
72+
const fixture = TestBed.createComponent(StatusChip);
73+
fixture.componentRef.setInput('status', StatusOption.RUNNING);
74+
fixture.detectChanges();
75+
76+
const chipEl = fixture.nativeElement.querySelector('mat-chip') as HTMLElement;
77+
expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue();
78+
expect(chipEl.classList.contains('blue')).toBeTrue();
79+
80+
fixture.componentRef.setInput('status', StatusOption.COMPLETED);
81+
fixture.detectChanges();
82+
83+
expect(chipEl.classList.contains('mat-mdc-chip')).toBeTrue();
84+
expect(chipEl.classList.contains('blue')).toBeFalse();
85+
expect(chipEl.classList.contains('green')).toBeTrue();
86+
});
87+
});

‎cmd/ui/app/common/components/work_units_status_bar/work_units_status_bar.ng.html‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
<div class="states">
2626
@for (stateCount of sortedStateCounts(); track stateCount.state) {
2727
@if (stateCount.count && stateCount.count !== '0') {
28-
<span [class]="'state ' + stateCount.state" [style.width]="getWidth(stateCount.count)">
28+
<span
29+
[class]="'state ' + (stateCount.state ?? '')"
30+
[style.width]="getWidth(stateCount.count)"
31+
>
2932
{{ stateCount.count }}
3033
</span>
3134
}

‎cmd/ui/app/resource_dashboard/components/smart_resource_finder/smart_resource_finder.ng.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
type="button"
7474
mat-icon-button
7575
class="remove-resource-btn"
76-
[attr.aria-label]="getRemoveRowAriaLabel(row.resourceType, $index)"
76+
[attr.aria-label]="removeRowAriaLabels()[$index]"
7777
(click)="removeRow($index)"
7878
>
7979
<mat-icon class="remove-icon">close</mat-icon>

‎cmd/ui/app/resource_dashboard/components/smart_resource_finder/smart_resource_finder.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { ChangeDetectionStrategy, Component, input, OnInit, output, signal } from '@angular/core';
15+
import {
16+
ChangeDetectionStrategy,
17+
Component,
18+
computed,
19+
input,
20+
OnInit,
21+
output,
22+
signal,
23+
} from '@angular/core';
1624
import { FormsModule } from '@angular/forms';
1725
import { MatButtonModule } from '@angular/material/button';
1826
import { MatOptionModule } from '@angular/material/core';
@@ -57,6 +65,11 @@ export class SmartResourceFinderComponent implements OnInit {
5765

5866
protected readonly dominantTooltip = ROD_TERM_TOOLTIPS.dominantResource;
5967
protected readonly rows = signal<ResourceRequirementRow[]>([createRow(1, '')]);
68+
protected readonly removeRowAriaLabels = computed(() =>
69+
this.rows().map(
70+
(row, index) => `Remove ${row.resourceType || 'resource filter'} (row ${index + 1})`,
71+
),
72+
);
6073

6174
ngOnInit() {
6275
this.emitQuery();
@@ -73,10 +86,6 @@ export class SmartResourceFinderComponent implements OnInit {
7386
this.emitQuery();
7487
}
7588

76-
protected getRemoveRowAriaLabel(resourceType: string, index: number): string {
77-
return `Remove ${resourceType || 'resource filter'} (row ${index + 1})`;
78-
}
79-
8089
protected updateRowAmount(index: number, val: number | string | null | undefined) {
8190
const amountNum = typeof val === 'number' ? val : Number(val);
8291
const current = this.rows()[index];

‎cmd/ui/app/resource_dashboard/components/smart_resource_finder/smart_resource_finder_test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,17 @@ describe('SmartResourceFinderComponent', () => {
177177
'The primary resource used to calculate and display queue velocity and pending demand metrics on queue cards.',
178178
);
179179
});
180+
181+
it('should compute remove button aria-labels from row resource types', async () => {
182+
await harness.clickAddButton();
183+
await harness.selectResourceType(0, 'nvidia.com/gpu');
184+
fixture.detectChanges();
185+
186+
const buttons = fixture.nativeElement.querySelectorAll(
187+
'.remove-resource-btn',
188+
) as NodeListOf<HTMLButtonElement>;
189+
expect(buttons.length).toBe(2);
190+
expect(buttons[0].getAttribute('aria-label')).toBe('Remove nvidia.com/gpu (row 1)');
191+
expect(buttons[1].getAttribute('aria-label')).toBe('Remove resource filter (row 2)');
192+
});
180193
});

0 commit comments

Comments
 (0)