Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event trigger extension method for @ontoggle #256

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The following section list all changes in 1.0.0 preview 01.
### Added
List of new features.

- Added support for triggering `@ontoggle` event handlers through a dedicated `Toggle()` method. By [@egil](https://github.com/egil) in [#248](https://github.com/egil/bUnit/pull/248).

### Changed
List of changes in existing functionality.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if NET5_0
using System;
using System.Threading.Tasks;
using AngleSharp.Dom;

namespace Bunit
{
/// <summary>
/// General event dispatch helper extension methods.
/// </summary>
public static class DetailsElementEventDispatcherExtensions
{
/// <summary>
/// Raises the <c>@ontoggle</c> event on <paramref name="element"/>
/// to the event handler.
/// </summary>
/// <param name="element">The element to raise the event on.</param>
public static void Toggle(this IElement element)
=> _ = ToggleAsync(element, EventArgs.Empty);

/// <summary>
/// Raises the <c>@ontoggle</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
/// to the event handler.
/// </summary>
/// <param name="element">The element to raise the event on.</param>
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
/// <returns>A task that completes when the event handler is done.</returns>
private static Task ToggleAsync(this IElement element, EventArgs eventArgs) => element.TriggerEventAsync("ontoggle", eventArgs);
}
}
#endif
19 changes: 19 additions & 0 deletions tests/bunit.testassets/SampleComponents/ToggleableDetails.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div>
@if (detailsExpanded)
{
<p>Read the details carefully!</p>
}
<details id="details-toggle" @ontoggle="OnToggle">
<summary>Summary</summary>
<p>Detailed content</p>
</details>
</div>

@code {
bool detailsExpanded;

void OnToggle()
{
detailsExpanded = !detailsExpanded;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if NET5_0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bunit;
using Bunit.TestAssets.SampleComponents;
using Shouldly;
using Xunit;

namespace Bunit.EventDispatchExtensions
{
public class DetailsElementEventDispatcherExtensionsTest : TestContext
{
[Fact(DisplayName = "Toggle raises ontoggle events")]
public void Test200()
{
var cut = RenderComponent<ToggleableDetails>();
cut.FindAll("div > p").Count.ShouldBe(0);

cut.Find("details").Toggle();

cut.FindAll("div > p").Count.ShouldBe(1);
}
}
}
#endif