Skip to content

Commit

Permalink
fix(ivy): consistenly return -1 from ViewContainerRef.indexOf for non…
Browse files Browse the repository at this point in the history
…-inserted view (#34156)

PR Close #34156
  • Loading branch information
pkozlowski-opensource authored and mhevery committed Dec 4, 2019
1 parent 91f4b81 commit 0044c66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/render3/view_engine_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/acceptance/view_container_ref_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0044c66

Please sign in to comment.