test cases for ngOnInit method in sidebar comp 28/104628/3
authorIndrijeet kumar <indriku1@in.ibm.com>
Sat, 28 Mar 2020 20:04:48 +0000 (01:34 +0530)
committerIndrijeet Kumar <indriku1@in.ibm.com>
Wed, 1 Apr 2020 15:02:34 +0000 (15:02 +0000)
test cases for ngOnInit method in sidebar component

Issue-ID: PORTAL-813
Change-Id: I4b0d6997a12fc81cdfa84c84a237ef19b53bfc1d
Signed-off-by: Indrijeet Kumar <indriku1@in.ibm.com>
ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/layout/components/sidebar/sidebar.component.spec.ts

index cc710b5..7f5a43f 100644 (file)
@@ -4,7 +4,7 @@
  * ===================================================================
  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
  * 
- * Modification Copyright © 2019 IBM.
+ * Modification Copyright © 2020 IBM.
  * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
 import { RouterTestingModule } from '@angular/router/testing';
 import { TranslateModule } from '@ngx-translate/core';
+import { HttpClientModule } from '@angular/common/http'; 
 import { SidebarComponent } from './sidebar.component';
 import { LayoutModule } from '../../layout.module';
-import { Session } from 'protractor';
 import { CookieService } from 'ngx-cookie-service';
 import { SidebarService } from 'src/app/shared/services';
-import { ReplaySubject } from 'rxjs';
 import 'rxjs/add/observable/of';
-import { Observable } from 'rxjs/Observable';
-import { BaseRequestOptions, Http, XHRBackend } from '@angular/http';
-import { MockBackend } from '@angular/http/testing';
-import { environment } from 'src/environments/environment';
-import { inject } from '@angular/core';
-import { Response } from '@angular/http';
 import { HttpClientTestingModule } from '@angular/common/http/testing';
+import { Observable } from 'rxjs';
 
 describe('SidebarComponent', () => {
   let component: SidebarComponent;
   let fixture: ComponentFixture<SidebarComponent>;
   let sidebarService: SidebarService;
-  let spy:any;
-  
-  let mockSidebarService = {
-    navigate: jasmine.createSpy('navigate')
-  };
+
+  var stubData1={
+    "data":'{"data":"cachedRegions"}',
+    "data2":'{"data2":"cachedRegions2"}'
+     };
+
 
   beforeEach(async(() => {
+    let sidebarService: SidebarService;
     TestBed.configureTestingModule({
       imports: [
         LayoutModule,
         RouterTestingModule,
         TranslateModule.forRoot(),
+        HttpClientModule,
         HttpClientTestingModule
       ],
-      providers:[CookieService, SidebarService,MockBackend, BaseRequestOptions, {
-        provide: Http,
-                    useFactory: (backend: XHRBackend, defaultOptions: BaseRequestOptions) => {
-                        return new Http(backend, defaultOptions);
-                    },
-                    deps: [MockBackend, BaseRequestOptions],                    
-      }],
+      providers:[CookieService, SidebarService],
     })
     .compileComponents();
   }));
@@ -89,22 +80,25 @@ describe('SidebarComponent', () => {
     component = fixture.componentInstance;
     fixture.detectChanges();
     sidebarService = TestBed.get(SidebarService);
+    
   });
 
   it('should create', () => {
     expect(component).toBeTruthy();
   });
 
+  it('testing if condition in ngOnInit method',()=>{
+    component.cookieService.set('show_app_header','false');
+    component.ngOnInit();
+    expect(component.showHeader).toBe(false)
+  })
+
   describe('should test ngOnInit',()=>{
-    it('should validate on ngOnInit',(done)=>{
-     var result=sidebarService.getLeftMenu()
-     spy=spyOn(sidebarService, 'getLeftMenu').and.returnValue(result);  
-     fixture.detectChanges(); 
-     sidebarService.getLeftMenu(); 
-     expect(spy).toHaveBeenCalled();
-     component.ngOnInit(); 
-     done()
-    //expect(component.result).toEqual(result)
+    it('should validate on ngOnInit',()=>{
+     let spy=spyOn(sidebarService,'getLeftMenu').and.returnValue(Observable.of(stubData1));
+     component.ngOnInit();
+     expect(spy).toHaveBeenCalled();  
     })
   })
 
@@ -134,15 +128,15 @@ describe('SidebarComponent', () => {
 
   it('should test isToggled method',()=>{
     expect(component.isToggled()).toBe(false);  
-  });
+  })
 
   it('should test toggleSidebar method',()=>{
     component.pushRightClass="kumar";
     expect(component.toggleSidebar()).toBeUndefined;
-  });
+  })
 
   it('should test on onLoggedout method',()=>{
      expect(localStorage.getItem('isLoggedin')).toBeFalsy
-    });
+    })
 
 });