Skip to content

Commit

Permalink
Merge pull request #476 from aranaravi/develop
Browse files Browse the repository at this point in the history
[MOSIP-29327]Changes related to unit testing
  • Loading branch information
aranaravi authored Sep 7, 2023
2 parents 9ef9f0b + 7c1d829 commit 43cb940
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 469 deletions.
554 changes: 101 additions & 453 deletions resident-ui/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resident-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"osm": "^1.0.0",
"pdfjs-dist": "2.0.943",
"rxjs": "^6.0.0",
"scuri": "^1.4.0",
"smoothscroll-polyfill": "^0.4.4",
"url-safe-base64": "^1.2.0",
"vm": "^0.1.0",
Expand All @@ -74,6 +73,7 @@
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"ngx-spec": "^2.0.0",
"protractor": "~5.4.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1",
Expand Down
70 changes: 60 additions & 10 deletions resident-ui/src/app/shared/dialog/dialog.component.spec.ts
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

});
2 changes: 1 addition & 1 deletion resident-ui/src/app/shared/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class DialogComponent implements OnInit {
/*private headerService: HeaderService,*/
private logoutService: LogoutService,
private interactionService: InteractionService,
private appConfigService: AppConfigService,
public appConfigService: AppConfigService,
private redirectService: LoginRedirectService
) {
this.translate.use(this.primaryLangCode);
Expand Down
102 changes: 102 additions & 0 deletions resident-ui/src/app/shared/header/header.component.spec.ts
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 resident-ui/src/app/shared/pdf-viewer/pdf-viewer.component.spec.ts
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
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class PDFViewerComponent {
@Output()
PdfDocumentLoad = new EventEmitter<IPdfDocumentLoad>();

private _pdfDocument: any;
public _pdfDocument: any;

constructor() {
if (isSSR()) {
Expand Down
43 changes: 43 additions & 0 deletions resident-ui/src/app/shared/progress/progress.component.spec.ts
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 resident-ui/src/app/shared/router/router-ext.service.spec.ts
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
});

0 comments on commit 43cb940

Please sign in to comment.