Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more unit tests #416

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,42 @@ describe('ProjectDetailsComponent', () => {

describe('Map Initialization', () => {
let mapSpy: jasmine.SpyObj<L.Map>;

let markerSpy: jasmine.SpyObj<L.Marker>;

beforeEach(() => {
mapSpy = jasmine.createSpyObj('L.Map', ['setView', 'addLayer', 'remove', 'invalidateSize','fitBounds']);
mapSpy = jasmine.createSpyObj('L.Map', ['setView', 'addLayer', 'remove', 'invalidateSize', 'fitBounds', 'removeLayer']);
markerSpy = jasmine.createSpyObj('L.Marker', ['addTo']);
spyOn(L, 'map').and.returnValue(mapSpy);
spyOn(L, 'marker').and.returnValue(markerSpy);
});

it('should initialize the map when updateMap is called without initializing the map', () => {
component['map'] = undefined;
component.updateMap(49.553209, -119.965887);

expect(L.map).toHaveBeenCalled();
expect(L.marker).toHaveBeenCalledWith([49.553209, -119.965887]);
expect(markerSpy.addTo).toHaveBeenCalledWith(mapSpy);
});

it('should not reinitialize the map if initMap is called and map already exists', () => {
component['map'] = mapSpy;
component.initMap();

expect(L.map).not.toHaveBeenCalled();
});

it('should not reinitialize the map if it already exists', () => {
component['map'] = mapSpy;
component.ngAfterViewInit();
expect(L.map).toHaveBeenCalledTimes(0);
});

it('should initialize the map if it does not already exist', () => {
component.updateMap(49.553209, -119.965887);
expect(L.map).toHaveBeenCalled();
});

it('should initialize map with default BC bounds if map is not defined', () => {
component.initMap();
expect(L.map).toHaveBeenCalled();
Expand All @@ -144,21 +163,68 @@ describe('ProjectDetailsComponent', () => {
]);
});


it('should initialize the map when initMap is called and map does not exist', () => {
component['map'] = undefined; // Ensure map is not already initialized
component.initMap();

expect(L.map).toHaveBeenCalled(); // Verify that the map was created
expect(mapSpy.fitBounds).toHaveBeenCalledWith([
[48.3, -139.1], // Southwest corner of BC
[60.0, -114.0], // Northeast corner of BC
]); // Verify that fitBounds was called with default bounds
});

it('should update the map view with the new latitude and longitude', () => {
component['map'] = mapSpy;
component.updateMap(49.553209, -119.965887);
expect(mapSpy.setView).toHaveBeenCalledWith([49.553209, -119.965887], 13);
});

it('should add a marker when updating the map view', () => {
component['map'] = mapSpy;
component.updateMap(49.553209, -119.965887);
expect(mapSpy.addLayer).toHaveBeenCalled();
expect(L.marker).toHaveBeenCalledWith([49.553209, -119.965887]);
expect(markerSpy.addTo).toHaveBeenCalledWith(mapSpy);
});

it('should remove the existing marker when updating the map', () => {
component['map'] = mapSpy;
component['marker'] = markerSpy;

component.updateMap(49.553209, -119.965887);

expect(mapSpy.removeLayer).toHaveBeenCalledWith(markerSpy); // Ensure the old marker is removed
expect(L.marker).toHaveBeenCalledWith([49.553209, -119.965887]); // New marker added
expect(markerSpy.addTo).toHaveBeenCalledWith(mapSpy); // New marker added to the map
});


it('should initialize the map and add a marker when coordinates are provided', () => {
component['map'] = undefined; // Ensure the map is not already initialized

component.updateMap(49.553209, -119.965887);

expect(L.map).toHaveBeenCalled(); // Verify that the map is created
expect(L.marker).toHaveBeenCalledWith([49.553209, -119.965887]); // Marker created
expect(markerSpy.addTo).toHaveBeenCalledWith(mapSpy); // Marker added to the map
});

it('should clean up the map on component destroy', () => {
component['map'] = mapSpy; // Assign the mock map to the component
component.ngOnDestroy(); // Trigger the lifecycle hook

expect(mapSpy.remove).toHaveBeenCalled(); // Ensure the map was removed
});

it('should do nothing when ngOnDestroy is called if map is not initialized', () => {
component['map'] = undefined; // Ensure the map is not initialized
component.ngOnDestroy(); // Trigger the lifecycle hook

// No errors should occur, and no calls should be made
expect(mapSpy.remove).not.toHaveBeenCalled();
});

});

describe('onCancel Method', () => {
it('should reset the form', () => {
spyOn(component.detailsForm, 'reset');
Expand Down Expand Up @@ -274,7 +340,14 @@ describe('ProjectDetailsComponent', () => {
expect(projectServiceSpy.getProjectByProjectGuid).toHaveBeenCalledWith('test-guid');
});

it('should not call getProjectByProjectGuid if projectGuid is missing', () => {
component.projectGuid = '';
component.loadProjectDetails();

expect(mockProjectService.getProjectByProjectGuid).not.toHaveBeenCalled();
});


it('should handle successful response and update component state', () => {
const mockResponse = {
projectName: 'Test Project',
Expand Down Expand Up @@ -518,5 +591,12 @@ describe('ProjectDetailsComponent', () => {
});
});

it('should not call updateProject if detailsForm is invalid', () => {
component.detailsForm.controls['projectTypeCode'].setValue('');
component.onSave();

expect(mockProjectService.updateProject).not.toHaveBeenCalled();
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import {
validateLatLong,
formatLatLong,
} from 'src/app/utils/tools';
import { OnDestroy } from '@angular/core';

@Component({
selector: 'app-project-details',
standalone: true,
imports: [ReactiveFormsModule,MatExpansionModule,CommonModule,FormsModule],
templateUrl: './project-details.component.html',
styleUrl: './project-details.component.scss'
})
export class ProjectDetailsComponent implements OnInit, AfterViewInit{
export class ProjectDetailsComponent implements OnInit, AfterViewInit, OnDestroy{
@Output() projectNameChange = new EventEmitter<string>();

private map: L.Map | undefined;
Expand Down Expand Up @@ -61,6 +63,12 @@ export class ProjectDetailsComponent implements OnInit, AfterViewInit{
this.loadProjectDetails();
}

ngOnDestroy(): void {
if (this.map) {
this.map.remove(); // Clean up the map
}
}

private initializeForm(): void {
this.detailsForm = this.fb.group({
projectTypeCode: ['', [Validators.required]],
Expand Down
Loading