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
This commit is contained in:
George Kalpakas 2019-10-09 16:29:59 +03:00 committed by Miško Hevery
parent ebd557c1e1
commit 3001716a2f
1 changed files with 5 additions and 7 deletions

View File

@ -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<ElementsLoader> 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<void>((resolve, reject) =>
docViewer.swapViews(cb).subscribe(resolve, reject));
const doSwapViews = (cb?: () => void) => docViewer.swapViews(cb).toPromise();
beforeEach(() => {
oldCurrViewContainer = docViewer.currViewContainer;