Skip to content

Commit 30acab5

Browse files
Copilotrenemadsen
andcommitted
Add unit tests for services and dialog components
Co-authored-by: renemadsen <[email protected]>
1 parent 0060907 commit 30acab5

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { DownloadExcelDialogComponent } from './download-excel-dialog.component';
3+
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
4+
import { TimePlanningPnWorkingHoursService } from '../../../../services';
5+
import { ToastrService } from 'ngx-toastr';
6+
import { of, throwError } from 'rxjs';
7+
import { format } from 'date-fns';
8+
9+
describe('DownloadExcelDialogComponent', () => {
10+
let component: DownloadExcelDialogComponent;
11+
let fixture: ComponentFixture<DownloadExcelDialogComponent>;
12+
let mockWorkingHoursService: jasmine.SpyObj<TimePlanningPnWorkingHoursService>;
13+
let mockToastrService: jasmine.SpyObj<ToastrService>;
14+
15+
beforeEach(async () => {
16+
mockWorkingHoursService = jasmine.createSpyObj('TimePlanningPnWorkingHoursService', [
17+
'downloadReport',
18+
'downloadReportAllWorkers'
19+
]);
20+
mockToastrService = jasmine.createSpyObj('ToastrService', ['error', 'success']);
21+
22+
await TestBed.configureTestingModule({
23+
declarations: [DownloadExcelDialogComponent],
24+
providers: [
25+
{ provide: MAT_DIALOG_DATA, useValue: [] },
26+
{ provide: TimePlanningPnWorkingHoursService, useValue: mockWorkingHoursService },
27+
{ provide: ToastrService, useValue: mockToastrService }
28+
]
29+
}).compileComponents();
30+
31+
fixture = TestBed.createComponent(DownloadExcelDialogComponent);
32+
component = fixture.componentInstance;
33+
});
34+
35+
it('should create', () => {
36+
expect(component).toBeTruthy();
37+
});
38+
39+
describe('Site Selection', () => {
40+
it('should update siteId when onSiteChanged is called', () => {
41+
const testSiteId = 123;
42+
43+
component.onSiteChanged(testSiteId);
44+
45+
expect(component.siteId).toBe(testSiteId);
46+
});
47+
});
48+
49+
describe('Date Updates', () => {
50+
it('should update dateFrom when updateDateFrom is called', () => {
51+
const testDate = new Date(2024, 0, 15);
52+
const event = { value: testDate } as any;
53+
54+
component.updateDateFrom(event);
55+
56+
expect(component.dateFrom).toBe(testDate);
57+
});
58+
59+
it('should update dateTo when updateDateTo is called', () => {
60+
const testDate = new Date(2024, 0, 21);
61+
const event = { value: testDate } as any;
62+
63+
component.updateDateTo(event);
64+
65+
expect(component.dateTo).toBe(testDate);
66+
});
67+
});
68+
69+
describe('Excel Report Download', () => {
70+
beforeEach(() => {
71+
component.dateFrom = new Date(2024, 0, 15);
72+
component.dateTo = new Date(2024, 0, 21);
73+
component.siteId = 123;
74+
});
75+
76+
it('should call downloadReport with correct model', () => {
77+
const mockBlob = new Blob(['test'], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
78+
mockWorkingHoursService.downloadReport.and.returnValue(of(mockBlob));
79+
80+
component.onDownloadExcelReport();
81+
82+
expect(mockWorkingHoursService.downloadReport).toHaveBeenCalledWith({
83+
dateFrom: '2024-01-15',
84+
dateTo: '2024-01-21',
85+
siteId: 123
86+
});
87+
});
88+
89+
it('should show error toast when download fails', (done) => {
90+
mockWorkingHoursService.downloadReport.and.returnValue(
91+
throwError(() => new Error('Download failed'))
92+
);
93+
94+
component.onDownloadExcelReport();
95+
96+
// Wait a bit for async operations
97+
setTimeout(() => {
98+
expect(mockToastrService.error).toHaveBeenCalledWith('Error downloading report');
99+
done();
100+
}, 100);
101+
});
102+
});
103+
104+
describe('Excel Report All Workers Download', () => {
105+
beforeEach(() => {
106+
component.dateFrom = new Date(2024, 0, 15);
107+
component.dateTo = new Date(2024, 0, 21);
108+
});
109+
110+
it('should call downloadReportAllWorkers with correct model', () => {
111+
const mockBlob = new Blob(['test'], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
112+
mockWorkingHoursService.downloadReportAllWorkers.and.returnValue(of(mockBlob));
113+
114+
component.onDownloadExcelReportAllWorkers();
115+
116+
expect(mockWorkingHoursService.downloadReportAllWorkers).toHaveBeenCalledWith({
117+
dateFrom: '2024-01-15',
118+
dateTo: '2024-01-21'
119+
});
120+
});
121+
122+
it('should show error toast when download all workers fails', (done) => {
123+
mockWorkingHoursService.downloadReportAllWorkers.and.returnValue(
124+
throwError(() => new Error('Download failed'))
125+
);
126+
127+
component.onDownloadExcelReportAllWorkers();
128+
129+
// Wait a bit for async operations
130+
setTimeout(() => {
131+
expect(mockToastrService.error).toHaveBeenCalledWith('Error downloading report');
132+
done();
133+
}, 100);
134+
});
135+
136+
it('should not include siteId in all workers report model', () => {
137+
const mockBlob = new Blob(['test'], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
138+
mockWorkingHoursService.downloadReportAllWorkers.and.returnValue(of(mockBlob));
139+
component.siteId = 999; // Should not be included
140+
141+
component.onDownloadExcelReportAllWorkers();
142+
143+
const callArgs = mockWorkingHoursService.downloadReportAllWorkers.calls.mostRecent().args[0];
144+
expect(callArgs).not.toHaveProperty('siteId');
145+
});
146+
});
147+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { TimePlanningPnPlanningsService } from './time-planning-pn-plannings.service';
3+
import { ApiBaseService } from 'src/app/common/services';
4+
import { of } from 'rxjs';
5+
6+
describe('TimePlanningPnPlanningsService', () => {
7+
let service: TimePlanningPnPlanningsService;
8+
let mockApiBaseService: jasmine.SpyObj<ApiBaseService>;
9+
10+
beforeEach(() => {
11+
mockApiBaseService = jasmine.createSpyObj('ApiBaseService', ['post', 'put', 'get']);
12+
13+
TestBed.configureTestingModule({
14+
providers: [
15+
TimePlanningPnPlanningsService,
16+
{ provide: ApiBaseService, useValue: mockApiBaseService }
17+
]
18+
});
19+
20+
service = TestBed.inject(TimePlanningPnPlanningsService);
21+
});
22+
23+
it('should be created', () => {
24+
expect(service).toBeTruthy();
25+
});
26+
27+
describe('getPlannings', () => {
28+
it('should call apiBaseService.post with correct parameters', () => {
29+
const mockRequest = {
30+
dateFrom: '2024-01-01',
31+
dateTo: '2024-01-07',
32+
sort: 'Date',
33+
isSortDsc: true,
34+
siteId: 1,
35+
showResignedSites: false
36+
};
37+
const mockResponse = { success: true, model: [] };
38+
mockApiBaseService.post.and.returnValue(of(mockResponse as any));
39+
40+
service.getPlannings(mockRequest).subscribe(result => {
41+
expect(result).toEqual(mockResponse as any);
42+
});
43+
44+
expect(mockApiBaseService.post).toHaveBeenCalledWith(
45+
'api/time-planning-pn/plannings/index',
46+
mockRequest
47+
);
48+
});
49+
50+
it('should handle empty response', () => {
51+
const mockRequest = {
52+
dateFrom: '2024-01-01',
53+
dateTo: '2024-01-07',
54+
sort: 'Date',
55+
isSortDsc: true,
56+
siteId: 0,
57+
showResignedSites: false
58+
};
59+
const mockResponse = { success: true, model: [] };
60+
mockApiBaseService.post.and.returnValue(of(mockResponse as any));
61+
62+
service.getPlannings(mockRequest).subscribe(result => {
63+
expect(result.model).toEqual([]);
64+
});
65+
});
66+
});
67+
68+
describe('updatePlanning', () => {
69+
it('should call apiBaseService.put with correct parameters', () => {
70+
const mockPlanningModel = {
71+
id: 123,
72+
planHours: 8,
73+
message: 1,
74+
planText: 'Test planning'
75+
} as any;
76+
const mockResponse = { success: true };
77+
mockApiBaseService.put.and.returnValue(of(mockResponse as any));
78+
79+
service.updatePlanning(mockPlanningModel, 123).subscribe(result => {
80+
expect(result).toEqual(mockResponse as any);
81+
});
82+
83+
expect(mockApiBaseService.put).toHaveBeenCalledWith(
84+
'api/time-planning-pn/plannings/123',
85+
mockPlanningModel
86+
);
87+
});
88+
89+
it('should construct correct URL with id parameter', () => {
90+
const mockPlanningModel = { id: 456 } as any;
91+
const mockResponse = { success: true };
92+
mockApiBaseService.put.and.returnValue(of(mockResponse as any));
93+
94+
service.updatePlanning(mockPlanningModel, 456).subscribe();
95+
96+
expect(mockApiBaseService.put).toHaveBeenCalledWith(
97+
'api/time-planning-pn/plannings/456',
98+
mockPlanningModel
99+
);
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)