-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
4,369 additions
and
2,798 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 150 additions & 5 deletions
155
...angular/src/app/components/edit-project/project-fiscals/project-fiscals.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,168 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ProjectFiscalsComponent } from './project-fiscals.component'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { of, throwError } from 'rxjs'; | ||
import { ProjectService } from 'src/app/services/project-services'; | ||
import { CodeTableServices } from 'src/app/services/code-table-services'; | ||
import { MatSnackBar } from '@angular/material/snack-bar'; | ||
import { FormBuilder } from '@angular/forms'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
const mockProjectService = { | ||
getProjectFiscalsByProjectGuid: jasmine.createSpy('getProjectFiscalsByProjectGuid').and.returnValue( | ||
of({ _embedded: { projectFiscals: [{ fiscalYear: 2023, projectFiscalName: 'Test Fiscal' }] } }) | ||
), | ||
updateProjectFiscal: jasmine.createSpy('updateProjectFiscal').and.returnValue(of({})), | ||
createProjectFiscal: jasmine.createSpy('createProjectFiscal').and.returnValue(of({})), | ||
}; | ||
|
||
const mockCodeTableServices = { | ||
fetchCodeTable: jasmine.createSpy('fetchCodeTable').and.returnValue(of({ _embedded: {} })), | ||
}; | ||
|
||
const mockSnackBar = { | ||
open: jasmine.createSpy('open'), | ||
}; | ||
|
||
describe('ProjectFiscalsComponent', () => { | ||
let component: ProjectFiscalsComponent; | ||
let fixture: ComponentFixture<ProjectFiscalsComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ProjectFiscalsComponent] | ||
}) | ||
.compileComponents(); | ||
imports: [ProjectFiscalsComponent, BrowserAnimationsModule], | ||
providers: [ | ||
{ provide: ActivatedRoute, useValue: { snapshot: { queryParamMap: { get: () => 'test-guid' } } } }, | ||
{ provide: ProjectService, useValue: mockProjectService }, | ||
{ provide: CodeTableServices, useValue: mockCodeTableServices }, | ||
{ provide: MatSnackBar, useValue: mockSnackBar }, | ||
FormBuilder, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(ProjectFiscalsComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
it('should create the component', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should generate fiscal years correctly', () => { | ||
component.generateFiscalYears(); | ||
expect(component.fiscalYears.length).toBe(11); | ||
expect(component.fiscalYears[0]).toBe(`${new Date().getFullYear() - 5}/${(new Date().getFullYear() - 4).toString().slice(-2)}`); | ||
}); | ||
|
||
it('should load code tables successfully', () => { | ||
mockCodeTableServices.fetchCodeTable.calls.reset(); // ✅ Reset call count | ||
component.loadCodeTables(); | ||
expect(mockCodeTableServices.fetchCodeTable).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should handle errors in loading code tables', () => { | ||
mockCodeTableServices.fetchCodeTable.calls.reset(); // ✅ Reset call count | ||
mockCodeTableServices.fetchCodeTable.and.returnValue(throwError(() => new Error('Error fetching data'))); | ||
component.loadCodeTables(); | ||
expect(mockCodeTableServices.fetchCodeTable).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should load project fiscals', () => { | ||
// ✅ Ensure the mock returns a valid response | ||
mockProjectService.getProjectFiscalsByProjectGuid.and.returnValue( | ||
of({ | ||
_embedded: { | ||
projectFiscals: [ | ||
{ fiscalYear: 2023, projectFiscalName: 'Test Fiscal' } | ||
] | ||
} | ||
}) | ||
); | ||
|
||
component.loadProjectFiscals(); | ||
|
||
expect(mockProjectService.getProjectFiscalsByProjectGuid).toHaveBeenCalledWith('test-guid'); | ||
expect(component.projectFiscals.length).toBeGreaterThan(0); // ✅ Should now have at least one fiscal | ||
expect(component.fiscalForms.length).toBe(component.projectFiscals.length); // ✅ Forms should match project fiscals count | ||
}); | ||
|
||
|
||
it('should handle errors in loading project fiscals', () => { | ||
mockProjectService.getProjectFiscalsByProjectGuid.and.returnValue(throwError(() => new Error('API Error'))); | ||
component.loadProjectFiscals(); | ||
expect(component.projectFiscals.length).toBe(0); | ||
}); | ||
|
||
it('should add a new fiscal', () => { | ||
component.projectFiscals = []; // ✅ Ensure projectFiscals starts empty | ||
component.projectGuid = 'test-guid'; // ✅ Ensure projectGuid is set before calling the method | ||
|
||
component.addNewFiscal(); | ||
|
||
expect(component.projectFiscals.length).toBe(1); // ✅ Should increase from 0 to 1 | ||
expect(component.selectedTabIndex).toBe(0); // ✅ Should select the first added fiscal | ||
}); | ||
|
||
it('should save a new fiscal', () => { | ||
spyOn(component, 'loadProjectFiscals'); | ||
|
||
// Ensure mock `createProjectFiscal` returns success | ||
mockProjectService.createProjectFiscal.and.returnValue(of({})); // ✅ Fix: Return success response | ||
|
||
component.onSaveFiscal(0); | ||
|
||
expect(mockProjectService.createProjectFiscal).toHaveBeenCalled(); | ||
expect(mockSnackBar.open).toHaveBeenCalledWith( | ||
component.messages.projectFiscalCreatedSuccess, | ||
'OK', | ||
{ duration: 5000, panelClass: 'snackbar-success' } // ✅ Ensure correct snackbar message | ||
); | ||
expect(component.loadProjectFiscals).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle errors when saving a new fiscal', () => { | ||
mockProjectService.createProjectFiscal.and.returnValue(throwError(() => new Error('API Error'))); | ||
component.onSaveFiscal(0); | ||
expect(mockSnackBar.open).toHaveBeenCalledWith(component.messages.projectFiscalCreatedFailure, 'OK', { | ||
duration: 5000, | ||
panelClass: 'snackbar-error', | ||
}); | ||
}); | ||
|
||
it('should update an existing fiscal', () => { | ||
spyOn(component, 'loadProjectFiscals'); | ||
|
||
// ✅ Ensure updateProjectFiscal returns success | ||
mockProjectService.updateProjectFiscal.and.returnValue(of({})); | ||
|
||
component.projectFiscals = [{ projectPlanFiscalGuid: 'existing-guid' }]; // ✅ Ensure a valid fiscal object exists | ||
|
||
component.onSaveFiscal(0); | ||
|
||
expect(mockProjectService.updateProjectFiscal).toHaveBeenCalled(); | ||
expect(mockSnackBar.open).toHaveBeenCalledWith( | ||
component.messages.projectFiscalUpdatedSuccess, | ||
'OK', | ||
{ duration: 5000, panelClass: 'snackbar-success' } // ✅ Ensure correct success message | ||
); | ||
expect(component.loadProjectFiscals).toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it('should handle errors when updating an existing fiscal', () => { | ||
// ✅ Ensure projectFiscals is initialized before setting properties | ||
component.projectFiscals = [{ projectPlanFiscalGuid: 'existing-guid' }]; | ||
|
||
mockProjectService.updateProjectFiscal.and.returnValue(throwError(() => new Error('API Error'))); | ||
|
||
component.onSaveFiscal(0); | ||
|
||
expect(mockSnackBar.open).toHaveBeenCalledWith( | ||
component.messages.projectFiscalUpdatedFailure, | ||
'OK', | ||
{ duration: 5000, panelClass: 'snackbar-error' } // ✅ Ensure correct error message is tested | ||
); | ||
}); | ||
|
||
}); |
Oops, something went wrong.