import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute, Route } from '@angular/router'; import { Observable } from 'rxjs/Rx'; import { DebugElement } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { inject, tick, TestBed, getTestBed, async, fakeAsync, ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { SidebarComponent } from './sidebar.component'; import { SharedService } from '../shared.service'; import { CartService } from '../../services/cart.service'; import { UserService } from '../../services/user.service'; import { PopupComponent } from '../popup/popup.component'; import { CustomerService } from '../../services/customer.service'; import { LookupService } from '../../services/lookup.service'; import { CatalogService } from '../../services/catalog.service'; import { UserServiceStub } from '../../services/user.service.stub'; import { notificationResponse, logoutResponse, userMockData, cartData, oktaConfig } from '../mock-data'; import * as Constants from '../../shared/constants'; import { OrderNotesComponent } from '../ordernotes/order-notes.component'; import { OrderHistoryComponent } from '../../home/customer/customer-detail/order-history/order-history.component'; import { TradeinService } from '../../services/tradein.service'; import { LoggingService } from '../../services/logging.service'; import { CheckoutService } from '../../services/checkout.service'; import {HttpClient, HttpHandler} from '@angular/common/http'; import { OktaAuthService, OktaAuthModule } from '@okta/okta-angular'; // import {Conne} describe('Side Bar Component', () => { let obj: SidebarComponent; let fixture: ComponentFixture; let sharedService: SharedService; let dataStub: UserServiceStub; let spyNavigate: any; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, UserService, CartService, CustomerService, LookupService, CatalogService, CheckoutService, TradeinService, LoggingService, HttpClient, HttpHandler, OktaAuthService ], imports: [ FormsModule, RouterTestingModule, OktaAuthModule.initAuth(oktaConfig) ], declarations: [SidebarComponent, OrderNotesComponent, OrderHistoryComponent, PopupComponent] }); fixture = TestBed.createComponent(SidebarComponent); obj = fixture.componentInstance; dataStub = fixture.debugElement.injector.get(UserService); TestBed.compileComponents(); spyNavigate = spyOn(obj.router, 'navigate'); obj['user'] = userMockData; obj.cartService.cart = cartData; })); // tests here it('should be created', () => { expect(obj).toBeTruthy(); }); it('All methods are defined', async(() => { expect(obj.onResize).toBeDefined(); })); it('onChangeShowFullSidebar functionality', async(() => { obj.showFullSidebar = false; spyOn(obj.sharedService, 'showSidebar') obj.onChangeShowFullSidebar(); expect(obj.sharedService.showSidebar).toHaveBeenCalledWith(true); })); it('should resolve getNotification test data', async(() => { const spy = spyOn(dataStub, 'getNotifications').and.returnValue( Observable.of(notificationResponse) ); obj.getNotifications(Constants.STORE_ID_CD, 30169); fixture.detectChanges(); expect(obj.karmaTestResult).toEqual(notificationResponse); })); it('should resolve updateNotification test data', async(() => { const spy = spyOn(dataStub, 'updateNotifications').and.returnValue( Observable.of(notificationResponse) ); let notificationItem = { id: 12345, title: 'channel status', isRead: false, notification: 'string', isCollapsed: false } obj.updateNotification(notificationItem); fixture.detectChanges(); expect(obj.karmaTestResult).toEqual(notificationResponse); })); it('should resolve logout test data', async(() => { const spy = spyOn(dataStub, 'logout').and.returnValue( Observable.of(logoutResponse) ); obj.logout(); fixture.detectChanges(); expect(obj.karmaTestResult).toEqual(logoutResponse); expect(spyNavigate).toHaveBeenCalledWith(['login']); })); it('toggling between switch channel is working fine', async(() => { obj.cartService.setCart(null); spyOn(obj.sharedService, 'updateStoreId'); obj.swithChannel(10553); expect(obj.sharedService.updateStoreId).toHaveBeenCalledWith(10553); expect(spyNavigate).toHaveBeenCalledWith(['home/search']); })); it('scroll top functionality', async(() => { spyOn(obj.scrollToTop, 'emit') obj.callScrollTop(); expect(obj.scrollToTop.emit).toHaveBeenCalled(); })); it('display cart functionality', async(() => { obj.showMinicart = false; spyOn(obj.sharedService, 'showMinicart') obj.displayMinicart(); expect(obj.sharedService.showMinicart).toHaveBeenCalledWith(true); })); it('check if is Resize is working', async(() => { spyOn(obj, 'isNotMobileMenu') obj.onResize(event); expect(obj.isNotMobileMenu).toHaveBeenCalled(); })); it('modal popup functionality', async(() => { spyOn(obj.onTitleAction, 'emit'); obj.triggerModal('customer'); expect(obj.onTitleAction.emit).toHaveBeenCalledWith('customer'); })); it('check if is Not Mobile Menu', async(() => { obj.isNotMobileMenu(); expect(obj.showFullSidebar).toBeTruthy(); })); it('Should update active package', () => { obj.cart = cartData; obj.updateActivePackage(); expect(obj.showFeatureMenu).toBeFalsy(); }); it('Should update canShowMenuItem package', () => { obj.cart = cartData; const pathflow = '/home/search' obj.canShowMenuItem(pathflow); expect(obj.cart.isShopifyOrder).toEqual(true); }); });