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
it('should return the favorite movies', () => {
let result: Movie[] = [];
serviceUnderTest.getFavoriteMovies().subscribe(data => {
result = data;
});
expect(result).toEqual(moviesToUse);
});
View favorite-movies.service.spec.ts
const moviesToUse = [
{ title: 'Interstellar' } as Movie,
{ title: 'The Green Book' } as Movie,
{ title: 'Dark Knight' } as Movie
];
describe('FavoriteMoviesService', () => {
let serviceUnderTest: FavoriteMoviesService;
beforeEach(() => {
@FunnyGhost
FunnyGhost / favorite-movies.service.ts
Created Sep 12, 2019
favorite movies service initial state
View favorite-movies.service.ts
export class FavoriteMoviesService {
constructor() {}
getFavoriteMovies(): Observable<Movie[]> {
return of([]);
}
}
View favorite-movies.component.html
<div class="error" *ngIf="error">
{{ error }}
</div>
View favorite-movies.component.ts
ngOnInit() {
this.favoriteMovies$ = this.favoriteMovieService.getFavoriteMovies().pipe(
catchError((error: any) => {
this.error = error;
return of([]);
})
);
}
View favorite-movies.component.spec.ts
it('should show an error if getting the movies fail', () => {
const errorToThrow = 'User not found';
jest
.spyOn(favoriteMovieService, 'getFavoriteMovies')
.mockReturnValue(throwError(errorToThrow));
fixture.detectChanges();
const errorElement = fixture.debugElement.queryAll(By.css('.error'));
expect(errorElement.length).toBe(1);
View favorite-movies.component.html
<ng-container *ngIf="(favoriteMovies$ | async); let favoriteMovies">
<div class="movie" *ngFor="let movie of favoriteMovies">
{{ movie.title }}
</div>
</ng-container>
View favorite-movies.component.ts
export class FavoriteMoviesComponent implements OnInit {
favoriteMovies$: Observable<Movie[]>;
constructor(private favoriteMovieService: FavoriteMoviesService) {}
ngOnInit() {
this.favoriteMovies$ = this.favoriteMovieService.getFavoriteMovies();
}
}
View favorite-movies.component.spec.ts
describe('FavoriteMoviesComponent', () => {
let component: FavoriteMoviesComponent;
let fixture: ComponentFixture<FavoriteMoviesComponent>;
let favoriteMovieService: FavoriteMoviesService;
beforeEach(() => {
fixture = TestBed.createComponent(FavoriteMoviesComponent);
component = fixture.componentInstance;
favoriteMovieService = TestBed.get(FavoriteMoviesService);
jest.spyOn(favoriteMovieService, 'getFavoriteMovies').mockReturnValue(of(favoriteMoviesToUse));
View favorite-movies.component.html
<h1>Favorite movies</h1>
<ng-container *ngIf="(favoriteMovies$ | async); let favoriteMovies">
<div class="movie" *ngFor="let movie of favoriteMovies">
{{ movie.title }}
</div>
</ng-container>
<div class="error" *ngIf="error">
{{ error }}
You can’t perform that action at this time.