This file contains hidden or 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 articleNotifier = new Subject(); | |
| @Injectable() | |
| export class ArticleService { | |
| constructor(private _http: HttpClient) {} | |
| @Cacheable({ | |
| cacheBusterObserver: articleNotifier | |
| }) | |
| getArticles() { | |
| return this._http(); |
This file contains hidden or 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
| beforeEach(() => { | |
| jasmine.clock().install(); | |
| }); | |
| afterEach(() => { | |
| jasmine.clock().uninstall(); | |
| }); |
This file contains hidden or 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 correct data', () => { | |
| const data = awaitStream(component.getData(), 500); | |
| expect(data).toEqual([1,2,3,4]) | |
| }) |
This file contains hidden or 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
| 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 | |
| */ |
This file contains hidden or 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 correct data', (done) => { | |
| component.getData().subscribe(data=> | |
| expect(data).toEqual([1,2,3,4]) | |
| done(); | |
| ) | |
| }) |
This file contains hidden or 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 correct data', () => { | |
| component.getData().subscribe(data=> | |
| expect(data).toEqual([1,2,3,4]) | |
| ) | |
| }) |
This file contains hidden or 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
| getData(){ | |
| return of([1,2,3,4]).pipe(delay(500)); | |
| } |
This file contains hidden or 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
| @CacheBuster({ | |
| cacheBusterNotifier: productNotifier | |
| }) | |
| saveProduct(id: string, name: string, review: string) { | |
| return this.http.put(`${environment.api}/products`, { | |
| id, | |
| name, | |
| review | |
| }); |
This file contains hidden or 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
| @Cacheable({ | |
| cacheBusterObserver: productNotifier | |
| }) | |
| getProducts(take?: number, skip?: number) { | |
| return this.http.get( | |
| `${environment.api}/products/latest?take=${take}&skip=${skip}` | |
| ); | |
| } |
This file contains hidden or 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
| getProducts(take?: number, skip?: number) { | |
| return this.http.get( | |
| `${environment.api}/products/latest?take=${take}&skip=${skip}` | |
| ); | |
| } |