From 3001716a2f05b04b591a6119118a7e17d245c4de Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Wed, 9 Oct 2019 16:29:59 +0300 Subject: [PATCH] test(docs-infra): ensure spy returns new observable every time (#32980) Previously, some spies in `DovViewerComponent` tests would return the same `of(undefined)` observable for all invocations of the spy in a test. While there is usually only one invocation per spy in each test, this is not always the case. In case of multiple invocations within the same test, subsequent calls would return an already completed observable, which deviates from the actual behavior of the spied function. This commit fixes it by ensuring a fresh `of(undefined)` observable is returned on each invocation. PR Close #32980 --- .../layout/doc-viewer/doc-viewer.component.spec.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts b/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts index ef241b1b2b..905129b43e 100644 --- a/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts +++ b/aio/src/app/layout/doc-viewer/doc-viewer.component.spec.ts @@ -47,7 +47,7 @@ describe('DocViewerComponent', () => { parentFixture.detectChanges(); }; - beforeEach(() => renderSpy = spyOn(docViewer, 'render').and.returnValue(of(undefined))); + beforeEach(() => renderSpy = spyOn(docViewer, 'render').and.callFake(() => of(undefined))); it('should render the new document', () => { setCurrentDoc('foo', 'bar'); @@ -87,7 +87,7 @@ describe('DocViewerComponent', () => { describe('#ngOnDestroy()', () => { it('should stop responding to document changes', () => { - const renderSpy = spyOn(docViewer, 'render').and.returnValue(of(undefined)); + const renderSpy = spyOn(docViewer, 'render').and.callFake(() => of(undefined)); expect(renderSpy).not.toHaveBeenCalled(); @@ -300,9 +300,9 @@ describe('DocViewerComponent', () => { beforeEach(() => { const elementsLoader = TestBed.inject(ElementsLoader) as Partial as MockElementsLoader; - loadElementsSpy = elementsLoader.loadContainedCustomElements.and.returnValue(of(undefined)); + loadElementsSpy = elementsLoader.loadContainedCustomElements.and.callFake(() => of(undefined)); prepareTitleAndTocSpy = spyOn(docViewer, 'prepareTitleAndToc'); - swapViewsSpy = spyOn(docViewer, 'swapViews').and.returnValue(of(undefined)); + swapViewsSpy = spyOn(docViewer, 'swapViews').and.callFake(() => of(undefined)); }); it('should return an `Observable`', () => { @@ -571,9 +571,7 @@ describe('DocViewerComponent', () => { let oldCurrViewContainer: HTMLElement; let oldNextViewContainer: HTMLElement; - const doSwapViews = (cb?: () => void) => - new Promise((resolve, reject) => - docViewer.swapViews(cb).subscribe(resolve, reject)); + const doSwapViews = (cb?: () => void) => docViewer.swapViews(cb).toPromise(); beforeEach(() => { oldCurrViewContainer = docViewer.currViewContainer;