Skip to content

Commit

Permalink
Maybe a solution to #118 - Attemting to add more waiting to the Dispa…
Browse files Browse the repository at this point in the history
…tchEventAsync method
  • Loading branch information
egil committed May 3, 2020
1 parent f4ac386 commit 0413ecf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ public Task<int> RenderFragment(RenderFragment renderFragment)
}

/// <inheritdoc/>
public new Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)
public new async Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)
{
if (fieldInfo is null)
throw new ArgumentNullException(nameof(fieldInfo));
_logger.LogDebug(new EventId(1, nameof(DispatchEventAsync)), $"Starting trigger of '{fieldInfo.FieldValue}'");

var task = Dispatcher.InvokeAsync(() =>
await Dispatcher.InvokeAsync(async () =>
{
try
{
return base.DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs);
await base.DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs);
}
catch (Exception e)
{
Expand All @@ -106,7 +106,6 @@ public Task<int> RenderFragment(RenderFragment renderFragment)
AssertNoUnhandledExceptions();

_logger.LogDebug(new EventId(1, nameof(DispatchEventAsync)), $"Finished trigger of '{fieldInfo.FieldValue}'");
return task;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ public void Test013()
expected.InnerException.ShouldBeOfType<InvalidOperationException>()
.Message.ShouldBe(expectedInnerMessage);
}

[Fact(DisplayName = "WaitForState can wait for multiple renders and changes to occur")]
public void Test100()
{
_testOutput.WriteLine($"INIT TEST100: {Thread.GetCurrentProcessorId()} - {Thread.CurrentThread.ManagedThreadId}");

// Initial state is stopped
var cut = RenderComponent<TwoRendersTwoChanges>();
var stateElement = cut.Find("#state");
Expand All @@ -170,8 +171,10 @@ public void Test100()
// This click causes two renders, thus something is needed to await here.
cut.Find("#tock").Click();
_testOutput.WriteLine($"BEFORE WAIT FOR STATE TEST100: {Thread.GetCurrentProcessorId()} - {Thread.CurrentThread.ManagedThreadId}");

cut.WaitForState(() => cut.Find("#state").TextContent == "Stopped");
_testOutput.WriteLine($"AFTER WAIT FOR STATE TEST100: {Thread.GetCurrentProcessorId()} - {Thread.CurrentThread.ManagedThreadId}");

cut.Find("#state").TextContent.ShouldBe("Stopped");
_testOutput.WriteLine($"END TEST100: {Thread.GetCurrentProcessorId()} - {Thread.CurrentThread.ManagedThreadId}");
}
Expand Down
4 changes: 4 additions & 0 deletions src/bunit.web/Rendering/Internal/Htmlizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace Bunit
{
/// <summary>
/// This file is based on
/// https://source.dot.net/#Microsoft.AspNetCore.Mvc.ViewFeatures/RazorComponents/HtmlRenderer.cs
/// </summary>
[SuppressMessage("Usage", "BL0006:Do not use RenderTree types", Justification = "<Pending>")]
internal class Htmlizer
{
Expand Down
3 changes: 3 additions & 0 deletions src/bunit.web/Rendering/RenderedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public string Markup
get
{
if (_latestRenderMarkup is null)
{
// TODO: Htmlizer can throw... should we handle that here?
_latestRenderMarkup = Htmlizer.GetHtml(Renderer, ComponentId);
}
return _latestRenderMarkup;
}
}
Expand Down

0 comments on commit 0413ecf

Please sign in to comment.