From 40869f8969dbbf24b7668f0284c6abbf40ce093c Mon Sep 17 00:00:00 2001 From: Mackinnon Buck Date: Wed, 12 Aug 2020 09:03:29 -0700 Subject: [PATCH] InputRadio validation styling fix (#24762) --- .../Web/src/Forms/InputRadioGroup.cs | 9 +++-- .../test/E2ETest/Tests/FormsTest.cs | 39 ++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Components/Web/src/Forms/InputRadioGroup.cs b/src/Components/Web/src/Forms/InputRadioGroup.cs index 0e30fd483e..661bd91e24 100644 --- a/src/Components/Web/src/Forms/InputRadioGroup.cs +++ b/src/Components/Web/src/Forms/InputRadioGroup.cs @@ -43,10 +43,11 @@ namespace Microsoft.AspNetCore.Components.Forms { Debug.Assert(_context != null); - builder.OpenComponent>(2); - builder.AddAttribute(3, "IsFixed", true); - builder.AddAttribute(4, "Value", _context); - builder.AddAttribute(5, "ChildContent", ChildContent); + builder.OpenComponent>(0); + builder.SetKey(_context); + builder.AddAttribute(1, "IsFixed", true); + builder.AddAttribute(2, "Value", _context); + builder.AddAttribute(3, "ChildContent", ChildContent); builder.CloseComponent(); } diff --git a/src/Components/test/E2ETest/Tests/FormsTest.cs b/src/Components/test/E2ETest/Tests/FormsTest.cs index c582ead49c..17b43feeef 100644 --- a/src/Components/test/E2ETest/Tests/FormsTest.cs +++ b/src/Components/test/E2ETest/Tests/FormsTest.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; @@ -305,9 +306,9 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests public void InputRadioGroupWithoutNameInteractsWithEditContext() { var appElement = MountTypicalValidationComponent(); - var airlineInputs = appElement.FindElement(By.ClassName("airline")).FindElements(By.TagName("input")); - var unknownAirlineInput = airlineInputs.First(i => i.GetAttribute("value").Equals("Unknown")); - var bestAirlineInput = airlineInputs.First(i => i.GetAttribute("value").Equals("BestAirline")); + var airlineInputs = FindAirlineInputs(appElement); + var unknownAirlineInput = FindUnknownAirlineInput(airlineInputs); + var bestAirlineInput = FindBestAirlineInput(airlineInputs); var messagesAccessor = CreateValidationMessagesAccessor(appElement); // Validate unselected inputs @@ -321,13 +322,28 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests // Validates on edit Assert.All(airlineInputs, i => Browser.Equal("valid", () => i.GetAttribute("class"))); + bestAirlineInput.Click(); + airlineInputs = FindAirlineInputs(appElement); + Assert.All(airlineInputs, i => Browser.Equal("modified valid", () => i.GetAttribute("class"))); // Can become invalid + unknownAirlineInput = FindUnknownAirlineInput(airlineInputs); unknownAirlineInput.Click(); + airlineInputs = FindAirlineInputs(appElement); + Assert.All(airlineInputs, i => Browser.Equal("modified invalid", () => i.GetAttribute("class"))); Browser.Equal(new[] { "Pick a valid airline." }, messagesAccessor); + + static IReadOnlyCollection FindAirlineInputs(IWebElement appElement) + => appElement.FindElement(By.ClassName("airline")).FindElements(By.TagName("input")); + + static IWebElement FindUnknownAirlineInput(IReadOnlyCollection airlineInputs) + => airlineInputs.First(i => i.GetAttribute("value").Equals("Unknown")); + + static IWebElement FindBestAirlineInput(IReadOnlyCollection airlineInputs) + => airlineInputs.First(i => i.GetAttribute("value").Equals("BestAirline")); } [Fact] @@ -336,8 +352,8 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests var appElement = MountTypicalValidationComponent(); var submitButton = appElement.FindElement(By.CssSelector("button[type=submit]")); var group = appElement.FindElement(By.ClassName("nested-radio-group")); - var countryInputs = group.FindElements(By.Name("country")); - var colorInputs = group.FindElements(By.Name("color")); + var countryInputs = FindCountryInputs(); + var colorInputs = FindColorInputs(); // Validate group counts Assert.Equal(3, countryInputs.Count); @@ -350,17 +366,30 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests // Invalidates on submit Assert.All(countryInputs, i => Browser.Equal("valid", () => i.GetAttribute("class"))); Assert.All(colorInputs, i => Browser.Equal("valid", () => i.GetAttribute("class"))); + submitButton.Click(); + countryInputs = FindCountryInputs(); + colorInputs = FindColorInputs(); + Assert.All(countryInputs, i => Browser.Equal("invalid", () => i.GetAttribute("class"))); Assert.All(colorInputs, i => Browser.Equal("invalid", () => i.GetAttribute("class"))); // Validates on edit countryInputs.First().Click(); + countryInputs = FindCountryInputs(); + colorInputs = FindColorInputs(); + Assert.All(countryInputs, i => Browser.Equal("modified valid", () => i.GetAttribute("class"))); Assert.All(colorInputs, i => Browser.Equal("invalid", () => i.GetAttribute("class"))); colorInputs.First().Click(); + colorInputs = FindColorInputs(); + Assert.All(colorInputs, i => Browser.Equal("modified valid", () => i.GetAttribute("class"))); + + IReadOnlyCollection FindCountryInputs() => group.FindElements(By.Name("country")); + + IReadOnlyCollection FindColorInputs() => group.FindElements(By.Name("color")); } [Fact]