How can I do a simple angular Jasmine Unit testing when using RXJS reactive approach
Here is my simple implementation of the component. I have an items stream that gets some collection of items. Then I have a dropdown in HTML that calls the onSelectItem method which triggers the next() method of the selectedItemSubject. My goal is to test the $itemsWithSelected stream - to check if it returns the correct value - which is influenced by the selected Item and the items stream.
items$ = this.someService.getItems().pipe(
map((items) => {
return items;
})
);
private selectedItemSubject$ = new Subject<any>();
selectionItemAction$ = selectedItemSubject$.asObservable();
$itemsWithSelected = combineLatest([this.selectionItemAction$, items$]).pipe(
map(([selected, items]) => {
var targetItem = items.find(x => x.id === selected);
return someProcess(targetItem.someProperyOfThisItem);
}));
//some method that calls next of the subject
onSelectItem($event){
this.selectedItemSubject$.next($event.value);
}
To check a value, emitted by
$itemsWithSelected
, you can chain a pipe and tap the emitted value:You can also put a tap on the
$itemsWithSelected
observable itself, like so:note that after
map
, the values emitted by the stream would be of the type returned by thesomeProcess
which is unclear from the code (I marked it asTheTypeReturnedFromSomeProcessFunction
)