Skip to content

Instantly share code, notes, and snippets.

💭
💻 🐵

Catalin Ciubotaru FunnyGhost

💭
💻 🐵
Block or report user

Report or block FunnyGhost

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View favorite-movies.service.spec.ts
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { environment } from 'src/environments/environment';
import { Movie } from '../movie-list/core/models/movie';
import { FavoriteMoviesService } from './favorite-movies.service';
const moviesToUse = [
{ title: 'Interstellar' } as Movie,
{ title: 'The Green Book' } as Movie,
{ title: 'Dark Knight' } as Movie
View favorite-movies.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { retry } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { Movie } from '../movie-list/core/models/movie';
@Injectable()
export class FavoriteMoviesService {
constructor(private http: HttpClient) {}
View favorite-movies.service.spec.ts
it('should return the list of favorite movies if the backend returns an error 2 times and the succeds', () => {
let favoriteMovies: Movie[] = [];
serviceUnderTest.getFavoriteMovies().subscribe(data => {
favoriteMovies = data;
});
const req = http.expectOne(environment.favoriteUrl, 'expected to make an initial request');
expect(req.request.method).toEqual('GET');
req.flush('ERROR', { status: 500, statusText: 'Internal server error' });
View favorite-movies.service.ts
getFavoriteMovies(): Observable<Movie[]> {
return this.http.get<Movie[]>(environment.favoriteUrl).pipe(retry(2));
}
View favorite-movies.service.spec.ts
it('should fail if the backend returns an error 3 times in a row', done => {
let bubblesUpTheError = false;
serviceUnderTest.getFavoriteMovies().subscribe(() => {}, () => (bubblesUpTheError = true);
const req = http.expectOne(environment.favoriteUrl, 'expected to make an initial request');
expect(req.request.method).toEqual('GET');
req.flush('ERROR', { status: 500, statusText: 'Internal server error' });
const req2 = http.expectOne(environment.favoriteUrl, 'expected to make a second request');
expect(req2.request.method).toEqual('GET');
View favorite-movies.service.spec.ts
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();
View favorite-movies.service.ts
export class FavoriteMoviesService {
constructor(private http: HttpClient) {}
getFavoriteMovies(): Observable<Movie[]> {
return this.http.get<Movie[]>(environment.favoriteUrl);
}
}
View favorite-movies.service.spec.ts
describe('FavoriteMoviesService', () => {
let serviceUnderTest: FavoriteMoviesService;
let http: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [FavoriteMoviesService]
});
View environment.ts
const favoriteUrl = 'test.favorite.com';
export const environment = {
production: false,
favoriteUrl
};
View favorite-movies.service.ts
getFavoriteMovies(): Observable<Movie[]> {
return of([
{ title: 'Interstellar' } as Movie,
{ title: 'The Green Book' } as Movie,
{ title: 'Dark Knight' } as Movie
]);
}
You can’t perform that action at this time.