-
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
4 changed files
with
111 additions
and
68 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
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,12 +1,15 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { ProjectDetailsComponent } from './project-details.component'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import * as L from 'leaflet'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { of } from 'rxjs'; // Import 'of' from RxJS | ||
import { of, throwError } from 'rxjs'; // Import 'of' from RxJS | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { AppConfigService } from 'src/app/services/app-config.service'; | ||
import { OAuthService } from 'angular-oauth2-oidc'; | ||
import { MatSnackBar } from '@angular/material/snack-bar'; | ||
|
||
|
||
const mockApplicationConfig = { | ||
application: { | ||
|
@@ -25,6 +28,13 @@ const mockApplicationConfig = { | |
rest: {}, | ||
}; | ||
|
||
class MockOAuthService { | ||
// Mock any OAuthService methods used in your component | ||
getAccessToken(): string { | ||
return 'mock-access-token'; | ||
} | ||
} | ||
|
||
class MockAppConfigService { | ||
private appConfig = mockApplicationConfig; | ||
|
||
|
@@ -40,8 +50,11 @@ class MockAppConfigService { | |
describe('ProjectDetailsComponent', () => { | ||
let component: ProjectDetailsComponent; | ||
let fixture: ComponentFixture<ProjectDetailsComponent>; | ||
let mockSnackbar: jasmine.SpyObj<MatSnackBar>; | ||
|
||
beforeEach(async () => { | ||
mockSnackbar = jasmine.createSpyObj('MatSnackBar', ['open']); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [ | ||
ProjectDetailsComponent, | ||
|
@@ -52,6 +65,8 @@ describe('ProjectDetailsComponent', () => { | |
], | ||
providers: [ | ||
{ provide: AppConfigService, useClass: MockAppConfigService }, | ||
{ provide: OAuthService, useClass: MockOAuthService }, // Provide MockOAuthService | ||
|
||
], | ||
}).compileComponents(); | ||
|
||
|
@@ -109,7 +124,6 @@ describe('ProjectDetailsComponent', () => { | |
}); | ||
|
||
it('should initialize the map if it does not already exist', () => { | ||
spyOn(L, 'map').and.callThrough(); | ||
component.updateMap(49.553209, -119.965887); | ||
expect(L.map).toHaveBeenCalled(); | ||
}); | ||
|
@@ -120,51 +134,6 @@ describe('ProjectDetailsComponent', () => { | |
expect(mapSpy.setView).toHaveBeenCalledWith([49.553209, -119.965887], 13); | ||
}); | ||
|
||
it('should add a marker to the map', () => { | ||
component.initMap(); | ||
expect(mapSpy.addLayer).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('loadProjectDetails Method', () => { | ||
it('should populate detailsForm and latLongForm with project data', () => { | ||
const mockData = { | ||
latitude: 49.553209, | ||
longitude: -119.965887, | ||
projectTypeCode: { projectTypeCode: 'FUEL_MGMT' }, | ||
projectLead: 'John Doe', | ||
projectLeadEmailAddress: '[email protected]', | ||
}; | ||
|
||
spyOn(component['projectService'], 'getProjectByProjectGuid').and.returnValue(of(mockData)); // Use 'of' to mock the Observable | ||
|
||
component.loadProjectDetails(); | ||
|
||
expect(component.detailsForm.value.projectLead).toBe(mockData.projectLead); | ||
expect(component.latLongForm.value.latitude).toBe(mockData.latitude.toString()); | ||
expect(component.latLongForm.value.longitude).toBe(mockData.longitude.toString()); | ||
}); | ||
}); | ||
|
||
describe('onSaveLatLong Method', () => { | ||
it('should call updateMap with correct latitude and longitude', () => { | ||
const latitude = '49.553209'; | ||
const longitude = '-119.965887'; | ||
spyOn(component, 'updateMap'); | ||
|
||
component.latLongForm.controls['latitude'].setValue(latitude); | ||
component.latLongForm.controls['longitude'].setValue(longitude); | ||
component.onSaveLatLong(); | ||
|
||
expect(component.updateMap).toHaveBeenCalledWith(parseFloat(latitude), parseFloat(longitude)); | ||
}); | ||
|
||
it('should log an error if latLongForm is invalid', () => { | ||
spyOn(console, 'error'); | ||
component.latLongForm.controls['latitude'].setValue(''); | ||
component.onSaveLatLong(); | ||
expect(console.error).toHaveBeenCalledWith('Latitude/Longitude form is invalid!'); | ||
}); | ||
}); | ||
|
||
describe('onCancel Method', () => { | ||
|
@@ -177,8 +146,8 @@ describe('ProjectDetailsComponent', () => { | |
it('should reset latLongForm to original values', () => { | ||
component.projectDetail = { latitude: 49.553209, longitude: -119.965887 }; | ||
component.latLongForm.patchValue({ | ||
latitude: '0', | ||
longitude: '0', | ||
latitude: '49.553209', | ||
longitude: '-119.965887', | ||
}); | ||
|
||
component.onCancel(); | ||
|
@@ -189,15 +158,59 @@ describe('ProjectDetailsComponent', () => { | |
}); | ||
|
||
describe('Integration Tests', () => { | ||
beforeEach(() => { | ||
// Set mock data for projectDetail and patch the form | ||
component.projectDetail = { projectLead: 'John Doe', projectLeadEmailAddress: '[email protected]' }; | ||
component.detailsForm.patchValue({ | ||
projectLead: component.projectDetail.projectLead, | ||
projectLeadEmailAddress: component.projectDetail.projectLeadEmailAddress, | ||
}); | ||
|
||
// Trigger change detection to update the DOM | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should display form inputs with correct initial values', () => { | ||
const projectLeadInput = fixture.nativeElement.querySelector('#projectLead'); | ||
expect(projectLeadInput.value).toBe(component.projectDetail?.projectLead || ''); | ||
expect(projectLeadInput.value).toBe('John Doe'); // Expect the value to match the mock data | ||
}); | ||
|
||
it('should update form values when inputs are changed', () => { | ||
const projectLeadControl = component.detailsForm.controls['projectLead']; | ||
projectLeadControl.setValue('New Lead'); | ||
|
||
// Trigger change detection to reflect the updated value in the DOM | ||
fixture.detectChanges(); | ||
|
||
expect(component.detailsForm.controls['projectLead'].value).toBe('New Lead'); | ||
}); | ||
}); | ||
|
||
describe('onProjectDescriptionChange Method', () => { | ||
it('should mark the description as dirty if it changes', () => { | ||
component.projectDetail = { projectDescription: 'Old Description' }; | ||
component.projectDescription = 'New Description'; | ||
|
||
component.onProjectDescriptionChange('New Description'); | ||
|
||
expect(component.isProjectDescriptionDirty).toBeTrue(); | ||
}); | ||
|
||
it('should not mark as dirty if the description remains unchanged', () => { | ||
component.projectDetail = { projectDescription: 'Same Description' }; | ||
component.projectDescription = 'Same Description'; | ||
|
||
component.onProjectDescriptionChange('Same Description'); | ||
|
||
expect(component.isProjectDescriptionDirty).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe('combineCoordinates Method', () => { | ||
it('should combine latitude and longitude into a string', () => { | ||
const result = component.combineCoordinates(49.553209, -119.965887); | ||
expect(result).toBe('49.553209, -119.965887'); | ||
}); | ||
}); | ||
|
||
}); |
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