Skip to content

Commit 74ddc0a

Browse files
committed
refactor(ui): remove explicit any from ui unit tests and mocks
Replace explicit `any` casts and annotations across 16 UI unit test and mock files with concrete Angular Router, ECharts, ComponentFixture, Subject, and proto types, using `ArtifactsList` bracket access in `artifacts_list_test.ts`. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
1 parent 89649b9 commit 74ddc0a

16 files changed

Lines changed: 157 additions & 114 deletions

‎cmd/ui/app/admin_panel.guard_test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import { TestBed } from '@angular/core/testing';
16-
import { Router } from '@angular/router';
16+
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
1717
import { firstValueFrom, Observable, of } from 'rxjs';
1818

1919
import { adminPanelGuard } from './admin_panel.guard';
@@ -53,7 +53,7 @@ describe('adminPanelGuard', () => {
5353
state.setAdminPanelAccessible(true);
5454

5555
const resultOrObservable = TestBed.runInInjectionContext(() =>
56-
adminPanelGuard({} as any, {} as any),
56+
adminPanelGuard({} as ActivatedRouteSnapshot, {} as RouterStateSnapshot),
5757
);
5858
const result =
5959
resultOrObservable instanceof Observable
@@ -68,7 +68,7 @@ describe('adminPanelGuard', () => {
6868
state.setAdminPanelAccessible(false);
6969

7070
const resultOrObservable = TestBed.runInInjectionContext(() =>
71-
adminPanelGuard({} as any, {} as any),
71+
adminPanelGuard({} as ActivatedRouteSnapshot, {} as RouterStateSnapshot),
7272
);
7373
const result =
7474
resultOrObservable instanceof Observable

‎cmd/ui/app/admin_panel/admin_panel_test.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { provideHttpClient } from '@angular/common/http';
1616
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1717
import { of, Subject, throwError } from 'rxjs';
1818

19+
import { HealthStatusResponse } from 'app/protos/health_service';
20+
1921
import { FeatureFlagService } from '../service/feature_flag_service';
2022
import { HealthService } from '../service/health_service';
2123
import { SnackbarService } from '../service/snackbar_service';
@@ -118,7 +120,7 @@ describe('AdminPanelPage Component', () => {
118120
xidDbSchemaVersion: '1',
119121
},
120122
healthCheckTime: { seconds: 1717804800, nanos: 0 },
121-
} as any),
123+
} as unknown as HealthStatusResponse),
122124
);
123125

124126
bootstrap(AdminPanelPage);
@@ -156,13 +158,13 @@ describe('AdminPanelPage Component', () => {
156158
},
157159
components: [],
158160
healthCheckTime: { seconds: 1717804800, nanos: 0 },
159-
};
161+
} as unknown as HealthStatusResponse;
160162
spyOn(healthService, 'getHealthStatus').and.callFake(() => {
161163
attempts++;
162164
if (attempts === 1) {
163165
return throwError(() => new Error('Initial load failed'));
164166
}
165-
return of(mockStatus as any);
167+
return of(mockStatus);
166168
});
167169

168170
bootstrap(AdminPanelPage);
@@ -193,9 +195,9 @@ describe('AdminPanelPage Component', () => {
193195
},
194196
components: [],
195197
healthCheckTime: { seconds: 1717804800, nanos: 0 },
196-
};
198+
} as unknown as HealthStatusResponse;
197199
spyOn(healthService, 'getHealthStatus').and.returnValues(
198-
of(firstStatus as any),
200+
of(firstStatus),
199201
throwError(() => new Error('Refresh error')),
200202
);
201203

@@ -216,7 +218,7 @@ describe('AdminPanelPage Component', () => {
216218
it('should display progress spinner while loading', async () => {
217219
const healthService = get(HealthService);
218220
// Never emit to keep isLoading true
219-
spyOn(healthService, 'getHealthStatus').and.returnValue(new Subject<any>());
221+
spyOn(healthService, 'getHealthStatus').and.returnValue(new Subject<HealthStatusResponse>());
220222

221223
bootstrap(AdminPanelPage);
222224
// Flush microtasks without completing observable
@@ -322,7 +324,7 @@ describe('AdminPanelPage Component', () => {
322324
xidDbSchemaVersion: '1',
323325
},
324326
healthCheckTime: { seconds: 1717804800, nanos: 0 },
325-
} as any),
327+
} as unknown as HealthStatusResponse),
326328
);
327329

328330
bootstrap(AdminPanelPage);
@@ -344,7 +346,7 @@ describe('AdminPanelPage Component', () => {
344346
xidDbSchemaVersion: '1',
345347
},
346348
healthCheckTime: { seconds: 1717804800, nanos: 0 },
347-
} as any),
349+
} as unknown as HealthStatusResponse),
348350
);
349351

350352
bootstrap(AdminPanelPage);
@@ -360,7 +362,7 @@ describe('AdminPanelPage Component', () => {
360362
of({
361363
healthState: 'HEALTH_STATE_HEALTHY',
362364
healthCheckTime: { seconds: 1717804800, nanos: 0 },
363-
} as any),
365+
} as unknown as HealthStatusResponse),
364366
);
365367

366368
bootstrap(AdminPanelPage);
@@ -393,7 +395,7 @@ describe('AdminPanelPage Component', () => {
393395
isSchedulingPaused: true,
394396
},
395397
healthCheckTime: { seconds: 1717804800, nanos: 0 },
396-
} as any),
398+
} as unknown as HealthStatusResponse),
397399
);
398400

399401
bootstrap(AdminPanelPage);
@@ -500,7 +502,7 @@ describe('AdminPanelPage Component', () => {
500502
isSchedulingPaused: pausedOnBackend,
501503
},
502504
healthCheckTime: { seconds: 1717804800, nanos: 0 },
503-
} as any);
505+
} as unknown as HealthStatusResponse);
504506
});
505507

506508
bootstrap(AdminPanelPage);
@@ -551,7 +553,7 @@ describe('AdminPanelPage Component', () => {
551553

552554
it('should disable toggle while pauseScheduling is in flight', async () => {
553555
const healthService = get(HealthService);
554-
const pauseSubject$ = new Subject<any>();
556+
const pauseSubject$ = new Subject<void>();
555557
spyOn(healthService, 'pauseScheduling').and.returnValue(pauseSubject$);
556558

557559
bootstrap(AdminPanelPage);
@@ -570,7 +572,7 @@ describe('AdminPanelPage Component', () => {
570572
// While request is pending, button is disabled
571573
expect(await pauseBtn!.isDisabled()).toBeTrue();
572574

573-
pauseSubject$.next({});
575+
pauseSubject$.next();
574576
pauseSubject$.complete();
575577
await flush();
576578

@@ -592,7 +594,7 @@ describe('AdminPanelPage Component', () => {
592594
schedulingPauseError: 'spanner read timeout',
593595
},
594596
healthCheckTime: { seconds: 1717804800, nanos: 0 },
595-
} as any),
597+
} as unknown as HealthStatusResponse),
596598
);
597599

598600
bootstrap(AdminPanelPage);

‎cmd/ui/app/auth.guard_test.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
// limitations under the License.
1414

1515
import { TestBed } from '@angular/core/testing';
16-
import { Router } from '@angular/router';
17-
import { of, throwError } from 'rxjs';
16+
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
17+
import { Observable, of, throwError } from 'rxjs';
1818

1919
import { authGuard } from './auth.guard';
20-
import { AuthService } from './service/auth.service';
20+
import { AuthService, UserResponse } from './service/auth.service';
2121
import { cleanState, setupModule } from './testing/test';
2222

2323
function setup() {
@@ -42,11 +42,13 @@ describe('authGuard', () => {
4242
const state = setup();
4343

4444
it('should return true if user is logged in', (done) => {
45-
state.authServiceSpy.getUser.and.returnValue(of({ email: 'user@example.com' } as any));
45+
state.authServiceSpy.getUser.and.returnValue(of({ email: 'user@example.com' } as UserResponse));
4646

47-
const result = TestBed.runInInjectionContext(() => authGuard({} as any, {} as any));
47+
const result = TestBed.runInInjectionContext(() =>
48+
authGuard({} as ActivatedRouteSnapshot, {} as RouterStateSnapshot),
49+
);
4850

49-
(result as any).subscribe((res: boolean) => {
51+
(result as Observable<boolean>).subscribe((res: boolean) => {
5052
expect(res).toBeTrue();
5153
expect(state.routerSpy.navigate).not.toHaveBeenCalled();
5254
done();
@@ -56,9 +58,11 @@ describe('authGuard', () => {
5658
it('should redirect to / and return false if user is not logged in', (done) => {
5759
state.authServiceSpy.getUser.and.returnValue(of(null));
5860

59-
const result = TestBed.runInInjectionContext(() => authGuard({} as any, {} as any));
61+
const result = TestBed.runInInjectionContext(() =>
62+
authGuard({} as ActivatedRouteSnapshot, {} as RouterStateSnapshot),
63+
);
6064

61-
(result as any).subscribe((res: boolean) => {
65+
(result as Observable<boolean>).subscribe((res: boolean) => {
6266
expect(res).toBeFalse();
6367
expect(state.routerSpy.navigate).toHaveBeenCalledWith(['/']);
6468
done();
@@ -68,9 +72,11 @@ describe('authGuard', () => {
6872
it('should redirect to / and return false if auth service throws an error', (done) => {
6973
state.authServiceSpy.getUser.and.returnValue(throwError(() => new Error('Auth error')));
7074

71-
const result = TestBed.runInInjectionContext(() => authGuard({} as any, {} as any));
75+
const result = TestBed.runInInjectionContext(() =>
76+
authGuard({} as ActivatedRouteSnapshot, {} as RouterStateSnapshot),
77+
);
7278

73-
(result as any).subscribe((res: boolean) => {
79+
(result as Observable<boolean>).subscribe((res: boolean) => {
7480
expect(res).toBeFalse();
7581
expect(state.routerSpy.navigate).toHaveBeenCalledWith(['/']);
7682
done();

‎cmd/ui/app/common/components/artifacts_list/artifacts_list_test.ts‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { provideNoopAnimations } from '@angular/platform-browser/animations';
1616
import { provideRouter } from '@angular/router';
1717
import { firstValueFrom, of } from 'rxjs';
1818

19+
import { ArtifactSingle } from 'app/common/artifacts/interfaces';
1920
import { Artifact, ListArtifactsResponse } from 'app/protos/experiment_state_server';
2021
import { ArtifactService } from 'app/service/artifact_service';
2122
import { bootstrap, setupModule } from 'app/testing/test';
@@ -81,67 +82,77 @@ describe('ArtifactsList Component', () => {
8182
});
8283

8384
it('should enable both EXPERIMENT and WORK_UNIT filters by default on experiment page', async () => {
84-
const comp = bootstrap(ArtifactsList, { parentName: 'experiments/1' });
85+
const comp: ArtifactsList = bootstrap(ArtifactsList, {
86+
parentName: 'experiments/1',
87+
});
8588
expect(comp).toBeTruthy();
8689

87-
const filter = (comp as any).artifactFilter.value;
90+
const filter = comp['artifactFilter'].value;
8891
expect(filter).toContain(ArtifactFilter.EXPERIMENT);
8992
expect(filter).toContain(ArtifactFilter.WORK_UNIT);
90-
expect((comp as any).showFilterToggle).toBeTrue();
91-
expect((comp as any).isWorkUnitPage).toBeFalse();
93+
expect(comp['showFilterToggle']).toBeTrue();
94+
expect(comp['isWorkUnitPage']).toBeFalse();
9295

93-
const inputItems: any = await firstValueFrom((comp as any).inputArtifacts$);
96+
const inputItems = await firstValueFrom(comp['inputArtifacts$']);
9497
// Both experiment-level and work-unit level input artifacts should be present
9598
expect(inputItems.length).toBe(2);
9699

97-
const outputItems: any = await firstValueFrom((comp as any).outputArtifacts$);
100+
const outputItems = await firstValueFrom(comp['outputArtifacts$']);
98101
// Both experiment-level and work-unit level output artifacts should be present
99102
expect(outputItems.length).toBe(2);
100103
});
101104

102105
it('should filter to only experiment-level artifacts when WORK_UNIT filter is unchecked', async () => {
103-
const comp = bootstrap(ArtifactsList, { parentName: 'experiments/1' });
104-
(comp as any).onFilterChange([ArtifactFilter.EXPERIMENT]);
106+
const comp: ArtifactsList = bootstrap(ArtifactsList, {
107+
parentName: 'experiments/1',
108+
});
109+
comp['onFilterChange']([ArtifactFilter.EXPERIMENT]);
105110

106-
const inputItems: any = await firstValueFrom((comp as any).inputArtifacts$);
111+
const inputItems = await firstValueFrom(comp['inputArtifacts$']);
107112
expect(inputItems.length).toBe(1);
108-
expect((inputItems[0] as any).artifact.artifact.name).toBe('artifacts/1');
113+
expect((inputItems[0] as ArtifactSingle).artifact.artifact.name).toBe('artifacts/1');
109114

110-
const outputItems: any = await firstValueFrom((comp as any).outputArtifacts$);
115+
const outputItems = await firstValueFrom(comp['outputArtifacts$']);
111116
expect(outputItems.length).toBe(1);
112-
expect((outputItems[0] as any).artifact.artifact.name).toBe('artifacts/3');
117+
expect((outputItems[0] as ArtifactSingle).artifact.artifact.name).toBe('artifacts/3');
113118
});
114119

115120
it('should filter to only work-unit level artifacts when EXPERIMENT filter is unchecked', async () => {
116-
const comp = bootstrap(ArtifactsList, { parentName: 'experiments/1' });
117-
(comp as any).onFilterChange([ArtifactFilter.WORK_UNIT]);
121+
const comp: ArtifactsList = bootstrap(ArtifactsList, {
122+
parentName: 'experiments/1',
123+
});
124+
comp['onFilterChange']([ArtifactFilter.WORK_UNIT]);
118125

119-
const inputItems: any = await firstValueFrom((comp as any).inputArtifacts$);
126+
const inputItems = await firstValueFrom(comp['inputArtifacts$']);
120127
expect(inputItems.length).toBe(1);
121-
expect((inputItems[0] as any).artifact.artifact.name).toBe('artifacts/2');
128+
expect((inputItems[0] as ArtifactSingle).artifact.artifact.name).toBe('artifacts/2');
122129

123-
const outputItems: any = await firstValueFrom((comp as any).outputArtifacts$);
130+
const outputItems = await firstValueFrom(comp['outputArtifacts$']);
124131
expect(outputItems.length).toBe(1);
125-
expect((outputItems[0] as any).artifact.artifact.name).toBe('artifacts/4');
132+
expect((outputItems[0] as ArtifactSingle).artifact.artifact.name).toBe('artifacts/4');
126133
});
127134

128135
it('should return empty lists when all filters are deselected', async () => {
129-
const comp = bootstrap(ArtifactsList, { parentName: 'experiments/1' });
130-
(comp as any).onFilterChange([]);
136+
const comp: ArtifactsList = bootstrap(ArtifactsList, {
137+
parentName: 'experiments/1',
138+
});
139+
comp['onFilterChange']([]);
131140

132-
const inputItems: any = await firstValueFrom((comp as any).inputArtifacts$);
141+
const inputItems = await firstValueFrom(comp['inputArtifacts$']);
133142
expect(inputItems.length).toBe(0);
134143

135-
const outputItems: any = await firstValueFrom((comp as any).outputArtifacts$);
144+
const outputItems = await firstValueFrom(comp['outputArtifacts$']);
136145
expect(outputItems.length).toBe(0);
137146
});
138147

139148
it('should disable filter toggle and return all artifacts on work unit page', async () => {
140-
const comp = bootstrap(ArtifactsList, { parentName: 'experiments/1/workUnits/1' });
141-
expect((comp as any).showFilterToggle).toBeFalse();
142-
expect((comp as any).isWorkUnitPage).toBeTrue();
149+
const comp: ArtifactsList = bootstrap(ArtifactsList, {
150+
parentName: 'experiments/1/workUnits/1',
151+
});
152+
expect(comp['showFilterToggle']).toBeFalse();
153+
expect(comp['isWorkUnitPage']).toBeTrue();
143154

144-
const inputItems: any = await firstValueFrom((comp as any).inputArtifacts$);
155+
const inputItems = await firstValueFrom(comp['inputArtifacts$']);
145156
expect(inputItems.length).toBe(2);
146157
});
147158
});

‎cmd/ui/app/common/components/dashboard/bar_options_builder_test.ts‎

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

15+
import type { BarSeriesOption, XAXisComponentOption, YAXisComponentOption } from 'echarts';
16+
1517
import { PlotData, PlotSpec, PlotType } from 'app/service/dashboard_models';
1618

1719
import { buildBarOption } from './bar_options_builder';
@@ -40,8 +42,9 @@ describe('bar_options_builder', () => {
4042

4143
it('should build bar chart with categories, values, and series name from spec title', () => {
4244
const option = buildBarOption(mockSpec, mockBarData);
43-
expect((option.xAxis as any)?.data).toEqual(['WorkUnit 1', 'WorkUnit 2', 'WorkUnit 3']);
44-
const seriesArr = option.series as any[];
45+
const xAxis = option.xAxis as XAXisComponentOption & { data?: string[] };
46+
expect(xAxis?.data).toEqual(['WorkUnit 1', 'WorkUnit 2', 'WorkUnit 3']);
47+
const seriesArr = option.series as BarSeriesOption[];
4548
expect(seriesArr[0].type).toBe('bar');
4649
expect(seriesArr[0].name).toBe('Accuracy Distribution');
4750
expect(seriesArr[0].data).toEqual([0.92, 0.88, 0.94]);
@@ -53,8 +56,9 @@ describe('bar_options_builder', () => {
5356
coords: { yMin: 0, yMax: 1.0 },
5457
};
5558
const option = buildBarOption(specWithCoords, mockBarData);
56-
expect((option.yAxis as any)?.min).toBe(0);
57-
expect((option.yAxis as any)?.max).toBe(1.0);
59+
const yAxis = option.yAxis as YAXisComponentOption;
60+
expect(yAxis?.min).toBe(0);
61+
expect(yAxis?.max).toBe(1.0);
5862
});
5963

6064
it('should fallback series name to metricName or Value when spec title is omitted', () => {
@@ -64,7 +68,7 @@ describe('bar_options_builder', () => {
6468
data: { metrics: ['eval/accuracy'] },
6569
};
6670
const optionMetric = buildBarOption(specNoTitle, mockBarData);
67-
expect((optionMetric.series as any[])[0].name).toBe('eval/accuracy');
71+
expect((optionMetric.series as BarSeriesOption[])[0].name).toBe('eval/accuracy');
6872

6973
const dataNoMetric: PlotData = {
7074
plotId: 'accuracy_dist',
@@ -74,7 +78,7 @@ describe('bar_options_builder', () => {
7478
},
7579
};
7680
const optionFallback = buildBarOption(specNoTitle, dataNoMetric);
77-
expect((optionFallback.series as any[])[0].name).toBe('Value');
81+
expect((optionFallback.series as BarSeriesOption[])[0].name).toBe('Value');
7882
});
7983
});
8084
});

0 commit comments

Comments
 (0)