// 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 { 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 { 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 { AccessoriesComponent } from './accessories.component'; import CartUtils from '../../shared/cart.utils'; import { CustomerService } from '../../services/customer.service'; import { CartService } from '../../services/cart.service'; import { SharedService } from '../../shared/shared.service'; import { creditCheckResponse, packagesData, cartData, customerSearchResponse, deleteAccessoryResponse, packageMockData, userMockData } from '../../shared/mock-data'; import { IAccessory } from '../../shared/modal/common'; import { RelatedAccComponent } from '../devices/related-acc/related-acc.component'; import * as Constants from '../../shared/constants'; import { TradeinService } from '../../services/tradein.service'; import { LoggingService } from '../../services/logging.service'; import { MarketingService } from '../../services/marketing.service'; import { CheckoutService } from '../../services/checkout.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('Accessory Component', () => { let accessoriesObject: AccessoriesComponent; let fixture: ComponentFixture; let dataStub: CartServiceStub; let cartUtils: CartUtils; let accessoryItem: IAccessory; beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ SharedService, CatalogService, LookupService, UserService, CartService, TradeinService, LoggingService, CustomerService, CheckoutService, MarketingService, HttpClient, HttpHandler, CompareService, MoreDetailsService ], imports: [ FormsModule, RouterTestingModule ], declarations: [AccessoriesComponent, PopupComponent, EspotComponent, CompareComponent, MoreDetailsComponent, RelatedAccComponent, OffersComponent] }); fixture = TestBed.createComponent(AccessoriesComponent); accessoriesObject = fixture.componentInstance; accessoriesObject['deviceItem'] = [{ itemId: 12345, name: 'string', description: 'string', deviceSKU: 'string', isByos: false, thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'string', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 12345, instantDiscount: 12345, openBoxDiscount: 12345, promotionDiscount: 12345, mailInRebate: 12345, finalPrice: 12345, loan: [{ crp: 1, term: 1, downPayment: 1, monthlyPrice: 1 }], lease: [{ crp: 1, term: 1, downPayment: 1, monthlyPrice: 1, residualPrice: 1 }], state: 'string', sim: 'string' }]; accessoriesObject['accessoryItem'] = [{ productId: 1234, productType: 'string', categoryId: 1234, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: [{ itemId: 12345, name: 'string', description: 'string', accessorySKU: 'string', thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'string', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 12345, instantDiscount: 12345, openBoxDiscount: 12345, promotionDiscount: 12345, mailInRebate: 12345, finalPrice: 12345, loan: [], lease: [] }], itemList: [], // local veriable for angualr2 itemIndex: 0, canCompare: false }]; accessoriesObject['accessories'] = [{ productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', startDate: '04/10/2017', endDate: '08/10/2017', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: true }, { productId: 12346, productType: 'string', categoryId: 123456, categoryName: 'string', name: 'string', manufacturer: 'Apple', startDate: '04/10/2017', endDate: '08/10/2017', description: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: true } ] accessoriesObject.catalogService.accessories = accessoriesObject['accessories']; accessoriesObject['deviceFilter'] = { type: 'Device Type', items: [{ name: 'PrePaid Cards', productIds: [110, 111, 112], // for local isChecked: true }, { name: 'Screen Protectors', productIds: [210, 211, 212], // for local isChecked: false }] }; accessoriesObject['featuresFilters'] = { type: 'Feature', items: [{ name: 'feature1', productIds: [115, 116, 117], // for local isChecked: true }, { name: 'feature2', productIds: [214, 215, 216], // for local isChecked: false }] }; accessoriesObject['filters'] = [accessoriesObject['deviceFilter'], accessoriesObject['featuresFilters']]; accessoriesObject.cartService.cart = cartData; accessoriesObject['cart'] = cartData; accessoriesObject.package = packageMockData[0]; accessoriesObject.customer = customerSearchResponse; accessoriesObject.catalogService.gsmDevices = [ { productId: 1, productType: 'string', categoryId: 1, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', descriptions: [], startDate: '04/10/2017', endDate: '08/10/2017', items: [], itemList: [], itemIndex: 1, newPHPSOC: 'string', canCompare: false } ]; accessoryItem = { productId: 1234, productType: 'string', categoryId: 1234, categoryName: 'string', manufacturer: 'Apple', name: 'string', description: 'string', startDate: '04/10/2017', endDate: '08/10/2017', items: [{ itemId: 12345, name: 'string', description: 'string', accessorySKU: 'string', thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'string', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 12345, instantDiscount: 12345, openBoxDiscount: 12345, promotionDiscount: 12345, mailInRebate: 12345, finalPrice: 12345, loan: [], lease: [], promotions: [] }], itemList: [], // local veriable for angualr2 itemIndex: 0, canCompare: false } accessoriesObject.sharedService.userService['user'] = userMockData; dataStub = fixture.debugElement.injector.get(CartService); TestBed.compileComponents(); TestBed.get(SharedService); TestBed.get(CartService); TestBed.get(UserService); })); // tests here it('should be created', () => { expect(accessoriesObject).toBeTruthy(); }); it('All methods are defined', async(() => { expect(accessoriesObject.applyCustomSorting).toBeDefined(); expect(accessoriesObject.setPage).toBeDefined(); expect(accessoriesObject.expandDetails).toBeDefined(); })); it('should Change the state of the button if there is no Accessories', fakeAsync(() => { const accessoriesItem = { itemId: 12345, name: 'string', description: 'string', accessorySKU: 'string', thumbNail: 'string', startDate: '04/10/2017', endDate: '08/10/2017', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: true, status: 'string', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 12345, instantDiscount: 12345, openBoxDiscount: 12345, promotionDiscount: 12345, mailInRebate: 12345, finalPrice: 12345, loan: [], lease: [], promotions: [] } expect(accessoriesObject.buttonState(accessoriesItem)).toEqual('ADD'); })); it('should expand Details if it is not opened yet', fakeAsync(() => { accessoriesObject.expandedProductId = null; accessoriesObject.expandDetails(12345); expect(accessoriesObject.expandedProductId).toEqual(12345); })); it('should expand Details if it is opened', fakeAsync(() => { accessoriesObject.expandedProductId = 12345; accessoriesObject.expandDetails(12345); expect(accessoriesObject.expandedProductId).toEqual(null); })); it('Clearing all filters functionality is working fine', async(() => { accessoriesObject.clearAllFilters(); expect(accessoriesObject.searchText).toEqual(''); })); it('Checking if atleast one feature is checked for comparison ', async(() => { const totalcheckedCount = accessoriesObject.atleastOneChecked(); expect(totalcheckedCount).toBeTruthy(); })); it('should resolve delete accessory test data', fakeAsync(() => { const deleteAccessoryStub = fixture.debugElement.injector.get(CartService); const accessoriesItem = { productId: 12345, startDate: '04/10/2017', endDate: '08/10/2017', productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: [{ itemId: 1, name: 'string', description: 'string', accessorySKU: 'string', thumbNail: 'string', fullImage: 'string', colorName: 'string', colorImageUrl: 'string', isRefurbished: false, status: 'INS', shipDateFrom: 'string', shipDateTo: 'string', suggestedPrice: 1, instantDiscount: 1, openBoxDiscount: 1, promotionDiscount: 1, mailInRebate: 1, finalPrice: 1, loan: [], lease: [], promotions: [] }], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: true } const spy = spyOn(deleteAccessoryStub, 'deleteAccessory').and.returnValue( Observable.of(deleteAccessoryResponse) ); accessoriesObject['package'] = packagesData[1]; accessoriesObject.removeAccessory(accessoriesItem); fixture.detectChanges(); expect(accessoriesObject.karmaTestResult).toEqual(deleteAccessoryResponse); })); it('should Add Accessory', fakeAsync(() => { const spy = spyOn(dataStub, 'createAccessory').and.returnValue( Observable.of(cartData) ); accessoriesObject.addAccessory(accessoryItem); fixture.detectChanges(); expect(accessoriesObject.cart).toEqual(cartData); })); it('Should do custom sorting', () => { const prePaidCardsAccessory = { productId: 110, productType: 'string', startDate: '04/10/2017', endDate: '08/10/2017', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: false }; const smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, startDate: '04/10/2017', endDate: '08/10/2017', categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 1, canCompare: true }; accessoriesObject['accessories'] = [prePaidCardsAccessory, smartPhoneDevice]; accessoriesObject.applyCustomSorting(); expect(accessoriesObject.allItems[0].productId).toEqual(prePaidCardsAccessory.productId); }); it('total number of pages are set correctly', fakeAsync(() => { accessoriesObject.pager = { 'totalPages': 10, 'startIndex': 1, 'endIndex': 2 } accessoriesObject.allItems = ['orderItems', 'orderStatus'] accessoriesObject.setPage(2); expect(accessoriesObject.pagedItems.length).toEqual(0); })); it('Should sort by deviceSKU', () => { const prePaidCardsAccessory = { productId: 110, productType: 'string', categoryId: 12345, startDate: '04/10/2017', endDate: '08/10/2017', categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: false }; const smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, startDate: '04/10/2017', endDate: '08/10/2017', categoryName: 'string', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 1, canCompare: true }; accessoriesObject['accessories'] = [prePaidCardsAccessory, smartPhoneDevice]; accessoriesObject['devicesFilterListArray'] = [{ 'deviceSKU': '12345', 'accessoryList': accessoriesObject['accessories'] }] accessoriesObject.storeId = Constants.STORE_ID_CD; accessoriesObject.sortByDevice('12345'); expect(accessoriesObject.allItems[0].productId).toEqual(prePaidCardsAccessory.productId); }); it('Should filter by Device Type', () => { const filteredResult = accessoriesObject.deviceTypeFilters; expect(filteredResult).toEqual(accessoriesObject['deviceFilter'].items); }); it('Should filter by Device Feature', () => { const filteredResult = accessoriesObject.deviceFeatureFilters; expect(filteredResult).toEqual(accessoriesObject['featuresFilters'].items); }); it('Should get the Device Item for selected product Ids', () => { const simOnlyDevice = { productId: 110, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', startDate: '04/10/2017', manufacturer: 'Apple', endDate: '08/10/2017', description: 'string', descriptions: [], soc: 'string', items: accessoriesObject['deviceItem'], itemList: accessoriesObject['deviceItem'], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 0, canCompare: false }; const smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, categoryName: 'string', startDate: '04/10/2017', endDate: '08/10/2017', name: 'string', description: 'string', manufacturer: 'Apple', descriptions: [], soc: 'string', items: accessoriesObject['deviceItem'], itemList: accessoriesObject['deviceItem'], newPHPSOC: 'string', // local veriable for angualr2 itemIndex: 1, canCompare: true }; accessoriesObject['devices'] = [simOnlyDevice, smartPhoneDevice]; const testProductIds = [110, 234, 546]; accessoriesObject.getDeviceItems(testProductIds); expect(accessoriesObject.deviceItems).toEqual(accessoriesObject['deviceItem']); }); it('Should create selected devices array and then get the Device Item for selected product Ids', () => { const prePaidCardsAccessory = { productId: 110, productType: 'string', categoryId: 12345, categoryName: 'string', startDate: '04/10/2017', endDate: '08/10/2017', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: false }; const smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, categoryName: 'string', startDate: '04/10/2017', endDate: '08/10/2017', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 1, canCompare: true }; accessoriesObject['accessories'] = [prePaidCardsAccessory, smartPhoneDevice]; accessoriesObject['devicesFilterListArray'] = [{ 'deviceSKU': '12345', 'accessoryList': accessoriesObject['accessories'] }] accessoriesObject.storeId = Constants.STORE_ID_CD; accessoriesObject['devicesFilterListArray'] = [{ 'deviceSKU': '12345', 'accessoryList': accessoriesObject['accessories'] }]; const testInput = 'Apple,12345'; accessoriesObject.createSelectedDevicesArray(testInput); expect(accessoriesObject.allItems[0].productId).toEqual(prePaidCardsAccessory.productId); }); it('Should fetch the accessories by previous selection', () => { const selectedDevicesArray = [{ 'name': 'Apple', 'deviceSKU': '12345', 'isChecked': true }] const prePaidCardsAccessory = { productId: 110, productType: 'string', categoryId: 12345, categoryName: 'string', startDate: '04/10/2017', endDate: '08/10/2017', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 0, canCompare: false }; const smartPhoneDevice = { productId: 210, productType: 'string', categoryId: 12345, categoryName: 'string', startDate: '04/10/2017', endDate: '08/10/2017', name: 'string', description: 'string', manufacturer: 'string', items: accessoriesObject['accessoryItem'], itemList: accessoriesObject['accessoryItem'], // local veriable for angualr2 itemIndex: 1, canCompare: true }; accessoriesObject['accessories'] = [prePaidCardsAccessory, smartPhoneDevice]; accessoriesObject['devicesFilterListArray'] = [{ 'deviceSKU': '12345', 'accessoryList': accessoriesObject['accessories'] }] accessoriesObject.storeId = Constants.STORE_ID_CD; accessoriesObject['devicesFilterListArray'] = [{ 'deviceSKU': '12345', 'accessoryList': accessoriesObject['accessories'] }]; const testInput = 'Apple,12345'; accessoriesObject.accessoriesByPreviousSelections('12345', true); expect(accessoriesObject.allItems[0].productId).toEqual(prePaidCardsAccessory.productId); }); it('Inventory Status is retrieved correctly', async(() => { const status = accessoriesObject.getInventoryStatus('INS'); expect(status).toEqual('In Stock'); })); it('should disable the compare checkbox if already 5 devices are checked', fakeAsync(() => { const device = { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', items: [], itemList: [], manufacturer: 'Apple', descriptions: [], startDate: '02/02/2017', endDate: '05/05/2017', // local veriable for angualr2 itemIndex: 12345, canCompare: false }; accessoriesObject.compareProducts = [{ productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', startDate: '02/02/2017', manufacturer: 'Apple', endDate: '05/05/2017', items: [], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', manufacturer: 'Apple', startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', manufacturer: 'Apple', description: 'string', startDate: '02/02/2017', endDate: '05/05/2017', items: [], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: false }, { productId: 12345, productType: 'string', categoryId: 12345, categoryName: 'string', name: 'string', description: 'string', startDate: '02/02/2017', manufacturer: 'Apple', endDate: '05/05/2017', items: [], itemList: [], // local veriable for angualr2 itemIndex: 12345, canCompare: false }]; expect(accessoriesObject.disableCompare(device)).toBeTruthy(); })); it('should resolve addItemPreprocess test data', fakeAsync(() => { const spy = spyOn(dataStub, 'createCartDetails').and.returnValue( Observable.of(cartData) ); accessoriesObject.addItemPreprocess(accessoryItem); fixture.detectChanges(); expect(accessoriesObject.cart).toEqual(cartData); })); it('Should update compare products', () => { const product = { productId: 1234, productType: 'string', categoryId: 1234, categoryName: 'string', name: 'string', description: 'string', startDate: 'string', manufacturer: 'Apple', endDate: 'string', items: [], itemList: [], // local veriable for angualr2 itemIndex: 1, canCompare: true } accessoriesObject.updateCompareProducts(true, product); expect(accessoriesObject.compareProducts[0]).toEqual(product); }); it('Should remove device from selected devices ', () => { accessoriesObject.selectedDevicesArray = [{ 'name': 'Apple', 'deviceSKU': '12345', 'isChecked': true }, { 'name': 'Apple', 'deviceSKU': '123456', 'isChecked': true }] spyOn(accessoriesObject, 'sortByDevice'); accessoriesObject.removeFromDeviceSelected('12345', true); expect(accessoriesObject.sortByDevice).toHaveBeenCalledWith('123456'); }); it('reset Accessory Filters', () => { accessoriesObject.selectedDevicesArray = [{ 'name': 'Apple', 'deviceSKU': '12345', 'isChecked': true }, { 'name': 'Apple', 'deviceSKU': '123456', 'isChecked': true }] const previousLength = accessoriesObject.selectedDevicesArray.length; accessoriesObject.resetAccessoryFilters(); expect(accessoriesObject.selectedDevicesArray.length).toEqual(previousLength); }); it('Should update active package', () => { accessoriesObject.customer = customerSearchResponse; spyOn(accessoriesObject.sharedService, 'showErrorMessage'); spyOn(accessoriesObject, 'applyCustomSorting'); accessoriesObject.updateActivePackage(); expect(accessoriesObject.applyCustomSorting).toHaveBeenCalled(); }); it('selectSuggestionItem is working correctly', () => { const item = 'test'; spyOn(accessoriesObject, 'applyCustomSorting') accessoriesObject.selectSuggestionItem(item); expect(accessoriesObject.applyCustomSorting).toHaveBeenCalled(); }); });