// import { Component, OnInit, OnDestroy } 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 { CustomerComponent } from './customer.component' import { TestBed, async, fakeAsync, ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { CartServiceStub } from '../../services/cart.service.stub'; import { SharedService } from '../../shared/shared.service'; import { CatalogService } from '../../services/catalog.service'; import { LookupService } from '../../services/lookup.service'; import { UserService } from '../../services/user.service'; import { CartService } from '../../services/cart.service'; import { CustomerService } from '../../services/customer.service'; import { CustomerDetailComponent } from './customer-detail/customer-detail.component'; import { CreditCheckComponent } from './credit-check/credit-check.component'; import { PopupComponent } from '../../shared/popup/popup.component'; import { DepositInfoComponent } from './customer-detail/deposit-info/deposit-info.component'; import { LineInfoComponent } from './customer-detail/line-info/line-info.component'; import { AccountInfoComponent } from './customer-detail/account-info/account-info.component'; import { OrderHistoryComponent } from './customer-detail/order-history/order-history.component'; import { UserServiceStub } from '../../services/user.service.stub'; import { dealerCodeStatus, newCustomerData, cartDataResponse } from '../../shared/spec.constants'; import Utils from '../../shared/utils'; import { CustomerSearchStub } from '../../services/customer.service.stub'; import { ManualReviewComponent } from '../manualreview/manual-review.component'; import { cartData, userMockData, customerSearchResponse } from '../../shared/mock-data'; import { TradeinService } from '../../services/tradein.service'; import { LoggingService } from '../../services/logging.service'; import { EspotComponent } from '../espot/espot.component'; import { CheckoutService } from '../../services/checkout.service'; import { DummyCheckComponent } from './dummy-check/dummy-check.component'; import { HttpClient, HttpHandler } from '@angular/common/http'; describe('Customer component', () => { let customerObject: CustomerComponent; let fixture: ComponentFixture; let customerDataStub: CustomerSearchStub; let userDataStub: UserServiceStub; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, CatalogService, LookupService, UserService, CartService, CheckoutService, CustomerService, TradeinService, LoggingService, HttpClient, HttpHandler ], imports: [ FormsModule, RouterTestingModule ], declarations: [CustomerComponent, CustomerDetailComponent, CreditCheckComponent, PopupComponent, DepositInfoComponent, LineInfoComponent, ManualReviewComponent, AccountInfoComponent, OrderHistoryComponent, EspotComponent, DummyCheckComponent] }); fixture = TestBed.createComponent(CustomerComponent); customerObject = fixture.componentInstance; customerObject['customer'] = customerSearchResponse; customerObject.customerService.customer = customerObject['customer']; customerObject['cart'] = cartData; customerObject.customerModel = { 'firstName': 'string', 'lastName': 'string', 'middleName': 'string', 'address1': 'string', 'address2': 'string', 'city': 'string', 'state': 'WA', 'zipCode': '98021', 'phone1': '123456790', 'email1': 'test@test.com', 'webOrderId': '12345', 'dealerCode': '10422', 'nonTMOMobileNumber': '123-456-7890', 'nonTMOMobilePin': '1234', 'eligibility':'Y', 'eligibilityDate':'2018-08-01', 'paymentCount':'12', 'brandType':'Prepaid', 'accountNumber':'12345678' }; customerObject['user'] = userMockData; customerObject.userService.user = userMockData; customerObject.cartService.cart = cartData; customerObject.customerService.customer = customerSearchResponse; customerDataStub = fixture.debugElement.injector.get(CustomerService); TestBed.compileComponents(); TestBed.get(SharedService); TestBed.get(CartService); TestBed.get(UserService); })); // tests here it('should be created', () => { expect(customerObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(customerObject.updateCreditType).toBeDefined(); })); it('switching credit type is working correctly', async(() => { spyOn(customerObject, 'updateCreditType'); customerObject.cart.packages = []; customerObject.switchCreditType('NC'); expect(customerObject.updateCreditType).toHaveBeenCalledWith(12345, 'NC'); })); it('reverting credit type is working correctly', async(() => { customerObject.revertBackCreditType('NC'); expect(customerObject.creditType).toEqual('CC'); })); it('customer is initialized', async(() => { spyOn(customerObject, 'updateCustomerModel'); customerObject.initializeCustomer(); expect(customerObject.updateCustomerModel).toHaveBeenCalled(); })); it('Saving customer profile is working as expected', fakeAsync(() => { customerObject.cartService.setCart(cartData); const spy = spyOn(customerDataStub, 'createCustomer').and.returnValue( Observable.of(newCustomerData) ); customerObject.saveCustomerProfile(); // fixture.detectChanges(); expect(customerObject.KarmaTestResult).toEqual(newCustomerData); })); it('should resolve dealer code test data', fakeAsync(() => { customerObject.cartService.setCart(cartData); userDataStub = fixture.debugElement.injector.get(UserService); const spy = spyOn(userDataStub, 'validateDealerCode').and.returnValue( Observable.of(dealerCodeStatus) ); customerObject.validateDealerCode('1a2b3c'); fixture.detectChanges(); expect(customerObject.customerModel.validDealerCode).toEqual(dealerCodeStatus.isDealerCodeValid); })); it('should resolve get cart summary test data', fakeAsync(() => { customerObject.cartService.setCart(cartData); const cartDataStub = fixture.debugElement.injector.get(CartService); const spy = spyOn(cartDataStub, 'getCartSummary').and.returnValue( Observable.of(cartDataResponse) ); customerObject.removeConflicts('remove'); fixture.detectChanges(); expect(customerObject.cartSummaryData).toEqual(cartDataResponse); })); it('cart is correctly associated to cart', fakeAsync(() => { const customerSearchStub = fixture.debugElement.injector.get(CustomerService); const spy = spyOn(customerSearchStub, 'createCustomer').and.returnValue( Observable.of(newCustomerData) ); customerObject.assosiateCustomerToCart(customerObject['customer']); fixture.detectChanges(); expect(customerObject.KarmaTestResult).toEqual(newCustomerData); })); it('acceptCreditSwitch functionality working fine', async(() => { customerObject.creditType = 'CC'; spyOn(customerObject, 'updateCreditType'); customerObject.acceptCreditSwitch('Continue'); expect(customerObject.updateCreditType).toHaveBeenCalledWith(customerObject.cart.orderId, customerObject.creditType); })); it('saveFields functionality working fine', async(() => { let val = { 'value': 1234, 'valid': true } spyOn(customerObject, 'saveCustomerProfile'); customerObject.saveFields(val); expect(customerObject.saveCustomerProfile).toHaveBeenCalled(); })); it('cart is updated accordingly', async(() => { customerObject.updateCart(customerObject['cart']); expect(customerObject.cart).toEqual(customerObject['cart']); })); it('zipcode promotion Orders are eligible', fakeAsync(() => { const customerStub = fixture.debugElement.injector.get(CustomerService); const spy = spyOn(customerStub, 'zipCodePromotion').and.returnValue( Observable.of(customerSearchResponse) ); customerObject.isZipCodePromoEligible(); expect(customerObject.customerService.zipCodePromotion).toHaveBeenCalled(); })); });