import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ProductListComponent } from './product-list.component';
import { FormsModule } from '@angular/forms';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { ProductService } from './product.service';
describe('ProductListComponent', () => {
let component: ProductListComponent;
let fixture: ComponentFixture<ProductListComponent>;
let debugElement: DebugElement;
let productService: ProductService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProductListComponent ],
imports: [FormsModule],
providers: [ProductService]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProductListComponent);
productService = TestBed.get(ProductService);
component = fixture.componentInstance;
fixture.detectChanges();
debugElement = fixture.debugElement;
});
fit('should test filter product list (done)', (done)=> {
//component.searchText='fresh';
let productSpy = spyOn(productService, 'filterProductList').withArgs('fresh').and.callThrough();
productSpy.calls.mostRecent().returnValue.then(()=>{
fixture.detectChanges();
const value = debugElement.query(By.css('#product_0')).nativeElement.innerText;
expect(value).toContain(component.searchText);
done();
});
});
});
Я пытаюсь написать контрольный пример, чтобы отфильтровать список продуктов и написал шпион продукта, но выдает ошибку:
TypeError: Cannot read property 'returnValue' of undefined
для следующей строки:
productSpy.calls.mostRecent().returnValue.then
Я пытался найти решение в Интернете, но не смог найти подходящее решение.
Ниже приводится служба, которую я пытаюсь проверить: 1. filterProductList - примет строку и отфильтрует через список названий продуктов и получить список продуктов - один, соответствующий заданному строковому параметру
@Injectable({
providedIn: 'root'
})
export class ProductService {
productList = PRODUCT_LIST;
constructor() { }
public getProductList(){
return of(this.productList);
}
public filterProductList(searchString: string): Promise<any>{
let dataObs: Observable<any>;
//dataObs = of(this.productList.filter( product => product.title.toLowerCase().indexOf(searchString) > -1));
//setTimeout(()=> { dataObs = of(this.productList.filter( product => product.title.toLowerCase().indexOf(searchString) > -1))},1000);
return of(this.productList.filter( product => product.title.toLowerCase().indexOf(searchString) > -1)).toPromise();
}
}
export const PRODUCT_LIST = [{
"title": "Brown eggs",
"type": "dairy",
"description": "Raw organic brown eggs in a basket",
"filename": "0.jpg",
"height": 600,
"width": 400,
"price": 28.1,
"rating": 4
}];