test(docs-infra): add missing test for the `rx-library` docs example (#38905)

This commit adds an extra test for the `retry-on-error` snippet of the
`rx-library` docs example to ensure it can successfully recover after a
couple of failed attempts.

This commit addresses comment
https://github.com/angular/angular/pull/38905#discussion_r491494196.

PR Close #38905
This commit is contained in:
George Kalpakas 2020-09-28 18:17:57 +03:00 committed by Alex Rickabaugh
parent fd7146cc71
commit e459dc7c42
1 changed files with 25 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import { of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { mergeMap, tap } from 'rxjs/operators';
import { docRegionDefault } from './retry-on-error';
describe('retry-on-error', () => {
@ -34,6 +34,30 @@ describe('retry-on-error', () => {
]);
});
it('should return the response if the request succeeds upon retrying', () => {
// Fail on the first two requests, but succeed from the 3rd onwards.
let failCount = 2;
const ajax = () => of(null).pipe(
tap(() => mockConsole.log('Subscribed to AJAX')),
// Fail on the first 2 requests, but succeed from the 3rd onwards.
mergeMap(() => {
if (failCount > 0) {
failCount--;
return throwError('Test error');
}
return of({ response: { foo: 'bar' } });
}),
);
docRegionDefault(mockConsole, ajax);
expect(mockConsole.log.calls.allArgs()).toEqual([
['Subscribed to AJAX'], // Initial request | 1st attempt overall
['Subscribed to AJAX'], // 1st retry attempt | 2nd attempt overall
['Subscribed to AJAX'], // 2nd retry attempt | 3rd attempt overall
['data: ', { foo: 'bar' }],
]);
});
it('should return an empty array when the ajax observable throws an error', () => {
const ajax = () => throwError('Test Error');