Skip to content

Instantly share code, notes, and snippets.

View angelnikolov's full-sized avatar

Angel Nikolov angelnikolov

View GitHub Profile
const articleNotifier = new Subject();
@Injectable()
export class ArticleService {
constructor(private _http: HttpClient) {}
@Cacheable({
cacheBusterObserver: articleNotifier
})
getArticles() {
return this._http();
beforeEach(() => {
jasmine.clock().install();
});
afterEach(() => {
jasmine.clock().uninstall();
});
it('should return correct data', () => {
const data = awaitStream(component.getData(), 500);
expect(data).toEqual([1,2,3,4])
})
function awaitStream(stream$: Observable<any>, skipTime?: number) {
let response = null;
stream$.subscribe(data => {
response = data;
});
if (skipTime) {
/**
* use jasmine clock to artificially manipulate time-based web apis like setTimeout and setInterval
* we can easily refactor this and use async/await but that means that we will have to actually wait out the time needed for every delay/mock request
*/
it('should return correct data', (done) => {
component.getData().subscribe(data=>
expect(data).toEqual([1,2,3,4])
done();
)
})
it('should return correct data', () => {
component.getData().subscribe(data=>
expect(data).toEqual([1,2,3,4])
)
})
getData(){
return of([1,2,3,4]).pipe(delay(500));
}
@CacheBuster({
cacheBusterNotifier: productNotifier
})
saveProduct(id: string, name: string, review: string) {
return this.http.put(`${environment.api}/products`, {
id,
name,
review
});
@Cacheable({
cacheBusterObserver: productNotifier
})
getProducts(take?: number, skip?: number) {
return this.http.get(
`${environment.api}/products/latest?take=${take}&skip=${skip}`
);
}
getProducts(take?: number, skip?: number) {
return this.http.get(
`${environment.api}/products/latest?take=${take}&skip=${skip}`
);
}