This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('should throw if the backend returns an error ', () => { | |
let bubblesUpTheError = false; | |
serviceUnderTest.getFavoriteMovies().subscribe(() => {}, () => (bubblesUpTheError = true)); | |
const req = http.expectOne(environment.favoriteUrl, 'expected to make a request'); | |
expect(req.request.method).toEqual('GET'); | |
req.flush('ERROR', { status: 500, statusText: 'Internal server error' }); | |
expect(bubblesUpTheError).toBeTruthy(); | |
http.verify(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FavoriteMoviesService { | |
constructor(private http: HttpClient) {} | |
getFavoriteMovies(): Observable<Movie[]> { | |
return this.http.get<Movie[]>(environment.favoriteUrl); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('FavoriteMoviesService', () => { | |
let serviceUnderTest: FavoriteMoviesService; | |
let http: HttpTestingController; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
imports: [HttpClientTestingModule], | |
providers: [FavoriteMoviesService] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const favoriteUrl = 'test.favorite.com'; | |
export const environment = { | |
production: false, | |
favoriteUrl | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getFavoriteMovies(): Observable<Movie[]> { | |
return of([ | |
{ title: 'Interstellar' } as Movie, | |
{ title: 'The Green Book' } as Movie, | |
{ title: 'Dark Knight' } as Movie | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('should return the favorite movies', () => { | |
let result: Movie[] = []; | |
serviceUnderTest.getFavoriteMovies().subscribe(data => { | |
result = data; | |
}); | |
expect(result).toEqual(moviesToUse); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const moviesToUse = [ | |
{ title: 'Interstellar' } as Movie, | |
{ title: 'The Green Book' } as Movie, | |
{ title: 'Dark Knight' } as Movie | |
]; | |
describe('FavoriteMoviesService', () => { | |
let serviceUnderTest: FavoriteMoviesService; | |
beforeEach(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FavoriteMoviesService { | |
constructor() {} | |
getFavoriteMovies(): Observable<Movie[]> { | |
return of([]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="error" *ngIf="error"> | |
{{ error }} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.favoriteMovies$ = this.favoriteMovieService.getFavoriteMovies().pipe( | |
catchError((error: any) => { | |
this.error = error; | |
return of([]); | |
}) | |
); | |
} |