import { Component, Input, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import * as Constants from '../../shared/constants' import { IPackage, IShipping, IUsableShipping, IShippingMode } from '../../shared/modal/common'; import { SharedService } from '../../shared/shared.service'; import { CartService } from '../../services/cart.service'; import { CustomerService } from '../../services/customer.service'; import { CheckoutService } from '../../services/checkout.service'; import { DebugElement } from '@angular/core'; import { FormsModule, FormBuilder } from '@angular/forms'; import { Observable } from 'rxjs/Rx'; import { TestBed, async, fakeAsync, ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { CustomerInfoComponent } from './customer-info.component'; import { CartServiceStub } from '../../services/cart.service.stub'; import { BillingComponent } from './billing/billing.component'; import { ShippingComponent } from './shipping/shipping.component'; import { AdditionalsComponent } from './additionals/additionals.component'; import { PPUAddressComponent } from './additionals/ppu-address/ppu-address.component'; import { NumberPortingComponent } from './additionals/number-porting/number-porting.component'; import { E911AddressComponent } from './additionals/e911-address/e911-address.component'; import { UserService } from '../../services/user.service'; import { LookupService } from '../../services/lookup.service'; import { CatalogService } from '../../services/catalog.service'; import { NpaNxxComponent } from './additionals/npa-nxx/npa-nxx.component'; import { cartData, userMockData, customerSearchResponse, shippingData, ShippingData, cartDataResponse, CheckoutResponse, dealerCodeStatus, returnableItems } from '../../shared/mock-data'; import { TradeinService } from '../../services/tradein.service'; import { LoggingService } from '../../services/logging.service'; import CheckoutUtils from '../../shared/checkout.utils'; import { HttpClient, HttpHandler } from '@angular/common/http'; describe('Customer info component', () => { let customerInfoObject: CustomerInfoComponent; let fixture: ComponentFixture; let dataStub: CartServiceStub; let spyNavigate: any; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, CartService, FormBuilder, CustomerService, UserService, LookupService, CheckoutService, CatalogService, TradeinService, LoggingService, FormBuilder, HttpClient, HttpHandler ], imports: [ FormsModule, RouterTestingModule ], declarations: [CustomerInfoComponent, BillingComponent, ShippingComponent, AdditionalsComponent, PPUAddressComponent, NumberPortingComponent, E911AddressComponent, NpaNxxComponent] }); fixture = TestBed.createComponent(CustomerInfoComponent); customerInfoObject = fixture.componentInstance; dataStub = fixture.debugElement.injector.get(CartService); spyNavigate = spyOn(customerInfoObject.router, 'navigate'); TestBed.compileComponents(); let sharedService = TestBed.get(SharedService); let userService = TestBed.get(UserService); let cartService = TestBed.get(CartService); customerInfoObject.storeId = 10553; customerInfoObject['user'] = userMockData; userService.user = customerInfoObject['user']; cartService.cart = cartData; customerInfoObject.cart = cartData; customerInfoObject.refTypeExchange = 'NA'; cartService.returnableItems = returnableItems.orderItems; })); it('should be created', () => { expect(customerInfoObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(customerInfoObject.getCartSummary).toBeDefined(); expect(customerInfoObject.getCustomerProfile).toBeDefined(); })); it('should resolve getcartSummary test data', fakeAsync(() => { customerInfoObject.cartService.setCart(cartData); const spy = spyOn(dataStub, 'getCartSummary').and.returnValue( Observable.of(cartData) ); customerInfoObject.getCartSummary(1245); fixture.detectChanges(); expect(customerInfoObject.cart).toEqual(cartData); })); it('should resolve get customer profile test data', fakeAsync(() => { const CustomerStub = fixture.debugElement.injector.get(CustomerService); customerInfoObject.cart['customer'] = {}; const spy = spyOn(CustomerStub, 'getCustomerProfile').and.returnValue( Observable.of(customerSearchResponse) ); customerInfoObject.getCustomerProfile(12345); fixture.detectChanges(); expect(customerInfoObject['cart'].customer).toEqual(customerSearchResponse); })); it('should resolve getSelectedShipModeId test data', fakeAsync(() => { customerInfoObject.usableShipping = ShippingData; customerInfoObject.enableShopify = true; const shipModeId = customerInfoObject.getSelectedShipModeId(); expect(shipModeId).toEqual(12345); })); it('should resolve updateShipMode test data', fakeAsync(() => { customerInfoObject.usableShipping = ShippingData; customerInfoObject.enableShopify = true; const shippingMode = customerInfoObject.usableShipping.usableShippingMode[0]; customerInfoObject.updateShipMode(shippingMode); expect(shippingMode.defaultShippingEnabled).toBeTruthy(); })); it('should resolve readyForPayReview test data', fakeAsync(() => { customerInfoObject.customerInfo.validDealerCode = false; const dealerCodeStatus = '_ERR_DEALER_CODE_NOT_FOUND' spyOn(customerInfoObject.sharedService, 'showErrorMessage'); customerInfoObject.readyForPayReview(); expect(customerInfoObject.sharedService.showErrorMessage).toHaveBeenCalledWith(dealerCodeStatus, false); })); it('update cart details is working fine', async(() => { const inputData = { 'valid': true, } const spy = spyOn(dataStub, 'updateCartDetails').and.returnValue( Observable.of(ShippingData) ); customerInfoObject.updateCartDetails(inputData); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(ShippingData); })); it('getPaperlessBilling is working fine', async(() => { const checkoutData = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(checkoutData, 'getPaperlessBilling').and.returnValue( Observable.of(CheckoutResponse) ); customerInfoObject.getPaperlessBilling('98021'); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(CheckoutResponse); })); it('should resolve shippingCalculate test data', fakeAsync(() => { const checkoutDataStub = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(dataStub, 'calculate').and.returnValue( Observable.of(CheckoutResponse) ); customerInfoObject.shippingCalculate(); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(CheckoutResponse); })); it('should resolve update shipmode Id test data', fakeAsync(() => { const checkoutDataStub = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(checkoutDataStub, 'updateShippingMode').and.returnValue( Observable.of(CheckoutResponse) ); customerInfoObject.updateShipModeId(); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(CheckoutResponse); })); it('update customer details is working fine', async(() => { const inputData = { 'valid': true, } const checkoutDataStub = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(checkoutDataStub, 'updateCustomerInfo').and.returnValue( Observable.of(CheckoutResponse) ); customerInfoObject.updateCustomerDetails(inputData); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(CheckoutResponse); })); it('should resolve update usable shipping test data', fakeAsync(() => { const usableShipping = { city: 'Bothell' } spyOn(CheckoutUtils, 'updateUsableShipping'); customerInfoObject.onShipModesUpdate(usableShipping); expect(CheckoutUtils.updateUsableShipping).toHaveBeenCalledWith(usableShipping); })); it('should resolve lookupNpaNxx test data', fakeAsync(() => { const checkoutDataStub = fixture.debugElement.injector.get(CheckoutService); const spy = spyOn(checkoutDataStub, 'lookupNpaNxx').and.returnValue( Observable.of(CheckoutResponse) ); customerInfoObject.lookupNpaNxx('98021'); fixture.detectChanges(); expect(customerInfoObject.karmaTestResult).toEqual(CheckoutResponse); })); });