// 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 { TestBed, async, fakeAsync, ComponentFixture } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; 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 { GSMDevicesComponent } from './gsm-devices.component'; import { CartServiceStub } from '../../../services/cart.service.stub'; import { EspotComponent } from '../../espot/espot.component'; import { RelatedAccComponent } from '../related-acc/related-acc.component'; import { MoreDetailsComponent } from '../../moredetails/more-details.component'; import { CompareComponent } from '../../compare/compare.component'; import { cartData, packagesData, customerSearchResponse, userMockData, deviceData, packageMockData, deviceItem } from '../../../shared/mock-data'; import { TradeinService } from '../../../services/tradein.service'; import { LoggingService } from '../../../services/logging.service'; import { CheckoutService } from '../../../services/checkout.service'; import { MarketingService } from '../../../services/marketing.service'; import { OffersComponent } from '../../offers/offers.component'; import { HttpClient, HttpHandler } from '@angular/common/http'; import { CompareService } from '../../../shared/compare-modal.service'; import { MoreDetailsService } from '../../../shared/more-details-modal.service'; describe('GSM devices component', () => { let gsmDevicesObject: GSMDevicesComponent; let fixture: ComponentFixture; let dataStub: CartServiceStub; let cartUtils: CartUtils; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, CatalogService, LookupService, UserService, CartService, CustomerService, TradeinService, LoggingService, CheckoutService, MarketingService, HttpClient, HttpHandler, HttpClient, CompareService, MoreDetailsService ], imports: [ FormsModule, RouterTestingModule ], declarations: [GSMDevicesComponent, PopupComponent, EspotComponent, CompareComponent, RelatedAccComponent, MoreDetailsComponent, OffersComponent] }); fixture = TestBed.createComponent(GSMDevicesComponent); gsmDevicesObject = fixture.componentInstance; gsmDevicesObject.cartService.cart = cartData; gsmDevicesObject['cart'] = cartData; gsmDevicesObject.package = CartUtils.getActivePackage(this.cart, 'voice'); gsmDevicesObject.userService.user = userMockData; gsmDevicesObject.customer = customerSearchResponse; dataStub = fixture.debugElement.injector.get(CartService); gsmDevicesObject.package = packagesData[0]; TestBed.compileComponents(); TestBed.get(SharedService); TestBed.get(CartService); TestBed.get(UserService); })); // tests here it('should be created', () => { expect(gsmDevicesObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(gsmDevicesObject.addDevice).toBeDefined(); expect(gsmDevicesObject.updateDevice).toBeDefined(); expect(gsmDevicesObject.removeDevice).toBeDefined(); expect(gsmDevicesObject.applyCustomSorting).toBeDefined(); expect(gsmDevicesObject.setPage).toBeDefined(); expect(gsmDevicesObject.expandDetails).toBeDefined(); expect(gsmDevicesObject.updateCompareProducts).toBeDefined(); })); it('should Update Device', fakeAsync(() => { const spy = spyOn(dataStub, 'updateDevice').and.returnValue( Observable.of(cartData) ); gsmDevicesObject.storeId = 10553; gsmDevicesObject.updateDevice(deviceData); fixture.detectChanges(); expect(gsmDevicesObject.cart).toEqual(cartData); })); it('should Add Device', fakeAsync(() => { const spy = spyOn(dataStub, 'createDevice').and.returnValue( Observable.of(cartData) ); gsmDevicesObject.storeId = 10553; gsmDevicesObject.addDevice(deviceData); fixture.detectChanges(); expect(gsmDevicesObject.cart).toEqual(cartData); })); it('should remove Device', fakeAsync(() => { const spy = spyOn(dataStub, 'deleteDevice').and.returnValue( Observable.of(cartData) ); gsmDevicesObject.storeId = 10553; gsmDevicesObject.removeDevice(deviceData); fixture.detectChanges(); expect(gsmDevicesObject.cart).toEqual(cartData); })); it('should Change the state of the button if there is no device', fakeAsync(() => { // const spy = spyOn(dataStub, 'updateDevice').and.returnValue( // Observable.of(cartData) // ); let deviceItem = { itemId: 12345, name: 'string', description: 'string', deviceSKU: 'string', isByos: false, thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'string', shipDateFrom: '10/20', shipDateTo: '1/20', suggestedPrice: 12345, instantDiscount: 12345, openBoxDiscount: 12345, promotionDiscount: 12345, mailInRebate: 12345, finalPrice: 12345, loan: [], lease: [], promotions: [], state: 'string', sim: null } gsmDevicesObject.package = packagesData[1]; expect(gsmDevicesObject.buttonState(deviceItem)).toEqual('ADD'); })); it('should expand Details if it is not opened yet', fakeAsync(() => { gsmDevicesObject.expandedProductId = null; gsmDevicesObject.expandDetails(12345); expect(gsmDevicesObject.expandedProductId).toEqual(12345); })); it('should expand Details if it is opened', fakeAsync(() => { gsmDevicesObject.expandedProductId = 12345; gsmDevicesObject.expandDetails(12345); expect(gsmDevicesObject.expandedProductId).toEqual(null); })); it('should disable the compare checkbox if already 5 devices are checked', fakeAsync(() => { let device = { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'Apple', items: [], itemList: [], descriptions: [], newPHPSOC: 'string', startDate: '02/02/2017', endDate: '05/05/2017', // local veriable for angualr2 itemIndex: 12345, canCompare: false }; gsmDevicesObject.compareProducts = [{ productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'Apple', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', descriptions: [], startDate: '02/02/2017', manufacturer: 'Apple', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'Apple', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: false }]; expect(gsmDevicesObject.disableCompare(device)).toBeTruthy(); })); it('should be enabled the compare checkbox if compare array have less than 5 devices', fakeAsync(() => { let device = { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: true }; gsmDevicesObject.compareProducts = [{ productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'Apple', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 12345, canCompare: true }]; expect(gsmDevicesObject.disableCompare(device)).toBeFalsy(); })); it('should return proper inventory status', async(() => { expect(gsmDevicesObject.getInventoryStatus('OOS')).toEqual('Out of stock'); })); it('total number of pages are set correctly', fakeAsync(() => { gsmDevicesObject.pager = { 'totalPages': 10, 'startIndex': 1, 'endIndex': 2 } gsmDevicesObject.allItems = ['orderItems', 'orderStatus'] gsmDevicesObject.setPage(2); expect(gsmDevicesObject.pagedItems.length).toEqual(0); })); it('Checking if atleast one feature is checked for comparison ', async(() => { let totalcheckedCount = gsmDevicesObject.atleastOneChecked(); expect(totalcheckedCount).toBeFalsy(); })); it('Clearing all filters functionality is working fine', async(() => { gsmDevicesObject['filters'] = [{ type: 'gsm-device', items: [{ name: 'string', productIds: [1234, 1002, 1003], // for local isChecked: true, }] }, { type: 'gsm-device', items: [{ name: 'strings', productIds: [2001, 2002, 2003], // for local isChecked: false, }] }]; gsmDevicesObject.clearAllFilters(); expect(gsmDevicesObject.searchText).toEqual(''); expect(gsmDevicesObject.financeType).toEqual('FRP'); })); it('should resolve addItemPreprocess test data', fakeAsync(() => { const spy = spyOn(dataStub, 'createCartDetails').and.returnValue( Observable.of(cartData) ); const deviceItem = { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], startDate: 'string', endDate: 'string', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 1, canCompare: true, } gsmDevicesObject.addItemPreprocess(deviceItem); fixture.detectChanges(); expect(gsmDevicesObject.cart).toEqual(cartData); })); it('Should do custom sorting', () => { let simOnlyDevice = { productId: 110, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], // soc: 'string', items: deviceItem, itemList: deviceItem, startDate: '04/12/2017', endDate: '04/12/2017', newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 0, canCompare: false }; let smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', descriptions: [], manufacturer: 'Apple', startDate: '04/12/2017', endDate: '04/12/2017', // soc: 'string', items: deviceItem, itemList: deviceItem, newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 1, canCompare: true }; gsmDevicesObject.catalogService.gsmDevices = [simOnlyDevice, smartPhoneDevice]; gsmDevicesObject.devices = [simOnlyDevice, smartPhoneDevice] ; gsmDevicesObject.isBYOSOnlyOrder = true; gsmDevicesObject.applyCustomSorting(); expect(gsmDevicesObject.allItems[0].productId).toEqual(simOnlyDevice.productId); }); it('selectSuggestionItem is working correctly', () => { const item = 'test'; spyOn(gsmDevicesObject, 'applyCustomSorting') gsmDevicesObject.selectSuggestionItem(item); expect(gsmDevicesObject.applyCustomSorting).toHaveBeenCalled(); }); it('Should update active package', () => { gsmDevicesObject.customer = customerSearchResponse; spyOn(gsmDevicesObject.sharedService, 'showErrorMessage'); spyOn(gsmDevicesObject, 'applyCustomSorting'); gsmDevicesObject.updateActivePackage(); expect(gsmDevicesObject.applyCustomSorting).toHaveBeenCalled(); }); it('Should update compare products', () => { const product = { productId: 1234, productType: 'string', categoryId: 1234, manufacturer: 'Apple', categoryName: 'string', name: 'string', description: 'string', descriptions: [], startDate: 'string', endDate: 'string', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 1, canCompare: true } gsmDevicesObject.updateCompareProducts(true, product); expect(gsmDevicesObject.compareProducts[0]).toEqual(product); }); it('should resolve Validate Byos test data', fakeAsync(() => { const deviceItem = { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', descriptions: [], startDate: 'string', endDate: 'string', manufacturer: 'Apple', items: [], itemList: [], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 1, canCompare: true, } const spy = spyOn(dataStub, 'deleteDevice').and.returnValue( Observable.of(cartData) ); gsmDevicesObject.validateBYOS(deviceItem); fixture.detectChanges(); expect(gsmDevicesObject.cart).toEqual(cartData); })); it('Should reset device filters', () => { gsmDevicesObject.filters = [{ type: 'Manufacturer', items: [{ name: 'string', productIds: [], isChecked: true, }], }]; gsmDevicesObject.resetDeviceFilters(); expect(gsmDevicesObject.deviceBrandFilters).toEqual([{ name: 'string', productIds: [], isChecked: true }]); }); it('should resolve device conflicts test data', fakeAsync(() => { const device = { productId: 1234, productType: 'string', categoryId: 1234, categoryName: 'string', name: 'string', manufacturer: 'string', description: 'string', descriptions: [], startDate: 'string', endDate: 'string', newPHPSOC: 'string', items: [ { itemId: 123, name: 'string', description: 'string', deviceSKU: 'string', isByos: false, sim: 'string', thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'string', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 123, instantDiscount: 123, openBoxDiscount: 123, promotionDiscount: 123, mailInRebate: 123, finalPrice: 123, loan: [], lease: [], promotions: [] }], itemList: [], // local veriable for angualr2 itemIndex: 0, canCompare: false } const cartStub = fixture.debugElement.injector.get(CartService); const spy = spyOn(cartStub, 'deviceFeatureConflicts').and.returnValue( Observable.of(cartData) ); gsmDevicesObject.checkForDeviceConflicts(device); expect(gsmDevicesObject.deviceConflicts).toEqual(cartData); })); it('should return proper inventory status', async(() => { const status = true; gsmDevicesObject.isRelatedAccAddToCart(status); expect(gsmDevicesObject.isRelatedAccStatus).toEqual(status); })); });