Skip to content

Commit 7a4b8ea

Browse files
committed
WEB-882: Add unit tests for CloseClientComponent
1 parent 7886c79 commit 7a4b8ea

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { ActivatedRoute, Router } from '@angular/router';
3+
import { of, throwError } from 'rxjs';
4+
import { CloseClientComponent } from './close-client.component';
5+
import { ClientsService } from 'app/clients/clients.service';
6+
import { SettingsService } from 'app/settings/settings.service';
7+
import { Dates } from 'app/core/utils/dates';
8+
import { TranslateModule } from '@ngx-translate/core';
9+
import { provideNativeDateAdapter } from '@angular/material/core';
10+
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
11+
12+
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
13+
14+
describe('CloseClientComponent', () => {
15+
let component: CloseClientComponent;
16+
let fixture: ComponentFixture<CloseClientComponent>;
17+
18+
let router: jest.Mocked<Router>;
19+
let clientsService: jest.Mocked<ClientsService>;
20+
let settingsService: SettingsService;
21+
let dates: jest.Mocked<Dates>;
22+
23+
const clientId = '456';
24+
const businessDate = new Date(2025, 11, 20);
25+
26+
const setup = async () => {
27+
router = { navigate: jest.fn() } as any;
28+
29+
clientsService = {
30+
executeClientCommand: jest.fn(() => of({}))
31+
} as any;
32+
33+
settingsService = {
34+
language: { code: 'en' },
35+
dateFormat: 'dd MMMM yyyy',
36+
businessDate
37+
} as any;
38+
39+
dates = {
40+
formatDate: jest.fn(() => '20 March 2026')
41+
} as any;
42+
43+
await TestBed.configureTestingModule({
44+
imports: [
45+
CloseClientComponent,
46+
TranslateModule.forRoot()
47+
],
48+
providers: [
49+
{ provide: Router, useValue: router },
50+
{
51+
provide: ActivatedRoute,
52+
useValue: {
53+
data: of({ clientActionData: { narrations: [] } }),
54+
parent: { snapshot: { params: { clientId } } }
55+
}
56+
},
57+
{ provide: ClientsService, useValue: clientsService },
58+
{ provide: SettingsService, useValue: settingsService },
59+
{ provide: Dates, useValue: dates },
60+
provideNativeDateAdapter(),
61+
provideAnimationsAsync()
62+
]
63+
}).compileComponents();
64+
65+
fixture = TestBed.createComponent(CloseClientComponent);
66+
component = fixture.componentInstance;
67+
fixture.detectChanges();
68+
};
69+
70+
const fillValidForm = (date: Date | string = new Date(2025, 10, 3)) => {
71+
component.closeClientForm.patchValue({
72+
closureDate: date,
73+
closureReasonId: 1
74+
});
75+
};
76+
77+
beforeEach(setup);
78+
79+
it('should initialize correctly', () => {
80+
expect(component.clientId).toBe(clientId);
81+
expect(component.maxDate).toEqual(businessDate);
82+
expect(component.closeClientForm.valid).toBe(false);
83+
});
84+
85+
it('should be invalid when required fields are missing', () => {
86+
expect(component.closeClientForm.valid).toBe(false);
87+
});
88+
89+
it('should be valid when all required fields are provided', () => {
90+
fillValidForm();
91+
expect(component.closeClientForm.valid).toBe(true);
92+
});
93+
94+
it('should submit and call API with formatted date', () => {
95+
const date = new Date(2025, 10, 3);
96+
fillValidForm(date);
97+
98+
component.submit();
99+
100+
expect(dates.formatDate).toHaveBeenCalledWith(date, 'dd MMMM yyyy');
101+
102+
expect(clientsService.executeClientCommand).toHaveBeenCalledWith(
103+
clientId,
104+
'close',
105+
expect.objectContaining({
106+
closureDate: '20 March 2026',
107+
closureReasonId: 1,
108+
locale: 'en',
109+
dateFormat: 'dd MMMM yyyy'
110+
})
111+
);
112+
});
113+
114+
it('should not format date if already a string', () => {
115+
fillValidForm('14 August 2025');
116+
117+
component.submit();
118+
119+
expect(dates.formatDate).not.toHaveBeenCalled();
120+
});
121+
122+
it('should navigate after successful submission', () => {
123+
fillValidForm();
124+
125+
component.submit();
126+
127+
expect(router.navigate).toHaveBeenCalled();
128+
});
129+
130+
it('should not navigate if API call fails', () => {
131+
clientsService.executeClientCommand.mockReturnValueOnce(throwError(() => new Error('API error')));
132+
133+
fillValidForm();
134+
135+
component.submit();
136+
137+
expect(router.navigate).not.toHaveBeenCalled();
138+
});
139+
});

0 commit comments

Comments
 (0)