[英]How to test Subject with jasmine marbles
Angular 6、Rxjs、Jest、茉莉花大理石。
非常常見的場景:搜索服務器端項目的組件。 在組件中,有一些控件可以更改搜索條件,我想以“反應式”進行編碼。 所以在組件代碼中我有這樣的東西:
class SearchComponent implements OnInit {
public searchData: SearchData = {};
public searchConditions$ = new Subject<SearchData>();
constructor(private searchService: SearchService) { }
public results$: Observable<ResultData> = this.searchConditions$.pipe(
distinctUntilChanged(this.compareProperties), // omissis but it works
flatMap(searchData => this.searchService.search(searchData)),
shareReplay(1)
);
// search actions
ngOnInit() {
this.searchConditions$.next(this.searchData);
}
public onChangeProp1(prop1: string) {
this.searchData = { ...this.searchData, prop1 };
this.searchConditions$.next(this.searchData);
}
public onChangeProp2(prop2: string) {
this.searchData = { ...this.searchData, prop2 };
this.searchConditions$.next(this.searchData);
}
}
也就是說,每次 UI 中的某些內容發生更改時都會觸發搜索條件的Subject
。
現在我想測試僅針對不同的輸入調用搜索服務。 我可以通過這種方式“沒有彈珠”做到這一點:
test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
service.search = jest.fn(() => of(TestData.results));
component.results$.subscribe({
complete: () => {
expect(service.Search).toHaveBeenCalledTimes(1);
done();
}
});
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.complete();
});
現在我想使用茉莉花彈珠轉換此測試,但我不知道如何...
我想要這樣的東西:
test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
service.search = jest.fn(() => of(TestData.results));
component.searchConditions$ = cold('--a--a|', { a : TestData.searchCriteria});
const expected = cold('--b---|', { b : TestData.results});
expect(component.results$).toBeObservable(expected);
});
顯然,它不起作用......
以某種方式關閉......使用“測試助手”
test('when searchConditions$ comes with equal events search service will not be called more than once - marble version', () => {
service.search = jest.fn(() => of(TestData.results));
const stream = cold('--a--a|', { a : TestData.searchCriteria});
const expected = cold('--b---|', { b : TestData.results});
stubSubject(component.searchConditions$, stream);
expect(component.results$).toBeObservable(expected);
});
// test helpers
const stubSubject = (subject: Subject<any> , marbles: TestObservable) => {
marbles.subscribe({
next: (value: any) => subject.next(value),
complete: () => subject.complete(),
error: (e) => subject.error(e)
});
};
測試的主要目標是模擬依賴項並且不更改測試單元 SearchComponent 內部的任何內容。
因此stubSubject(component.searchConditions$, stream)
或component.searchConditions$ = cold
是一個不好的做法。
因為我們想在searchConditions$
調度發射, searchConditions$
我們需要有一個內部調度流,我們也可以在這里使用cold
或hot
。
源數據(對不起,我猜到了一些類型)
type SearchData = {
prop?: string;
};
type ResultData = Array<string>;
@Injectable()
class SearchService {
public search(term: SearchData): Observable<any> {
return of();
}
}
@Component({
selector: 'search',
template: '',
})
class SearchComponent implements OnInit {
public searchData: SearchData = {};
public searchConditions$ = new Subject<SearchData>();
constructor(private searchService: SearchService) { }
public results$: Observable<ResultData> = this.searchConditions$.pipe(
distinctUntilKeyChanged('prop'),
tap(console.log),
flatMap(searchData => this.searchService.search(searchData)),
shareReplay(1),
);
// search actions
ngOnInit() {
this.searchConditions$.next(this.searchData);
}
public onChangeProp1(prop1: string) {
this.searchData = { ...this.searchData, prop: prop1 }; // I've changed it to prop
this.searchConditions$.next(this.searchData);
}
public onChangeProp2(prop2: string) {
this.searchData = { ...this.searchData, prop: prop2 }; // I've changed it to prop
this.searchConditions$.next(this.searchData);
}
}
和它的測試
test('when searchConditions$ come with equal events search service will not be called more than once', () => {
service.search = jest.fn(() => of(TestData.results));
// our internal scheduler how we click our component.
// the first delay `-` is important to allow `expect` to subscribe.
cold('-a--a--b--b--a-|', {
a: 'a',
b: 'b',
}).pipe(
tap(v => component.searchConditions$.next({prop: v})),
// or like user does it
// tap(v => component.onChangeProp1(v)),
finalize(() => component.searchConditions$.complete()),
).subscribe();
// adding our expectations.
expect(component.results$).toBeObservable(cold('-r-----r-----r-|', {
r: TestData.results,
}));
});
現在我們不改變我們的測試單元,只改變它的依賴項( service.search
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.