Hi,
I am not good at unit testing but willing to learn. The following is my
service. Basically, there are 3 JSON files a,b,c. It should return a if it
exists, else b. if b is not found then c, which is always avaliable (or is
default).
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Rx";
@Injectable()
export class DataService {
constructor(
public http: HttpClient
) {}
public getDataJSON(): Observable<any> {
/*
returns JSON file. Return contents of a.json if it exists
else b.json. if b.json is not found, return c.json
*/
let vm = this;
return new Observable(observer => {
vm.http
.get(
"/assets/a.json"
)
.subscribe(
data => {
console.log("a.json found");
observer.next(data);
observer.complete();
},
error => {
// is it missing error?
console.log(
"error or missing a.json ",
error
);
if (error.status == 404) {
// see if b.json exists
vm.http
.get(
"/assets/b.json"
)
.subscribe(
data => {
console.log("use b.json");
observer.next(data);
observer.complete();
},
error => {
console.log(
"error with b.json or missing ",
error
);
vm.http
.get(
"/assets/c.json"
)
.subscribe(
data => {
console.log("using default c.json ", data);
observer.next(data);
observer.complete();
},
error => {
console.log("we cant find any data");
observer.next(null);
observer.complete();
}
);
}
);
}
}
);
});
}
}
I want to make sure that the correct json file is picked but really lost on
how to do a test for this kind of thing.
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.