From e459dc7c4221ea7ca040339292987a6928086da5 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 28 Sep 2020 18:17:57 +0300 Subject: [PATCH] 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 --- .../rx-library/src/retry-on-error.spec.ts | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/aio/content/examples/rx-library/src/retry-on-error.spec.ts b/aio/content/examples/rx-library/src/retry-on-error.spec.ts index 0dc48a4471..6638749ee7 100644 --- a/aio/content/examples/rx-library/src/retry-on-error.spec.ts +++ b/aio/content/examples/rx-library/src/retry-on-error.spec.ts @@ -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');