diff --git a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs index 563f86f010..ea1a15bf83 100644 --- a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs @@ -128,6 +128,26 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests Assert.Equal("1", messageElementInChild.Text); } + [Fact] + public void CanAddAndRemoveChildComponentsDynamically() + { + // Initially there are zero child components + var appElement = MountTestComponent(); + var addButton = appElement.FindElement(By.ClassName("addChild")); + var removeButton = appElement.FindElement(By.ClassName("removeChild")); + Func> childComponentWrappers = () => appElement.FindElements(By.TagName("p")); + Assert.Empty(childComponentWrappers()); + + // Click to add some child components + addButton.Click(); + addButton.Click(); + removeButton.Click(); + addButton.Click(); + Assert.Collection(childComponentWrappers(), + elem => Assert.Equal("Child 1", elem.FindElement(By.ClassName("message")).Text), + elem => Assert.Equal("Child 3", elem.FindElement(By.ClassName("message")).Text)); + } + private IWebElement MountTestComponent() where TComponent: IComponent { var componentTypeName = typeof(TComponent).FullName; diff --git a/test/testapps/BasicTestApp/AddRemoveChildComponents.cshtml b/test/testapps/BasicTestApp/AddRemoveChildComponents.cshtml new file mode 100644 index 0000000000..0f82b31c41 --- /dev/null +++ b/test/testapps/BasicTestApp/AddRemoveChildComponents.cshtml @@ -0,0 +1,28 @@ +@using System.Collections.Generic +Child components follow. + + + +@foreach (var message in currentChildrenMessages) +{ +

+} + +@functions { + int numAdded = 0; + List currentChildrenMessages = new List(); + + void AddChild() + { + numAdded++; + currentChildrenMessages.Add($"Child {numAdded}"); + } + + void RemoveChild() + { + if (currentChildrenMessages.Count > 0) + { + currentChildrenMessages.RemoveAt(currentChildrenMessages.Count - 1); + } + } +}