import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { SharedService } from '../../../shared/shared.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; import { DebugElement } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { Observable } from 'rxjs/Rx'; import CartUtils from '../../../shared/cart.utils'; import { inject,tick,TestBed,getTestBed,async,fakeAsync,ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { creditCheckResponse, packagesData, customerSearchResponse, deleteRatePlanResponse, packageMockData, packagePlanData } from '../../../shared/mock-data'; import { PopupComponent } from '../../../shared/popup/popup.component'; import { UserService } from '../../../services/user.service'; import { LookupService } from '../../../services/lookup.service'; import { CatalogService } from '../../../services/catalog.service'; import { Customer } from '../../../shared/modal/customer'; import { Cart } from '../../../shared/modal/cart'; import { CartServiceStub } from '../../../services/cart.service.stub'; import { EspotComponent } from '../../espot/espot.component'; import { MoreDetailsComponent } from '../../moredetails/more-details.component'; import { CompareComponent } from '../../compare/compare.component'; import { MIRateplansComponent } from './mi-rateplans.component'; import { IRateplan } from '../../../shared/modal/common'; import { userMockData, cartDataResponse, cartData } from '../../../shared/mock-data'; 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 { MoreDetailsService } from '../../../shared/more-details-modal.service'; describe('MI Rateplan Component', () => { let obj: MIRateplansComponent; let fixture: ComponentFixture; let dataStub: CartServiceStub; let cartUtils: CartUtils; let ratePlanItem: IRateplan; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, CatalogService, LookupService, UserService, CheckoutService, CartService, CustomerService, TradeinService, LoggingService, HttpClient, HttpHandler, MoreDetailsService ], imports: [ FormsModule, RouterTestingModule ], declarations: [MIRateplansComponent, PopupComponent, EspotComponent, CompareComponent, MoreDetailsComponent] }); fixture = TestBed.createComponent(MIRateplansComponent); obj = fixture.componentInstance; obj.cartService.cart = cartData; obj['cart'] = cartData; obj.package = cartData.packages[0]; obj.customer = customerSearchResponse; obj.catalogService.gsmDevices = [ { productId: 1, productType: 'string', categoryId: 1, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], // soc: 'string', startDate: '04/12/2017', endDate: '04/12/2017', items: [], itemList: [], newPHPSOC: 'string', itemIndex: 1, canCompare: false } ]; obj['filters'] = [{ type: 'feature', items: [{ name: 'string', productIds: [1001, 1002, 1003], // for local isChecked: true, }] }, { type: 'features', items: [{ name: 'strings', productIds: [2001, 2002, 2003], // for local isChecked: true, }] }]; ratePlanItem = { productId: 1001, productType: 'string', categoryId: 1001, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: false, isVirtualDigit: true, isDuplicateDigit: false, contractType: 'string', contractLength: 1001, minimumLines: 1001, maximumLines: 1001, aalFee: 1001, autoPayCreditLines: 1001, autoPayCreditAmount: 1001, monthlyPrice: 1001, selectedLines: 1001, } obj.sharedService.userService['user'] = userMockData; dataStub = fixture.debugElement.injector.get(CartService); TestBed.compileComponents(); })); // tests here it('should be created', () => { expect(obj).toBeTruthy(); }); it('All methods are defined', async(() => { expect(obj.applyCustomSorting).toBeDefined(); expect(obj.setPage).toBeDefined(); expect(obj.expandDetails).toBeDefined(); })); it('should Change the state of the button for selected Rate Plan', fakeAsync(() => { obj.package = packagesData[1]; expect(obj.buttonState('string')).toEqual('REM'); })); it('should Change the state of the button for other than selected Rate Plan', fakeAsync(() => { obj.package = packagesData[1]; expect(obj.buttonState('string123')).toEqual('UPD'); })); it('should expand Details if it is not opened yet', fakeAsync(() => { obj.expandedProductId = null; obj.expandDetails(10015); expect(obj.expandedProductId).toEqual(10015); })); it('should expand Details if it is opened', fakeAsync(() => { obj.expandedProductId = 10015; obj.expandDetails(10015); expect(obj.expandedProductId).toEqual(null); })); it('should resolve add rateplan test data', fakeAsync(() => { const spy = spyOn(obj.sharedService, 'addLogging').and.returnValue( Observable.of(cartData) ); obj.addRateplan(ratePlanItem); fixture.detectChanges(); expect(obj.searchText).toEqual(''); expect(obj.sharedService.showSpinner).toBeTruthy(); })); it('should resolve delete rateplan test data', fakeAsync(() => { const spy = spyOn(dataStub, 'deleteRateplan').and.returnValue( Observable.of(deleteRatePlanResponse) ); obj.package.plan = cartData.packages[1].plan; obj.removeRateplan(ratePlanItem); fixture.detectChanges(); expect(obj.KarmaTestResult).toEqual(deleteRatePlanResponse); })); it('should resolve update rateplan test data', fakeAsync(() => { const spy = spyOn(dataStub, 'updateRateplan').and.returnValue( Observable.of(cartData) ); obj.package = packagesData[1]; obj.updateRateplan(ratePlanItem); fixture.detectChanges(); expect(obj.KarmaTestResult).toEqual(cartData); })); it('Clearing all filters functionality is working fine', async(() => { obj.clearAllFilters(); expect(obj.searchText).toEqual(''); expect(obj.rangeLineCount).toEqual(1); })); it('Should do custom sorting', () => { let simOnlyDevice = { productId: 1002, productType: 'string', categoryId: 1234, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isVirtualDigit: true, isDuplicateDigit: false, isTaxInclusivePlan: false, isPooledPlan: false, contractType: 'string', contractLength: 1234, minimumLines: 1234, maximumLines: 1234, aalFee: 1234, autoPayCreditLines: 1234, autoPayCreditAmount: 1234, monthlyPrice: 1234, // added for local use selectedLines: 1234 }; let smartPhoneDevice = { productId: 1001, productType: 'string', categoryId: 1234, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isVirtualDigit: true, isDuplicateDigit: false, isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: false, contractType: 'string', contractLength: 1234, minimumLines: 1234, maximumLines: 1234, aalFee: 1234, autoPayCreditLines: 1234, autoPayCreditAmount: 1234, monthlyPrice: 1234, // added for local use selectedLines: 1234 }; obj['rateplans'] = [simOnlyDevice, smartPhoneDevice]; obj.applyCustomSorting(); expect(obj.allItems[0].productId).toEqual(simOnlyDevice.productId); }); it('Checking if atleast one feature is checked for comparison ', async(() => { let totalcheckedCount = obj.atleastOneChecked(); expect(totalcheckedCount).toBeTruthy(); })); it('total number of pages are set correctly', fakeAsync(() => { obj.pager = { 'totalPages': 10, 'startIndex': 1, 'endIndex': 2 } obj.allItems = ['orderItems', 'orderStatus'] obj.setPage(2); expect(obj.pagedItems.length).toEqual(0); })); it('setting minimum and maximum lines is working as expected', fakeAsync(() => { let ratePlanItem = { productId: 1001, productType: 'string', categoryId: 1001, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: true, isVirtualDigit: true, isDuplicateDigit: false, contractType: 'string', contractLength: 1001, minimumLines: 1, maximumLines: 2, aalFee: 1001, autoPayCreditLines: 1001, autoPayCreditAmount: 1001, monthlyPrice: 1001, selectedLines: 1001, } const minmaxlinesArray = obj.minMaxLineNumbers(ratePlanItem); expect(minmaxlinesArray.length).toEqual(2); })); it('Monthly price calculation is workind fine', fakeAsync(() => { obj.rangeLineCount = 2; let ratePlanItem = { productId: 1001, productType: 'string', categoryId: 1001, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: false, isVirtualDigit: true, isDuplicateDigit: false, contractType: 'string', contractLength: 1001, minimumLines: 3, maximumLines: 4, aalFee: 1001, autoPayCreditLines: 10, autoPayCreditAmount: 25, monthlyPrice: 225, selectedLines: 3, } const monthlyPrice = obj.calculateMonthlyPrice(ratePlanItem); expect(monthlyPrice).toEqual(ratePlanItem.monthlyPrice); })); it('Discount price calculation is workind fine', fakeAsync(() => { obj.rangeLineCount = 2; const ratePlan = { productId: 1001, productType: 'string', categoryId: 1001, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: false, isVirtualDigit: true, isDuplicateDigit: false, contractType: 'string', contractLength: 1001, minimumLines: 4, maximumLines: 6, aalFee: 10, autoPayCreditLines: 2, autoPayCreditAmount: 5, monthlyPrice: 225, selectedLines: 5, } const dicountedMonthlyPrice = obj.discountedMonthlyPrice(ratePlan); expect(dicountedMonthlyPrice).toEqual(225); })); it('setting maximum line limit for a rate plan is done correctly', fakeAsync(() => { obj['rateplans'] = [{ productId: 1001, productType: 'string', categoryId: 1001, categoryName: 'string', name: 'string', description: 'string', rateplanSOC: 'string', isNCFPlan: false, isTaxInclusivePlan: false, isPooledPlan: false, isVirtualDigit: true, isDuplicateDigit: false, contractType: 'string', contractLength: 1001, minimumLines: 4, maximumLines: 6, aalFee: 10, autoPayCreditLines: 2, autoPayCreditAmount: 5, monthlyPrice: 225, selectedLines: 5, }, ] expect(obj.maximumLineLimit).toEqual(6); })); it('should resolve addItemPreprocess test data', fakeAsync(() => { const spy = spyOn(dataStub, 'createCartDetails').and.returnValue( Observable.of(cartData) ); obj.addItemPreprocess(ratePlanItem); fixture.detectChanges(); expect(obj.cart).toEqual(cartData); })); it('reset rateplans Filters', () => { obj.individualPlanFilters = []; const previousLength = obj.individualPlanFilters.length; obj.resetRateplanFilters(); expect(obj.individualPlanFilters.length).toEqual(previousLength); }); it('should resolve Sync cart test data', fakeAsync(() => { const spy = spyOn(dataStub, 'getCartSummary').and.returnValue( Observable.of(cartDataResponse) ); obj.syncCartUpdates(20045); fixture.detectChanges(); expect(obj.KarmaTestResult).toEqual(cartDataResponse); })); it('Should update active package', () => { obj.customer = customerSearchResponse; spyOn(obj.sharedService, 'showErrorMessage'); spyOn(obj, 'applyCustomSorting'); obj.updateActivePackage(); expect(obj.sharedService.showErrorMessage).toHaveBeenCalledWith('_ERR_MAX_LINE_LIMIT_EXCEEDED_ACCOUNT', false); expect(obj.applyCustomSorting).toHaveBeenCalled(); }); });