fix(ivy): support inserting a viewRef that is already present (#34052)

When inserting a `viewRef` it is possible to not provide
an `index`, which is regarded as appending to the end of
the container.

If the `viewRef` already exists in the container, then
this results in a move. But there was a fault in the logic
that computed where to insert the `viewRef` that did not
account for the fact that the `viewRef` was already in
the container, so the insertion `index` was outside the
bounds of the array.

Fixes #33924

PR Close #34052
This commit is contained in:
Pete Bacon Darwin
2019-11-26 12:09:28 +00:00
committed by Matias Niemelä
parent b659aa32c9
commit 978b500961
2 changed files with 20 additions and 2 deletions

View File

@ -204,6 +204,22 @@ describe('ViewContainerRef', () => {
expect(fixture.nativeElement.textContent).toEqual('102');
}
});
it('should do nothing when a view is re-inserted / moved at the same index', () => {
const fixture = TestBed.createComponent(ViewContainerRefApp);
fixture.detectChanges();
const templates = fixture.componentInstance.vcrComp.templates.toArray();
const viewContainerRef = fixture.componentInstance.vcrComp.vcr;
const ref0 = viewContainerRef.createEmbeddedView(templates[0]);
expect(fixture.nativeElement.textContent).toEqual('0');
// insert again at the same place but without specifying any index
viewContainerRef.insert(ref0);
expect(fixture.nativeElement.textContent).toEqual('0');
});
});
describe('move', () => {