From 62f596c2bdae514c5e1e8406e829f0dd80577042 Mon Sep 17 00:00:00 2001
From: Steve Sanderson
Date: Fri, 27 Sep 2019 12:43:22 +0100
Subject: [PATCH] Actual event bubbling E2E tests
---
.../test/E2ETest/Tests/EventBubblingTest.cs | 90 +++++++++++++++++++
.../BasicTestApp/EventBubblingComponent.razor | 3 +-
2 files changed, 92 insertions(+), 1 deletion(-)
diff --git a/src/Components/test/E2ETest/Tests/EventBubblingTest.cs b/src/Components/test/E2ETest/Tests/EventBubblingTest.cs
index 36e92ffe7a..62af1fbf36 100644
--- a/src/Components/test/E2ETest/Tests/EventBubblingTest.cs
+++ b/src/Components/test/E2ETest/Tests/EventBubblingTest.cs
@@ -99,12 +99,102 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
Browser.Empty(GetLogLines);
}
+ [Theory]
+ [InlineData("target")]
+ [InlineData("intermediate")]
+ public void StopPropagation(string whereToStopPropagation)
+ {
+ // If stopPropagation is off, we observe the event on the listener and all its ancestors
+ Browser.FindElement(By.Id("button-with-onclick")).Click();
+ Browser.Equal(new[] { "target onclick", "parent onclick" }, GetLogLines);
+
+ // If stopPropagation is on, the event doesn't reach the ancestor
+ // Note that in the "intermediate element" case, the intermediate element does *not* itself
+ // listen for this event, which shows that stopPropagation works independently of handling
+ ClearLog();
+ Browser.FindElement(By.Id($"{whereToStopPropagation}-stop-propagation")).Click();
+ Browser.FindElement(By.Id("button-with-onclick")).Click();
+ Browser.Equal(new[] { "target onclick" }, GetLogLines);
+
+ // We can also toggle it back off
+ ClearLog();
+ Browser.FindElement(By.Id($"{whereToStopPropagation}-stop-propagation")).Click();
+ Browser.FindElement(By.Id("button-with-onclick")).Click();
+ Browser.Equal(new[] { "target onclick", "parent onclick" }, GetLogLines);
+ }
+
+ [Fact]
+ public void PreventDefaultWorksOnTarget()
+ {
+ // Clicking a checkbox without preventDefault produces both "click" and "change"
+ // events, and it becomes checked
+ var checkboxWithoutPreventDefault = Browser.FindElement(By.Id("checkbox-with-preventDefault-false"));
+ checkboxWithoutPreventDefault.Click();
+ Browser.Equal(new[] { "Checkbox click", "Checkbox change" }, GetLogLines);
+ Browser.True(() => checkboxWithoutPreventDefault.Selected);
+
+ // Clicking a checkbox with preventDefault produces a "click" event, but no "change"
+ // event, and it remains unchecked
+ ClearLog();
+ var checkboxWithPreventDefault = Browser.FindElement(By.Id("checkbox-with-preventDefault-true"));
+ checkboxWithPreventDefault.Click();
+ Browser.Equal(new[] { "Checkbox click" }, GetLogLines);
+ Browser.False(() => checkboxWithPreventDefault.Selected);
+ }
+
+ [Fact]
+ public void PreventDefault_WorksOnAncestorElement()
+ {
+ // Even though the checkbox we're clicking this case does *not* have preventDefault,
+ // if its ancestor does, then we don't get the "change" event and it remains unchecked
+ Browser.FindElement(By.Id($"ancestor-prevent-default")).Click();
+ var checkboxWithoutPreventDefault = Browser.FindElement(By.Id("checkbox-with-preventDefault-false"));
+ checkboxWithoutPreventDefault.Click();
+ Browser.Equal(new[] { "Checkbox click" }, GetLogLines);
+ Browser.False(() => checkboxWithoutPreventDefault.Selected);
+
+ // We can also toggle it back off dynamically
+ Browser.FindElement(By.Id($"ancestor-prevent-default")).Click();
+ ClearLog();
+ checkboxWithoutPreventDefault.Click();
+ Browser.Equal(new[] { "Checkbox click", "Checkbox change" }, GetLogLines);
+ Browser.True(() => checkboxWithoutPreventDefault.Selected);
+ }
+
+ [Fact]
+ public void PreventDefaultCanBlockKeystrokes()
+ {
+ // By default, the textbox accepts keystrokes
+ var textbox = Browser.FindElement(By.Id($"textbox-that-can-block-keystrokes"));
+ textbox.SendKeys("a");
+ Browser.Equal(new[] { "Received keydown" }, GetLogLines);
+ Browser.Equal("a", () => textbox.GetAttribute("value"));
+
+ // We can turn on preventDefault to stop keystrokes
+ // There will still be a keydown event, but we're preventing it from actually changing the textbox value
+ ClearLog();
+ Browser.FindElement(By.Id($"prevent-keydown")).Click();
+ textbox.SendKeys("b");
+ Browser.Equal(new[] { "Received keydown" }, GetLogLines);
+ Browser.Equal("a", () => textbox.GetAttribute("value"));
+
+ // We can turn it back off
+ ClearLog();
+ Browser.FindElement(By.Id($"prevent-keydown")).Click();
+ textbox.SendKeys("c");
+ Browser.Equal(new[] { "Received keydown" }, GetLogLines);
+ Browser.Equal("ac", () => textbox.GetAttribute("value"));
+ }
+
private string[] GetLogLines()
=> Browser.FindElement(By.TagName("textarea"))
.GetAttribute("value")
.Replace("\r\n", "\n")
.Split('\n', StringSplitOptions.RemoveEmptyEntries);
+ void ClearLog()
+ => Browser.FindElement(By.Id("clear-log")).Click();
+
private void TriggerCustomBubblingEvent(string elementId, string eventName)
{
var jsExecutor = (IJavaScriptExecutor)Browser;
diff --git a/src/Components/test/testassets/BasicTestApp/EventBubblingComponent.razor b/src/Components/test/testassets/BasicTestApp/EventBubblingComponent.razor
index 8d592ba201..90081be643 100644
--- a/src/Components/test/testassets/BasicTestApp/EventBubblingComponent.razor
+++ b/src/Components/test/testassets/BasicTestApp/EventBubblingComponent.razor
@@ -41,7 +41,7 @@
- Textbox that can block keystrokes: LogEvent("Received keydown"))" />
+ Textbox that can block keystrokes: LogEvent("Received keydown"))" />
@@ -75,6 +75,7 @@
Event log
+
@code {
bool intermediateStopPropagation;