Unit testing an observable in Angular 2

The correct way for Angular (ver. 2+): it(‘retrieves all the cars’, waitForAsync(inject([CarService], (carService) => { carService.getCars().subscribe(result => expect(result.length).toBeGreaterThan(0)); })); Async Observables vs Sync Observables It is important to understand that Observables can be either synchronous or asynchronous. In your specific example the Observable is asynchronous (it wraps an http call). Therefore you have to use … Read more

fromPromise does not exist on type Observable

UPDATE: As of rxjs 6.0.0-beta.3, operators and observable creators should be imported from rxjs. Furthermore, fromPromise is not part of the public API anymore and its wrapped in the from method. TL;DR; UPDATE For rxjs 6.0.0 use: import { from } from ‘rxjs’; var observableFromPromise = from(promiseSrc); UPDATE: After the release of the pipeable operators … Read more

How can I create an observable with a delay

Using the following imports: import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; import ‘rxjs/add/operator/delay’; Try this: let fakeResponse = [1,2,3]; let delayedObservable = Observable.of(fakeResponse).delay(5000); delayedObservable.subscribe(data => console.log(data)); UPDATE: RXJS 6 The above solution doesn’t really work anymore in newer versions of RXJS (and of angular for example). So the scenario is that I have an array of … Read more

Get route query params

update (2.0.0 final) (somepath/:someparam/someotherpath) you can subscribe to them using _router.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private _router:Router) { _activatedRoute.queryParams.subscribe( params => console.log(‘queryParams’, params[‘st’])); original If you want queryParams instead of route params (somepath/:someparam/someotherpath) you can subscribe to them using _router.routerState.queryParams.subscribe(…): export class HeroComponent implements OnInit { constructor(private _activatedRoute: ActivatedRoute, private … Read more

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

In my case the error occurred only during e2e tests. It was caused by throwError in my AuthenticationInterceptor. I imported it from a wrong source because I used WebStorm’s import feature. I am using RxJS 6.2. Wrong: import { throwError } from ‘rxjs/internal/observable/throwError’; Correct: import { throwError } from ‘rxjs’; Here the full code of … Read more

How to parse xml in Angular 2

Based on the library http://goessner.net/download/prj/jsonxml/, I implemented a sample to receive XML data and parse them into an Angular2 application: var headers = new Headers(); (…) headers.append(‘Accept’, ‘application/xml’); return this.http.get(‘https://angular2.apispark.net/v1/companies/’, { headers: headers }).map(res => JSON.parse(xml2json(res.text(),’ ‘))); To be able to use the library, you need to parse first the XML string: var parseXml; if … Read more