Added CatImageService to get the image url

This commit is contained in:
Lorenz Hilpert
2019-01-30 16:03:33 +01:00
parent 8a85b87e58
commit 9988a7aa3e
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
import { CatImageService } from './cat-image.service';
import { TestBed } from '@angular/core/testing';
import { CatSearchMockService } from './cat-search-mock.service';
import { CatSearchService } from './cat-search.service';
import { of } from 'rxjs';
describe('CatImageService', () => {
let service: CatImageService;
let mockService: CatSearchMockService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
providers: [{ provide: CatSearchService, useClass: CatSearchMockService }]
});
service = TestBed.get(CatImageService);
mockService = TestBed.get(CatSearchService);
});
it('should create the service', () => {
expect(service).toBeDefined();
});
describe('#getImageUrl', () => {
it('should return an url', () => {
spyOn(mockService, 'settings').and.returnValue(of({
result: { imageUrl: 'https://produktbilder.ihugendubel.de/{ean}_150x150.jpg?showDummy=true' }
}));
service.getImageUrl('testean123').subscribe((url) => {
expect(url).toBe('https://produktbilder.ihugendubel.de/testean123_150x150.jpg?showDummy=true');
});
});
});
});

View File

@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { ItemDTO } from './dtos';
import { CatSearchService } from './cat-search.service';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
@Injectable({ providedIn: 'root' })
export class CatImageService {
constructor(private catSearch: CatSearchService) { }
getImageUrl(ean: string): Observable<string> {
return this.catSearch.settings().pipe(
map(response => response.result.imageUrl),
map(url => url.replace('{ean}', ean)),
catchError(() => of(''))
);
}
}