diff --git a/packages/core/src/render3/view_engine_compatibility.ts b/packages/core/src/render3/view_engine_compatibility.ts index 26834e538f..fda2a4b0cf 100644 --- a/packages/core/src/render3/view_engine_compatibility.ts +++ b/packages/core/src/render3/view_engine_compatibility.ts @@ -281,9 +281,8 @@ export function createContainerRef( } indexOf(viewRef: viewEngine_ViewRef): number { - return this._lContainer[VIEW_REFS] !== null ? - this._lContainer[VIEW_REFS] !.indexOf(viewRef) : - 0; + const viewRefsArr = this._lContainer[VIEW_REFS]; + return viewRefsArr !== null ? viewRefsArr.indexOf(viewRef) : -1; } remove(index?: number): void { diff --git a/packages/core/test/acceptance/view_container_ref_spec.ts b/packages/core/test/acceptance/view_container_ref_spec.ts index b023d21a83..ade2e74f31 100644 --- a/packages/core/test/acceptance/view_container_ref_spec.ts +++ b/packages/core/test/acceptance/view_container_ref_spec.ts @@ -478,6 +478,24 @@ describe('ViewContainerRef', () => { vcRefDir.vcref.remove(0); expect(vcRefDir.vcref.indexOf(viewRef !)).toEqual(-1); }); + + it('should return -1 as indexOf when no views were inserted', () => { + const fixture = TestBed.createComponent(ViewContainerRefComp); + fixture.detectChanges(); + + const cmpt = fixture.componentInstance; + const viewRef = cmpt.templates.first.createEmbeddedView({}); + + // ViewContainerRef is empty and we've got a reference to a view that was not attached + // anywhere + expect(cmpt.vcr.indexOf(viewRef)).toBe(-1); + + cmpt.vcr.insert(viewRef); + expect(cmpt.vcr.indexOf(viewRef)).toBe(0); + + cmpt.vcr.remove(0); + expect(cmpt.vcr.indexOf(viewRef)).toBe(-1); + }); }); describe('move', () => {