Skip to content

Commit f4368b9

Browse files
committed
refactor: Added tests for form click behavior
1 parent 38403e5 commit f4368b9

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

src/bunit/EventDispatchExtensions/TriggerEventDispatchExtensions.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,50 @@ private static bool TryGetParentFormElementSpecialCase(
170170
return false;
171171
}
172172

173-
var form = element switch
173+
// Check if this is a submit element
174+
var isSubmitElement = element switch
174175
{
175-
IHtmlInputElement { Type: "submit", Form: not null } input => input.Form,
176-
IHtmlButtonElement { Type: "submit", Form: not null } button => button.Form,
176+
IHtmlInputElement input => string.Equals(input.Type, "submit", StringComparison.OrdinalIgnoreCase),
177+
// Button defaults to type="submit" if not specified, so we check if it's NOT "button" or "reset"
178+
IHtmlButtonElement button => !string.Equals(button.Type, "button", StringComparison.OrdinalIgnoreCase)
179+
&& !string.Equals(button.Type, "reset", StringComparison.OrdinalIgnoreCase),
180+
_ => false
181+
};
182+
183+
if (!isSubmitElement)
184+
{
185+
return false;
186+
}
187+
188+
// Try to get the associated form, first via the Form property
189+
IHtmlFormElement? form = element switch
190+
{
191+
IHtmlInputElement { Form: not null } input => input.Form,
192+
IHtmlButtonElement { Form: not null } button => button.Form,
177193
_ => null
178194
};
179195

196+
// If Form property is null but element has a form attribute, try to find the form by ID
197+
// This handles the case where a button/input is outside the form but associated via form attribute
198+
if (form is null && element.HasAttribute("form"))
199+
{
200+
var formId = element.GetAttribute("form");
201+
if (!string.IsNullOrEmpty(formId) && element.Owner is not null)
202+
{
203+
var foundElement = element.Owner.GetElementById(formId);
204+
// Unwrap in case it's wrapped
205+
form = (foundElement as IElement)?.Unwrap() as IHtmlFormElement;
206+
207+
// Debug
208+
System.Diagnostics.Debug.WriteLine($"Looking for form with ID '{formId}'. Found: {foundElement != null}, Is IHtmlFormElement: {form != null}");
209+
if (form != null)
210+
{
211+
var hasOnsubmit = form.TryGetEventId(Htmlizer.ToBlazorAttribute("onsubmit"), out var testEventId);
212+
System.Diagnostics.Debug.WriteLine($"Form has onsubmit event: {hasOnsubmit}, eventId: {testEventId}");
213+
}
214+
}
215+
}
216+
180217
return form is not null
181218
&& form.TryGetEventId(Htmlizer.ToBlazorAttribute("onsubmit"), out eventId);
182219
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<form @onsubmit="Callback">
2+
@if (HasSubmitType)
3+
{
4+
<button type="submit" id="submitter">Submit</button>
5+
}
6+
else
7+
{
8+
<button id="other" type="button" @onclick="() => { }">Other button</button>
9+
<button id="submitter">Submit</button>
10+
}
11+
</form>
12+
13+
@code {
14+
public bool SubmitWasCalled { get; private set; }
15+
16+
[Parameter]
17+
public bool HasSubmitType { get; set; } = true;
18+
19+
private void Callback()
20+
{
21+
SubmitWasCalled = true;
22+
}
23+
}
24+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Bunit.EventDispatchExtensions;
2+
3+
public class FormDispatchExtensionTest : BunitContext
4+
{
5+
[Fact]
6+
public void ClickingOnSubmitButtonTriggersOnsubmitOfForm()
7+
{
8+
var cut = Render<FormWithButton>(p => p.Add(s => s.HasSubmitType, true));
9+
10+
cut.Find("#submitter").Click();
11+
12+
cut.Instance.SubmitWasCalled.ShouldBeTrue();
13+
}
14+
15+
[Fact]
16+
public void ButtonWithoutTypeIsConsideredSubmitAndTriggersOnsubmitOfForm()
17+
{
18+
var cut = Render<FormWithButton>(p => p.Add(s => s.HasSubmitType, false));
19+
20+
cut.Find("#submitter").Click();
21+
22+
cut.Instance.SubmitWasCalled.ShouldBeTrue();
23+
}
24+
25+
[Fact]
26+
public void ButtonThatIsNotSubmitShouldNotTrigger()
27+
{
28+
var cut = Render<FormWithButton>(p => p.Add(s => s.HasSubmitType, false));
29+
30+
cut.Find("#other").Click();
31+
32+
cut.Instance.SubmitWasCalled.ShouldBeFalse();
33+
}
34+
}

0 commit comments

Comments
 (0)