Skip to content

Commit f76a112

Browse files
google-genai-botcopybara-github
authored andcommitted
ADK changes
PiperOrigin-RevId: 827517900
1 parent 375dbed commit f76a112

File tree

2 files changed

+97
-33
lines changed

2 files changed

+97
-33
lines changed

src/app/components/session-tab/session-tab.component.spec.ts

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,47 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { ComponentFixture, TestBed } from '@angular/core/testing';
19-
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
20-
import { SessionTabComponent } from './session-tab.component';
21-
import { SESSION_SERVICE, SessionService } from '../../core/services/interfaces/session';
22-
import { UI_STATE_SERVICE, UiStateService } from '../../core/services/interfaces/ui-state';
23-
import { MockUiStateService } from '../../core/services/testing/mock-ui-state.service';
24-
import { of } from 'rxjs';
25-
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
18+
import {ComponentFixture, TestBed,} from '@angular/core/testing';
19+
import {MatDialog, MatDialogModule} from '@angular/material/dialog';
20+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
21+
// 1p-ONLY-IMPORTS: import {beforeEach, describe, it,}
22+
import {of} from 'rxjs';
23+
24+
import {SESSION_SERVICE, SessionService,} from '../../core/services/interfaces/session';
25+
import {UI_STATE_SERVICE,} from '../../core/services/interfaces/ui-state';
26+
import {MockSessionService} from '../../core/services/testing/mock-session.service';
27+
import {MockUiStateService} from '../../core/services/testing/mock-ui-state.service';
28+
import {fakeAsync, initTestBed} from '../../testing/utils';
29+
30+
import {SessionTabComponent} from './session-tab.component';
2631

2732
describe('SessionTabComponent', () => {
2833
let component: SessionTabComponent;
2934
let fixture: ComponentFixture<SessionTabComponent>;
3035
let mockUiStateService: MockUiStateService;
36+
let sessionService: MockSessionService;
3137

3238
beforeEach(async () => {
33-
const sessionService = jasmine.createSpyObj<SessionService>([
34-
'listSessions',
35-
'getSession',
36-
'canEdit',
37-
]);
38-
sessionService.listSessions.and.returnValue(of([]));
39-
sessionService.getSession.and.returnValue(of({} as any));
40-
sessionService.canEdit.and.returnValue(of(true));
39+
sessionService = new MockSessionService();
4140
mockUiStateService = new MockUiStateService();
4241

42+
sessionService.listSessionsResponse.next([]);
43+
sessionService.canEditResponse.next(true);
4344

44-
await TestBed.configureTestingModule({
45-
imports: [MatDialogModule, SessionTabComponent, NoopAnimationsModule],
46-
providers: [
47-
{ provide: MatDialog, useValue: jasmine.createSpyObj('MatDialog', ['open']) },
48-
{ provide: SESSION_SERVICE, useValue: sessionService },
49-
{ provide: UI_STATE_SERVICE, useValue: mockUiStateService },
50-
],
51-
}).compileComponents();
45+
initTestBed(); // required for 1p compatibility
46+
await TestBed
47+
.configureTestingModule({
48+
imports: [MatDialogModule, SessionTabComponent, NoopAnimationsModule],
49+
providers: [
50+
{
51+
provide: MatDialog,
52+
useValue: jasmine.createSpyObj('MatDialog', ['open']),
53+
},
54+
{provide: SESSION_SERVICE, useValue: sessionService},
55+
{provide: UI_STATE_SERVICE, useValue: mockUiStateService},
56+
],
57+
})
58+
.compileComponents();
5259

5360
fixture = TestBed.createComponent(SessionTabComponent);
5461
component = fixture.componentInstance;
@@ -59,8 +66,63 @@ describe('SessionTabComponent', () => {
5966
expect(component).toBeTruthy();
6067
});
6168

62-
it('should set session loading state when getting a session', () => {
63-
component.getSession('session1');
64-
expect(mockUiStateService.setIsSessionLoading).toHaveBeenCalledWith(true);
69+
describe('when getting a session', () => {
70+
beforeEach(() => {
71+
spyOn(component.sessionSelected, 'emit');
72+
});
73+
74+
it('should set session loading state to true', () => {
75+
component.getSession('session1');
76+
expect(mockUiStateService.setIsSessionLoading).toHaveBeenCalledWith(true);
77+
});
78+
79+
describe('when getting a session is successful', () => {
80+
beforeEach(() => {
81+
sessionService.getSessionResponse.next({} as any);
82+
component.getSession('session1');
83+
});
84+
85+
it('emits sessionSelected', () => {
86+
expect(component.sessionSelected.emit).toHaveBeenCalled();
87+
});
88+
89+
it('sets session loading state to false', fakeAsync(() => {
90+
expect(mockUiStateService.setIsSessionLoading)
91+
.toHaveBeenCalledWith(
92+
false,
93+
);
94+
}));
95+
});
96+
97+
describe('when getting a session throws error', () => {
98+
beforeEach(() => {
99+
sessionService.getSessionResponse.error(new Error('error'));
100+
component.getSession('session1');
101+
});
102+
103+
it('hides session loading state', fakeAsync(() => {
104+
expect(mockUiStateService.setIsSessionLoading)
105+
.toHaveBeenCalledWith(
106+
false,
107+
);
108+
}));
109+
110+
it(
111+
'does not emit sessionSelected', fakeAsync(() => {
112+
expect(component.sessionSelected.emit).not.toHaveBeenCalled();
113+
}));
114+
115+
describe('on retry', () => {
116+
beforeEach(() => {
117+
sessionService.getSession.calls.reset();
118+
sessionService.getSessionResponse.next({} as any);
119+
component.getSession('session1');
120+
});
121+
122+
it('fetches session again', () => {
123+
expect(sessionService.getSession).toHaveBeenCalled();
124+
});
125+
});
126+
});
65127
});
66128
});

src/app/components/session-tab/session-tab.component.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {AsyncPipe, NgClass} from '@angular/common';
1919
import {ChangeDetectorRef, Component, EventEmitter, inject, Input, OnInit, Output, signal} from '@angular/core';
2020
import {MatChip} from '@angular/material/chips';
2121
import {MatProgressBar} from '@angular/material/progress-bar';
22-
import {Subject} from 'rxjs';
23-
import {debounceTime, map, switchMap, tap} from 'rxjs/operators';
22+
import {of, Subject} from 'rxjs';
23+
import {catchError, debounceTime, map, switchMap, tap} from 'rxjs/operators';
2424

2525
import {Session} from '../../core/models/Session';
2626
import {SESSION_SERVICE} from '../../core/services/interfaces/session';
@@ -86,15 +86,17 @@ export class SessionTabComponent implements OnInit {
8686
tap(() => {
8787
this.uiStateService.setIsSessionLoading(true);
8888
}),
89-
switchMap(
90-
(sessionId) => this.sessionService.getSession(
91-
this.userId, this.appName, sessionId)),
89+
switchMap((sessionId) => {
90+
return this.sessionService
91+
.getSession(this.userId, this.appName, sessionId)
92+
.pipe(catchError(() => of(null)));
93+
}),
9294
tap((res) => {
95+
if (!res) return;
9396
const session = this.fromApiResultToSession(res);
9497
this.sessionSelected.emit(session);
9598
this.changeDetectorRef.markForCheck();
9699
}),
97-
debounceTime(300),
98100
)
99101
.subscribe(
100102
(session) => {

0 commit comments

Comments
 (0)