fix(ivy): consistenly return -1 from ViewContainerRef.indexOf for non-inserted view (#34156)

PR Close #34156
This commit is contained in:
Pawel Kozlowski 2019-11-29 22:29:00 +01:00 committed by Miško Hevery
parent bd820fdf84
commit 2cf25facd9
2 changed files with 20 additions and 3 deletions

View File

@ -281,9 +281,8 @@ export function createContainerRef(
} }
indexOf(viewRef: viewEngine_ViewRef): number { indexOf(viewRef: viewEngine_ViewRef): number {
return this._lContainer[VIEW_REFS] !== null ? const viewRefsArr = this._lContainer[VIEW_REFS];
this._lContainer[VIEW_REFS] !.indexOf(viewRef) : return viewRefsArr !== null ? viewRefsArr.indexOf(viewRef) : -1;
0;
} }
remove(index?: number): void { remove(index?: number): void {

View File

@ -478,6 +478,24 @@ describe('ViewContainerRef', () => {
vcRefDir.vcref.remove(0); vcRefDir.vcref.remove(0);
expect(vcRefDir.vcref.indexOf(viewRef !)).toEqual(-1); 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', () => { describe('move', () => {