import { Component, OnInit, OnDestroy, Pipe } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { DebugElement } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { Observable } from 'rxjs/Rx'; import { TestBed, async, fakeAsync, ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; import { LookupService } from '../../../services/lookup.service'; import { CatalogService } from '../../../services/catalog.service'; import { TradeinService } from '../../../services/tradein.service'; import { LoggingService } from '../../../services/logging.service'; import { CheckoutService } from '../../../services/checkout.service'; import { cartData, packagesData, customerSearchResponse, billingResponse, userMockData, ShippingData, dealerCodeStatus } from '../../../shared/mock-data'; import { BillingComponent } from './billing.component'; import { CustomerSearchStub } from '../../../services/customer.service.stub'; import { HttpClient, HttpHandler } from '@angular/common/http'; describe('billing Component', () => { let billingObject: BillingComponent; let fixture: ComponentFixture; let dataStub: CustomerSearchStub; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, UserService, CartService, CustomerService, LookupService, CatalogService, TradeinService, LoggingService, CheckoutService, HttpClient, HttpHandler ], imports: [ FormsModule, RouterTestingModule ], declarations: [BillingComponent] }); fixture = TestBed.createComponent(BillingComponent); TestBed.get(SharedService); TestBed.get(UserService); TestBed.get(CartService); billingObject = fixture.componentInstance; billingObject.storeId = 10553; billingObject.billingModel = billingResponse; billingObject.cart = cartData; billingObject.userService.user = userMockData; dataStub = fixture.debugElement.injector.get(CustomerService); })); it('should be created', () => { expect(billingObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(billingObject.validateAddress).toBeDefined(); expect(billingObject.validateDealerCode).toBeDefined(); expect(billingObject.updateBillingAddress).toBeDefined(); expect(billingObject.getShippingModes).toBeDefined(); })); it('should resolve validate Address test data', fakeAsync(() => { const inputData = { valid: true } const spy = spyOn(dataStub, 'validateAddress').and.returnValue( Observable.of(customerSearchResponse) ); billingObject.validateAddress(inputData); fixture.detectChanges(); expect(billingObject.karmaTestResult).toEqual(customerSearchResponse); })); it('shipping address is working fine', async(() => { const checkoutdata = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(checkoutdata, 'getUsableShippingInfo').and.returnValue( Observable.of(ShippingData) ); billingObject.getShippingModes('98210'); fixture.detectChanges(); expect(billingObject.karmaTestResult).toEqual(ShippingData); })); it('validate dealer code is working fine', async(() => { const elementData = { value: 'S0000001', valid: true } const userData = fixture.debugElement.injector.get(UserService); const spy = spyOn(userData, 'validateDealerCode').and.returnValue( Observable.of(dealerCodeStatus) ); billingObject.validateDealerCode(elementData); fixture.detectChanges(); expect(billingObject.karmaTestResult).toEqual(dealerCodeStatus); })); it('check whether email exists', async(() => { billingObject.billingModel.email1 = true; billingObject.isEmailExists(); expect(billingObject.isEmailAvailable).toBeTruthy(); })); });