Skip to content

Commit 4ddd20d

Browse files
fixed path and added unit tests
1 parent 55c6c85 commit 4ddd20d

4 files changed

Lines changed: 49 additions & 14 deletions

File tree

src/app/chat/component/chat-page/chat-page.component.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChatComponent } from './chat-page.component';
2-
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
33
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
44
import { NgClass, NgForOf, NgIf, NgStyle } from '@angular/common';
55
import { FormsModule } from '@angular/forms';
@@ -12,6 +12,7 @@ describe('ChatComponent', () => {
1212
let component: ChatComponent;
1313
let fixture: ComponentFixture<ChatComponent>;
1414
let httpMock: HttpTestingController;
15+
let historyMock: jasmine.Spy;
1516

1617
beforeEach(async () => {
1718
const setup = await setupChatComponentTest();
@@ -20,6 +21,7 @@ describe('ChatComponent', () => {
2021
httpMock = setup.httpMock;
2122

2223
localStorage.setItem('accessToken', 'mock-token');
24+
historyMock = spyOnProperty(history, 'state', 'get').and.returnValue({ selectedChatId: 123 });
2325
});
2426

2527
afterEach(() => {
@@ -31,6 +33,49 @@ describe('ChatComponent', () => {
3133
expect(component).toBeTruthy();
3234
});
3335

36+
it('should set selectedChatId if chatId is in history state', () => {
37+
historyMock.and.returnValue({ selectedChatId: 123 });
38+
component.ngOnInit();
39+
expect(component.selectedChatId).toEqual(123);
40+
});
41+
it('should not set selectedChatId if no chatId is in history state', () => {
42+
historyMock.and.returnValue({ selectedChatId: undefined });
43+
component.ngOnInit();
44+
expect(component.selectedChatId).toEqual(undefined);
45+
});
46+
it('should call selectChat method if selectedChatId was provided', fakeAsync(() => {
47+
const selectChatSpy = spyOn(component, 'selectChat').and.callThrough();
48+
component.selectedChatId = 123;
49+
spyOn(component['http'], 'get').and.returnValue(
50+
of({
51+
page: [
52+
{
53+
id: 123,
54+
username: '61',
55+
chatId: '123',
56+
lastMessage: { text: 'test', sendAt: '2025-07-29T10:00:00Z' }
57+
}
58+
]
59+
})
60+
);
61+
62+
component.loadAllChats();
63+
tick();
64+
65+
expect(component.selectedChatId).toEqual(123);
66+
expect(selectChatSpy).toHaveBeenCalled();
67+
expect(selectChatSpy).toHaveBeenCalledWith(jasmine.objectContaining({ chatInternalId: 123 }));
68+
}));
69+
70+
it('should not call selectChat method if selectedChatId was not provided', () => {
71+
const selectChatSpy = spyOn(component, 'selectChat');
72+
73+
component.loadAllChats();
74+
75+
expect(component.selectedChatId).toEqual(undefined);
76+
expect(selectChatSpy).not.toHaveBeenCalled();
77+
});
78+
3479
it('should not send message if newMessage is blank or no chat', () => {
3580
spyOn(component['http'], 'post');
3681
component.newMessage = ' ';

src/app/chat/component/chat-page/chat-page.component.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ export class ChatComponent implements OnInit {
4141
if (history.state.selectedChatId) {
4242
this.selectedChatId = history.state.selectedChatId;
4343
}
44-
this.store
45-
.select(userRoleSelector)
46-
.pipe(take(1))
47-
.subscribe((role) => {
48-
if (!role) {
49-
this.router.navigate(['/']);
50-
} else if (role === 'ROLE_UBS_EMPLOYEE') {
51-
this.router.navigate(['/ubs/admin/orders']);
52-
}
53-
});
5444
this.store.select(userRoleSelector).pipe(take(1));
5545
this.loadAllChats();
5646
}

src/app/ubs/ubs-admin/components/ubs-admin-customers/ubs-admin-customers.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ describe('UbsAdminCustomersComponent', () => {
142142
expect(component.filterForm.value.bonusesTo).toBe('');
143143
});
144144

145-
fit('on onOpenChat should redirect to chat with a client', () => {
145+
it('on onOpenChat should redirect to chat with a client', () => {
146146
const chatIdMock = 12;
147147

148148
component.onOpenChat(chatIdMock);
149149

150150
expect(routerSpy.navigate).toHaveBeenCalled();
151-
expect(routerSpy.navigate).toHaveBeenCalledWith(['greenCity', 'chat-page'], { state: { selectedChatId: chatIdMock } });
151+
expect(routerSpy.navigate).toHaveBeenCalledWith(['ubs/admin', 'chat-page'], { state: { selectedChatId: chatIdMock } });
152152
});
153153
});

src/app/ubs/ubs-admin/components/ubs-admin-customers/ubs-admin-customers.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export class UbsAdminCustomersComponent implements OnInit, AfterViewChecked, OnD
416416
}
417417

418418
onOpenChat(chatId: number) {
419-
this.router.navigate(['greenCity', 'chat-page'], { state: { selectedChatId: chatId } });
419+
this.router.navigate(['ubs/admin', 'chat-page'], { state: { selectedChatId: chatId } });
420420
}
421421

422422
private openCustomer(row, username): void {

0 commit comments

Comments
 (0)