|
| 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 | +}); |
0 commit comments