-
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.
Wfprev 242 , Fix the snackbar, add more unit tests to pass sonar gate (…
- Loading branch information
Showing
9 changed files
with
667 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,7 +159,7 @@ describe('CreateNewProjectDialogComponent', () => { | |
expect(mockSnackbarService.open).toHaveBeenCalledWith( | ||
Messages.projectCreatedSuccess, | ||
'OK', | ||
{ duration: 100000, panelClass: 'snackbar-success' } | ||
{ duration: 5000, panelClass: 'snackbar-success' } | ||
); // Ensure snackbar was called | ||
expect(mockDialogRef.close).toHaveBeenCalledWith({ success: true }); // Ensure the dialog was closed | ||
}); | ||
|
@@ -321,6 +321,148 @@ describe('CreateNewProjectDialogComponent', () => { | |
}) | ||
); | ||
}); | ||
|
||
it('should show an error when latLong is in an invalid format', () => { | ||
component.projectForm.patchValue({ | ||
projectName: 'New Project', | ||
businessArea: 'Area 1', | ||
forestRegion: 1, | ||
forestDistrict: 2, | ||
bcParksRegion: 3, | ||
bcParksSection: 4, | ||
projectLead: 'John Doe', | ||
projectLeadEmail: '[email protected]', | ||
siteUnitName: 'Unit 1', | ||
closestCommunity: 'Community 1', | ||
latLong: 'invalid, format', // Invalid latLong format | ||
}); | ||
|
||
component.onCreate(); | ||
|
||
expect(mockSnackbarService.open).toHaveBeenCalledWith( | ||
'Invalid latitude and longitude. Please ensure it is in the correct format and within BC boundaries.', | ||
'OK', | ||
{ duration: 5000, panelClass: 'snackbar-error' } | ||
); | ||
|
||
expect(mockProjectService.createProject).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should disable bcParksSection if region is deselected', () => { | ||
component.projectForm.get('bcParksRegion')?.setValue(null); // No region selected | ||
fixture.detectChanges(); | ||
|
||
expect(component.projectForm.get('bcParksSection')?.disabled).toBeTrue(); | ||
expect(component.bcParksSections).toEqual([]); | ||
}); | ||
|
||
it('should handle error while fetching code tables', () => { | ||
mockCodeTableService.fetchCodeTable.and.returnValue(throwError(() => new Error('Network error'))); | ||
|
||
component.loadCodeTables(); | ||
|
||
expect(mockCodeTableService.fetchCodeTable).toHaveBeenCalledWith('programAreaCodes'); | ||
expect(mockCodeTableService.fetchCodeTable).toHaveBeenCalledWith('forestRegionCodes'); | ||
// Add assertions for console.error or fallback logic | ||
}); | ||
|
||
it('should close dialog on cancel confirmation', () => { | ||
const mockAfterClosed = of(true); // User confirmed | ||
mockDialog.open.and.returnValue({ afterClosed: () => mockAfterClosed } as any); | ||
|
||
component.onCancel(); | ||
|
||
|
||
mockAfterClosed.subscribe(() => { | ||
expect(mockDialogRef.close).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should create project with only required fields', () => { | ||
component.projectForm.patchValue({ | ||
projectName: 'Required Project', | ||
businessArea: 'Area 1', | ||
forestRegion: 1, | ||
forestDistrict: 2, | ||
bcParksRegion: 3, | ||
bcParksSection: 4, | ||
closestCommunity: 'Community 1', | ||
}); | ||
|
||
mockProjectService.createProject.and.returnValue(of({})); | ||
|
||
component.onCreate(); | ||
|
||
expect(mockProjectService.createProject).toHaveBeenCalledWith( | ||
jasmine.objectContaining({ | ||
projectName: 'Required Project', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return null if there are no errors', () => { | ||
component.projectForm.get('projectName')?.setErrors(null); | ||
|
||
const errorMessage = component.getErrorMessage('projectName'); | ||
|
||
expect(errorMessage).toBeNull(); | ||
}); | ||
|
||
it('should return required field message when "required" error exists', () => { | ||
component.projectForm.get('projectName')?.setErrors({ required: true }); | ||
|
||
const errorMessage = component.getErrorMessage('projectName'); | ||
|
||
expect(errorMessage).toBe(Messages.requiredField); | ||
}); | ||
|
||
it('should return max length exceeded message when "maxlength" error exists', () => { | ||
component.projectForm.get('projectName')?.setErrors({ maxlength: true }); | ||
|
||
const errorMessage = component.getErrorMessage('projectName'); | ||
|
||
expect(errorMessage).toBe(Messages.maxLengthExceeded); | ||
}); | ||
|
||
it('should return invalid email message when "email" error exists', () => { | ||
// Arrange: Set 'email' error on the control | ||
component.projectForm.get('projectLeadEmail')?.setErrors({ email: true }); | ||
|
||
// Act: Call the getErrorMessage method | ||
const errorMessage = component.getErrorMessage('projectLeadEmail'); | ||
|
||
// Assert: Expect the invalid email error message | ||
expect(errorMessage).toBe(Messages.invalidEmail); | ||
}); | ||
|
||
it('should show an error snackbar if creating a project fails', () => { | ||
// Arrange: Simulate the createProject API call failing | ||
mockProjectService.createProject.and.returnValue(throwError(() => new Error('Failed to create project'))); | ||
|
||
// Populate the form with valid values | ||
component.projectForm.patchValue({ | ||
projectName: 'New Project', | ||
businessArea: 'Area 1', | ||
forestRegion: 1, | ||
forestDistrict: 2, | ||
bcParksRegion: 3, | ||
bcParksSection: 4, | ||
projectLead: 'John Doe', | ||
projectLeadEmail: '[email protected]', | ||
siteUnitName: 'Unit 1', | ||
closestCommunity: 'Community 1', | ||
}); | ||
|
||
// Act: Call the method to create a project | ||
component.onCreate(); | ||
|
||
// Assert | ||
expect(mockProjectService.createProject).toHaveBeenCalled(); // Ensure createProject was called | ||
expect(mockSnackbarService.open).toHaveBeenCalledWith( | ||
component.messages.projectCreatedFailure, | ||
'OK', | ||
{ duration: 5000, panelClass: 'snackbar-error' } | ||
); // Ensure the error snackbar was displayed | ||
}); | ||
|
||
|
||
}); |
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
Oops, something went wrong.