|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { TimePlanningsContainerComponent } from './time-plannings-container.component'; |
| 3 | +import { TimePlanningPnPlanningsService } from '../../../services/time-planning-pn-plannings.service'; |
| 4 | +import { TimePlanningPnSettingsService } from '../../../services/time-planning-pn-settings.service'; |
| 5 | +import { MatDialog } from '@angular/material/dialog'; |
| 6 | +import { Store } from '@ngrx/store'; |
| 7 | +import { of } from 'rxjs'; |
| 8 | +import { format } from 'date-fns'; |
| 9 | + |
| 10 | +describe('TimePlanningsContainerComponent', () => { |
| 11 | + let component: TimePlanningsContainerComponent; |
| 12 | + let fixture: ComponentFixture<TimePlanningsContainerComponent>; |
| 13 | + let mockPlanningsService: jasmine.SpyObj<TimePlanningPnPlanningsService>; |
| 14 | + let mockSettingsService: jasmine.SpyObj<TimePlanningPnSettingsService>; |
| 15 | + let mockDialog: jasmine.SpyObj<MatDialog>; |
| 16 | + let mockStore: jasmine.SpyObj<Store>; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + mockPlanningsService = jasmine.createSpyObj('TimePlanningPnPlanningsService', ['getPlannings', 'updatePlanning']); |
| 20 | + mockSettingsService = jasmine.createSpyObj('TimePlanningPnSettingsService', ['getAvailableSites', 'getResignedSites', 'getAssignedSite', 'updateAssignedSite']); |
| 21 | + mockDialog = jasmine.createSpyObj('MatDialog', ['open']); |
| 22 | + mockStore = jasmine.createSpyObj('Store', ['select']); |
| 23 | + |
| 24 | + mockStore.select.and.returnValue(of('en-US')); |
| 25 | + mockSettingsService.getAvailableSites.and.returnValue(of({ success: true, model: [] })); |
| 26 | + mockPlanningsService.getPlannings.and.returnValue(of({ success: true, model: [] })); |
| 27 | + |
| 28 | + await TestBed.configureTestingModule({ |
| 29 | + declarations: [TimePlanningsContainerComponent], |
| 30 | + providers: [ |
| 31 | + { provide: TimePlanningPnPlanningsService, useValue: mockPlanningsService }, |
| 32 | + { provide: TimePlanningPnSettingsService, useValue: mockSettingsService }, |
| 33 | + { provide: MatDialog, useValue: mockDialog }, |
| 34 | + { provide: Store, useValue: mockStore } |
| 35 | + ] |
| 36 | + }).compileComponents(); |
| 37 | + |
| 38 | + fixture = TestBed.createComponent(TimePlanningsContainerComponent); |
| 39 | + component = fixture.componentInstance; |
| 40 | + }); |
| 41 | + |
| 42 | + it('should create', () => { |
| 43 | + expect(component).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Date Navigation', () => { |
| 47 | + beforeEach(() => { |
| 48 | + component.dateFrom = new Date(2024, 0, 15); // Jan 15, 2024 |
| 49 | + component.dateTo = new Date(2024, 0, 21); // Jan 21, 2024 |
| 50 | + }); |
| 51 | + |
| 52 | + it('should move dates backward by 7 days when goBackward is called', () => { |
| 53 | + const expectedDateFrom = new Date(2024, 0, 8); // Jan 8, 2024 |
| 54 | + const expectedDateTo = new Date(2024, 0, 14); // Jan 14, 2024 |
| 55 | + |
| 56 | + component.goBackward(); |
| 57 | + |
| 58 | + expect(component.dateFrom.getDate()).toBe(expectedDateFrom.getDate()); |
| 59 | + expect(component.dateTo.getDate()).toBe(expectedDateTo.getDate()); |
| 60 | + expect(mockPlanningsService.getPlannings).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should move dates forward by 7 days when goForward is called', () => { |
| 64 | + const expectedDateFrom = new Date(2024, 0, 22); // Jan 22, 2024 |
| 65 | + const expectedDateTo = new Date(2024, 0, 28); // Jan 28, 2024 |
| 66 | + |
| 67 | + component.goForward(); |
| 68 | + |
| 69 | + expect(component.dateFrom.getDate()).toBe(expectedDateFrom.getDate()); |
| 70 | + expect(component.dateTo.getDate()).toBe(expectedDateTo.getDate()); |
| 71 | + expect(mockPlanningsService.getPlannings).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Date Formatting', () => { |
| 76 | + it('should format date range correctly', () => { |
| 77 | + component.dateFrom = new Date(2024, 0, 15); // Jan 15, 2024 |
| 78 | + component.dateTo = new Date(2024, 0, 21); // Jan 21, 2024 |
| 79 | + |
| 80 | + const result = component.formatDateRange(); |
| 81 | + |
| 82 | + expect(result).toBe('15.01.2024 - 21.01.2024'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle single digit days and months correctly', () => { |
| 86 | + component.dateFrom = new Date(2024, 0, 1); // Jan 1, 2024 |
| 87 | + component.dateTo = new Date(2024, 0, 7); // Jan 7, 2024 |
| 88 | + |
| 89 | + const result = component.formatDateRange(); |
| 90 | + |
| 91 | + expect(result).toBe('01.01.2024 - 07.01.2024'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('Event Handlers', () => { |
| 96 | + it('should call getPlannings when onTimePlanningChanged is triggered', () => { |
| 97 | + spyOn(component, 'getPlannings'); |
| 98 | + |
| 99 | + component.onTimePlanningChanged({}); |
| 100 | + |
| 101 | + expect(component.getPlannings).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should call getPlannings when onAssignedSiteChanged is triggered', () => { |
| 105 | + spyOn(component, 'getPlannings'); |
| 106 | + |
| 107 | + component.onAssignedSiteChanged({}); |
| 108 | + |
| 109 | + expect(component.getPlannings).toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should update siteId and call getPlannings when onSiteChanged is triggered', () => { |
| 113 | + spyOn(component, 'getPlannings'); |
| 114 | + const testSiteId = 123; |
| 115 | + |
| 116 | + component.onSiteChanged(testSiteId); |
| 117 | + |
| 118 | + expect(component.siteId).toBe(testSiteId); |
| 119 | + expect(component.getPlannings).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('Dialog', () => { |
| 124 | + it('should open download excel dialog with available sites', () => { |
| 125 | + component.availableSites = [{ id: 1, name: 'Test Site' } as any]; |
| 126 | + const mockDialogRef = { afterClosed: () => of(null) }; |
| 127 | + mockDialog.open.and.returnValue(mockDialogRef as any); |
| 128 | + |
| 129 | + component.openDownloadExcelDialog(); |
| 130 | + |
| 131 | + expect(mockDialog.open).toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('Show Resigned Sites', () => { |
| 136 | + it('should load resigned sites when showResignedSites is true', () => { |
| 137 | + mockSettingsService.getResignedSites.and.returnValue(of({ success: true, model: [{ id: 1, name: 'Resigned Site' }] } as any)); |
| 138 | + |
| 139 | + component.onShowResignedSitesChanged({ checked: true }); |
| 140 | + |
| 141 | + expect(mockSettingsService.getResignedSites).toHaveBeenCalled(); |
| 142 | + expect(component.showResignedSites).toBe(true); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should load available sites when showResignedSites is false', () => { |
| 146 | + component.showResignedSites = true; |
| 147 | + mockSettingsService.getAvailableSites.and.returnValue(of({ success: true, model: [{ id: 1, name: 'Available Site' }] } as any)); |
| 148 | + |
| 149 | + component.onShowResignedSitesChanged({ checked: false }); |
| 150 | + |
| 151 | + expect(mockSettingsService.getAvailableSites).toHaveBeenCalled(); |
| 152 | + expect(component.showResignedSites).toBe(false); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments