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 { NavbarComponent } from './navbar.component'; import { SharedService } from '../shared.service'; import { CartService } from '../../services/cart.service'; import { UserService } from '../../services/user.service'; 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, UpdateNotificationResponse, logoutResponse, loginResponse, userMockData, profileResponse, retailAddressData, oktaConfig } from '../mock-data'; import { PopupComponent } from '../popup/popup.component'; import { FixedPluginComponent } from '../fixedplugin/fixedplugin.component'; import * as Constants from '../../shared/constants'; import { OrderHistoryComponent } from '../../home/customer/customer-detail/order-history/order-history.component'; import { OrderNotesComponent } from '../ordernotes/order-notes.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'; declare var $: any; // import {Conne} describe('Nav Bar Component', () => { let navbarObject: NavbarComponent; let fixture: ComponentFixture; let sharedService: SharedService; let dataStub: UserServiceStub; let spyNavigate: any; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, UserService, CartService, CustomerService, CheckoutService, LookupService, CatalogService, TradeinService, LoggingService, HttpClient, HttpHandler, OktaAuthService ], imports: [ FormsModule, RouterTestingModule, OktaAuthModule.initAuth(oktaConfig) ], declarations: [NavbarComponent, OrderHistoryComponent, OrderNotesComponent, PopupComponent, FixedPluginComponent] }); fixture = TestBed.createComponent(NavbarComponent); navbarObject = fixture.componentInstance; navbarObject['user'] = userMockData; navbarObject.userService.user = navbarObject['user']; dataStub = fixture.debugElement.injector.get(UserService); TestBed.compileComponents(); spyNavigate = spyOn(navbarObject.router, 'navigate'); $('script')[0].src = 'walkme'; // fake assignment to bypass the walkmefunction call. })); // tests here it('should be created', () => { expect(navbarObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(navbarObject.getTitle).toBeDefined(); })); it('should resolve getNotification test data', async(() => { const spy = spyOn(dataStub, 'getNotifications').and.returnValue( Observable.of(notificationResponse) ); navbarObject.getNotifications(Constants.STORE_ID_CD, 30169); fixture.detectChanges(); expect(navbarObject.karmaTestResult).toEqual(notificationResponse); })); it('should resolve updateNotification test data', async(() => { const spy = spyOn(dataStub, 'updateNotifications').and.returnValue( Observable.of(UpdateNotificationResponse) ); const notificationItem = { id: 12345, title: 'channel status', isRead: false, notification: 'string', isCollapsed: true } navbarObject.updateNotification(notificationItem); fixture.detectChanges(); expect(navbarObject.karmaTestResult).toEqual(UpdateNotificationResponse); })); it('should resolve logout test data', async(() => { const spy = spyOn(dataStub, 'logout').and.returnValue( Observable.of(logoutResponse) ); navbarObject.userService.setUser(navbarObject['user']); fixture.detectChanges(); navbarObject.logout(); fixture.detectChanges(); expect(navbarObject.karmaTestResult).toEqual(logoutResponse); expect(spyNavigate).toHaveBeenCalledWith(['mylogin']); })); it('toggling between switch channel is working fine', async(() => { navbarObject.swithChannel(Constants.STORE_ID_CD); spyOn(navbarObject.sharedService, 'showErrorMessage'); })); it('should resolve profile test data', fakeAsync(() => { const userStub = fixture.debugElement.injector.get(UserService); const spy = spyOn(userStub, 'login').and.returnValue( Observable.of(loginResponse) ); navbarObject.storeId = 10553; navbarObject.reLogin(10553); fixture.detectChanges(); tick(500); expect(navbarObject.karmaTestResult).toEqual(loginResponse); })); it('should resolve refreshSalesStats test data', async(() => { const userStub = fixture.debugElement.injector.get(UserService); const spy = spyOn(dataStub, 'getProfile').and.returnValue( Observable.of(profileResponse) ); navbarObject.refreshSalesStats(true); fixture.detectChanges(); expect(navbarObject.karmaTestResult).toEqual(profileResponse); })); it('should resolve changeSettingStatus test data', async(() => { const status = true; navbarObject.changeSettingStatus(status) expect(navbarObject.showSettings).toEqual(status); })); it('should resolve checkForChannels test data', fakeAsync(() => { navbarObject['user'] = userMockData; navbarObject.checkForChannels(10553); expect(navbarObject.errorMessage).toEqual('Not authorized for any channel.'); })); it('formatting phone number is working fine', async(() => { expect(navbarObject.mobileNumber('1234567890')).toEqual('123-456-7890'); })); it('should resolve updated store details test data', fakeAsync(() => { const userStub = fixture.debugElement.injector.get(UserService); const spy = spyOn(userStub, 'getRetailStoreDetail').and.returnValue( Observable.of(retailAddressData) ); navbarObject.storeId = 10553; navbarObject.updateRetailStoreDetail(10553); fixture.detectChanges(); tick(500); expect(navbarObject.karmaTestResult).toEqual(retailAddressData); })); it('should resolve getNotificationsReadCounts test data', fakeAsync(() => { navbarObject.unReadNotificationCount = 0; navbarObject['user'] = userMockData; navbarObject.getNotificationsReadCounts(); expect(navbarObject.user.notifications[0].isCollapsed ).toEqual(true); })); });