-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from aranaravi/develop
[MOSIP-29327]Changes related to unit testing
- Loading branch information
Showing
9 changed files
with
404 additions
and
469 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
70 changes: 60 additions & 10 deletions
70
resident-ui/src/app/shared/dialog/dialog.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,25 +1,75 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog'; | ||
import { Router } from '@angular/router'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { of } from 'rxjs'; | ||
import { DialogComponent } from './dialog.component'; | ||
|
||
describe('DialogComponent', () => { | ||
let component: DialogComponent; | ||
let fixture: ComponentFixture<DialogComponent>; | ||
let mockMatDialogRef: MatDialogRef<DialogComponent>; | ||
let mockMatDialog: MatDialog; | ||
let mockRouter: Router; | ||
let mockTranslateService: TranslateService; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ DialogComponent ] | ||
}) | ||
.compileComponents(); | ||
})); | ||
const mockDialogData = { | ||
title: 'Test', | ||
// Add any other properties you need for testing here | ||
}; | ||
|
||
beforeEach(() => { | ||
mockMatDialogRef = jasmine.createSpyObj(['close']); | ||
mockMatDialog = jasmine.createSpyObj(['closeAll']); | ||
mockRouter = jasmine.createSpyObj(['navigateByUrl']); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [DialogComponent], | ||
providers: [ | ||
{ provide: MatDialogRef, useValue: mockMatDialogRef }, | ||
{ provide: MAT_DIALOG_DATA, useValue: mockDialogData }, | ||
{ provide: MatDialog, useValue: mockMatDialog }, | ||
{ provide: Router, useValue: mockRouter }, | ||
{ provide: TranslateService, useValue: mockTranslateService }, | ||
], | ||
}); | ||
|
||
fixture = TestBed.createComponent(DialogComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
it('should create the DialogComponent', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should close the dialog when calling onNoClick', () => { | ||
component.onNoClick(); | ||
expect(mockMatDialogRef.close).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should navigate to the specified URL when calling viewDetails', () => { | ||
const eventId = '12345'; | ||
component.viewDetails(eventId); | ||
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith(`uinservices/trackservicerequest?eid=${eventId}`); | ||
}); | ||
|
||
it('should toggle isChecked property when calling agreeConditions', () => { | ||
const initialCheckedState = component.isChecked; | ||
component.agreeConditions(); | ||
expect(component.isChecked).toBe(!initialCheckedState); | ||
}); | ||
|
||
it('should set otpTimeMinutes and disableInput properties when calling setOtpTime', () => { | ||
component.appConfigService = { | ||
getConfig: () => ({ 'mosip.kernel.otp.expiry-time': 120 }), | ||
} as any; | ||
|
||
component.setOtpTime(); | ||
|
||
expect(component.otpTimeMinutes).toBe(2); | ||
expect(component.disableInput).toBe(false); | ||
}); | ||
|
||
// Add more test cases as needed for other methods and properties | ||
|
||
}); |
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
102 changes: 102 additions & 0 deletions
102
resident-ui/src/app/shared/header/header.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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { HeaderComponent } from './header.component'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { DataStorageService } from 'src/app/core/services/data-storage.service'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
import { LogoutService } from './../../core/services/logout.service'; | ||
import { HeaderService } from 'src/app/core/services/header.service'; | ||
import { AuditService } from 'src/app/core/services/audit.service'; | ||
import { MatDialog } from '@angular/material'; | ||
import { InteractionService } from 'src/app/core/services/interaction.service'; | ||
import { of } from 'rxjs'; | ||
|
||
describe('HeaderComponent', () => { | ||
let component: HeaderComponent; | ||
let fixture: ComponentFixture<HeaderComponent>; | ||
|
||
// Mock services and dependencies | ||
const translateServiceMock = { | ||
use: () => {}, | ||
getTranslation: () => of({}), | ||
}; | ||
|
||
const dataStorageServiceMock = { | ||
getProfileInfo: () => of({}), | ||
getNotificationCount: () => of({}), | ||
getNotificationData: () => of({}), | ||
updateNotificationTime: () => of({}), | ||
}; | ||
|
||
const sanitizerMock = { | ||
bypassSecurityTrustResourceUrl: (url: string) => url, | ||
}; | ||
|
||
const logoutServiceMock = { | ||
logout: () => {}, | ||
}; | ||
|
||
const headerServiceMock = { | ||
setUsername: () => {}, | ||
}; | ||
|
||
const auditServiceMock = { | ||
audit: () => {}, | ||
}; | ||
|
||
const dialogMock = { | ||
open: () => ({ afterClosed: () => of({}) }), | ||
}; | ||
|
||
const interactionServiceMock = { | ||
getClickEvent: () => of('logOutBtn'), | ||
}; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [HeaderComponent], | ||
imports: [RouterTestingModule], | ||
providers: [ | ||
{ provide: TranslateService, useValue: translateServiceMock }, | ||
{ provide: DataStorageService, useValue: dataStorageServiceMock }, | ||
{ provide: DomSanitizer, useValue: sanitizerMock }, | ||
{ provide: LogoutService, useValue: logoutServiceMock }, | ||
{ provide: HeaderService, useValue: headerServiceMock }, | ||
{ provide: AuditService, useValue: auditServiceMock }, | ||
{ provide: MatDialog, useValue: dialogMock }, | ||
{ provide: InteractionService, useValue: interactionServiceMock }, | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(HeaderComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create the component', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should call onScroll when scrolling down', () => { | ||
spyOn(component, 'onScroll'); | ||
window.dispatchEvent(new Event('scroll')); | ||
expect(component.onScroll).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onScrollUp when scrolling up', () => { | ||
spyOn(component, 'onScrollUp'); | ||
window.dispatchEvent(new Event('scroll')); | ||
expect(component.onScrollUp).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call doLogout when logOutBtn is clicked', () => { | ||
spyOn(logoutServiceMock, 'logout'); | ||
component.ngOnInit(); // Simulate ngOnInit to subscribe to the clickEventObservable | ||
component.clickEventSubscription.next('logOutBtn'); | ||
expect(logoutServiceMock.logout).toHaveBeenCalled(); | ||
}); | ||
|
||
// Add more test cases as needed for other methods and scenarios | ||
}); |
68 changes: 68 additions & 0 deletions
68
resident-ui/src/app/shared/pdf-viewer/pdf-viewer.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { PDFViewerComponent } from './pdf-viewer.component'; | ||
import { EventEmitter } from '@angular/core'; | ||
|
||
describe('PDFViewerComponent', () => { | ||
let component: PDFViewerComponent; | ||
let fixture: ComponentFixture<PDFViewerComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [PDFViewerComponent], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(PDFViewerComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create the PDFViewerComponent', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should initialize with default values', () => { | ||
expect(component.pageNumber).toEqual(1); | ||
expect(component.zoom).toEqual(1.0); | ||
expect(component.bgColor).toEqual('rgba(0,0,0,0)'); | ||
}); | ||
|
||
it('should emit PdfDocumentLoad event after ngOnInit', async () => { | ||
spyOn(component.PdfDocumentLoad, 'emit'); | ||
component.pdfSrc = 'path/to/pdf.pdf'; | ||
await component.ngOnInit(); | ||
expect(component.PdfDocumentLoad.emit).toHaveBeenCalledWith({ | ||
numPages: component.getNumPages(), | ||
}); | ||
}); | ||
|
||
it('should handle ngOnDestroy correctly', () => { | ||
component._pdfDocument = {}; // Simulate that _pdfDocument exists | ||
spyOn(component._pdfDocument, 'destroy'); | ||
component.ngOnDestroy(); | ||
expect(component._pdfDocument.destroy).toHaveBeenCalled(); | ||
expect(component._pdfDocument).toBeNull(); | ||
}); | ||
|
||
it('should return true for isValidPageNumberRequest if page number is valid', () => { | ||
component._pdfDocument = { | ||
_pdfInfo: { | ||
numPages: 5, // Set the total number of pages | ||
}, | ||
}; | ||
expect(component.isValidPageNumberRequest(3)).toBe(true); | ||
}); | ||
|
||
it('should return false for isValidPageNumberRequest if page number is invalid', () => { | ||
component._pdfDocument = { | ||
_pdfInfo: { | ||
numPages: 5, // Set the total number of pages | ||
}, | ||
}; | ||
expect(component.isValidPageNumberRequest(0)).toBe(false); | ||
expect(component.isValidPageNumberRequest(6)).toBe(false); | ||
}); | ||
|
||
// Add more test cases as needed for other methods and scenarios | ||
}); |
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
43 changes: 43 additions & 0 deletions
43
resident-ui/src/app/shared/progress/progress.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ProgressComponent } from './progress.component'; | ||
|
||
describe('ProgressComponent', () => { | ||
let component: ProgressComponent; | ||
let fixture: ComponentFixture<ProgressComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ProgressComponent], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ProgressComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create the ProgressComponent', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should initialize with progress set to 0', () => { | ||
expect(component.progress).toBe(0); | ||
}); | ||
|
||
it('should display the progress value', () => { | ||
const element: HTMLElement = fixture.nativeElement; | ||
const progressElement = element.querySelector('.progress'); | ||
expect(progressElement.textContent).toContain('0%'); // Assuming you have an element with the class 'progress' to display the progress value | ||
}); | ||
|
||
it('should update the progress value when @Input is changed', () => { | ||
component.progress = 50; | ||
fixture.detectChanges(); | ||
const element: HTMLElement = fixture.nativeElement; | ||
const progressElement = element.querySelector('.progress'); | ||
expect(progressElement.textContent).toContain('50%'); | ||
}); | ||
|
||
// Add more test cases as needed for other scenarios | ||
}); |
30 changes: 27 additions & 3 deletions
30
resident-ui/src/app/shared/router/router-ext.service.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,12 +1,36 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { Router } from '@angular/router'; | ||
import { RouterExtService } from './router-ext.service'; | ||
|
||
describe('RouterExtService', () => { | ||
beforeEach(() => TestBed.configureTestingModule({})); | ||
let service: RouterExtService; | ||
let router: Router; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [RouterExtService, Router], | ||
}); | ||
service = TestBed.get(RouterExtService); | ||
router = TestBed.get(Router); | ||
}); | ||
|
||
it('should be created', () => { | ||
const service: RouterExtService = TestBed.get(RouterExtService); | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should initialize with undefined previous and current URLs', () => { | ||
expect(service.getPreviousUrl()).toBeUndefined(); | ||
}); | ||
|
||
it('should update previous and current URLs on navigation', () => { | ||
router.navigate(['/home']); // Navigate to a URL | ||
expect(service.getPreviousUrl()).toBeUndefined(); // The previous URL should still be undefined as there was no previous navigation within this test | ||
expect(service.getPreviousUrl()).toBe('/home'); // The current URL should be the one we navigated to | ||
|
||
router.navigate(['/about']); // Navigate to another URL | ||
expect(service.getPreviousUrl()).toBe('/home'); // Now, the previous URL should be '/home' | ||
expect(service.getPreviousUrl()).toBe('/about'); // The current URL should be the one we navigated to | ||
}); | ||
|
||
// Add more test cases as needed for other scenarios | ||
}); |