Skip to content

Make WaitForNextRender method asynchronous #27

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

Merged
merged 3 commits into from
Jan 15, 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
35 changes: 33 additions & 2 deletions src/Components/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace Egil.RazorComponents.Testing
Expand All @@ -12,20 +13,29 @@ namespace Egil.RazorComponents.Testing
public class Fixture : FragmentBase
{
private Action _setup = NoopTestMethod;
private Func<Task> _setupAsync = NoopAsyncTestMethod;
private Action _test = NoopTestMethod;
private Func<Task> _testAsync = NoopAsyncTestMethod;
private IReadOnlyCollection<Action> _tests = Array.Empty<Action>();
private IReadOnlyCollection<Func<Task>> _testsAsync = Array.Empty<Func<Task>>();

/// <summary>
/// A description or name for the test that will be displayed if the test fails.
/// </summary>
[Parameter] public string? Description { get; set; }

/// <summary>
/// Gets or sets the setup action to perform before the <see cref="Test"/> action
/// and <see cref="Tests"/> actions are invoked.
/// Gets or sets the setup action to perform before the <see cref="Test"/> action,
/// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked.
/// </summary>
[Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; }

/// <summary>
/// Gets or sets the asynchronous setup action to perform before the <see cref="Test"/> action,
/// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked.
/// </summary>
[Parameter] public Func<Task> SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopAsyncTestMethod; }

/// <summary>
/// Gets or sets the first test action to invoke, after the <see cref="Setup"/> action has
/// executed (if provided).
Expand All @@ -35,6 +45,15 @@ public class Fixture : FragmentBase
/// </summary>
[Parameter] public Action Test { get => _test; set => _test = value ?? NoopTestMethod; }

/// <summary>
/// Gets or sets the first test action to invoke, after the <see cref="SetupAsync"/> action has
/// executed (if provided).
///
/// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s
/// defined in the <see cref="Fixture"/>.
/// </summary>
[Parameter] public Func<Task> TestAsync { get => _testAsync; set => _testAsync = value ?? NoopAsyncTestMethod; }

/// <summary>
/// Gets or sets the test actions to invoke, one at the time, in the order they are placed
/// into the collection, after the <see cref="Setup"/> action and the <see cref="Test"/> action has
Expand All @@ -45,6 +64,18 @@ public class Fixture : FragmentBase
/// </summary>
[Parameter] public IReadOnlyCollection<Action> Tests { get => _tests; set => _tests = value ?? Array.Empty<Action>(); }

/// <summary>
/// Gets or sets the test actions to invoke, one at the time, in the order they are placed
/// into the collection, after the <see cref="SetupAsync"/> action and the <see cref="TestAsync"/> action has
/// executed (if provided).
///
/// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s
/// defined in the <see cref="Fixture"/>.
/// </summary>
[Parameter] public IReadOnlyCollection<Func<Task>> TestsAsync { get => _testsAsync; set => _testsAsync = value ?? Array.Empty<Func<Task>>(); }

private static void NoopTestMethod() { }

private static Task NoopAsyncTestMethod() => Task.CompletedTask;
}
}
26 changes: 23 additions & 3 deletions src/Components/TestComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Egil.RazorComponents.Testing.Asserting;
using Egil.RazorComponents.Testing.Diffing;
using Microsoft.AspNetCore.Components;
Expand Down Expand Up @@ -52,12 +53,12 @@ public TestComponentBase()
/// in the file and runs their associated tests.
/// </summary>
[Fact(DisplayName = "Razor test runner")]
public void RazorTest()
public async Task RazorTest()
{
var container = new ContainerComponent(_renderer.Value);
container.Render(BuildRenderTree);

ExecuteFixtureTests(container);
await ExecuteFixtureTests(container).ConfigureAwait(false);
ExecuteSnapshotTests(container);
}

Expand Down Expand Up @@ -92,7 +93,7 @@ public override void WaitForNextRender(Action renderTrigger, TimeSpan? timeout =
base.WaitForNextRender(renderTrigger, timeout);
}

private void ExecuteFixtureTests(ContainerComponent container)
private async Task ExecuteFixtureTests(ContainerComponent container)
{
foreach (var (_, fixture) in container.GetComponents<Fixture>())
{
Expand All @@ -102,13 +103,20 @@ private void ExecuteFixtureTests(ContainerComponent container)
_testContextAdapter.ActivateRazorTestContext(testData);

InvokeFixtureAction(fixture, fixture.Setup);
await InvokeFixtureAction(fixture, fixture.SetupAsync).ConfigureAwait(false);
InvokeFixtureAction(fixture, fixture.Test);
await InvokeFixtureAction(fixture, fixture.TestAsync).ConfigureAwait(false);

foreach (var test in fixture.Tests)
{
InvokeFixtureAction(fixture, test);
}

foreach (var test in fixture.TestsAsync)
{
await InvokeFixtureAction(fixture, test).ConfigureAwait(false);
}

_testContextAdapter.DisposeActiveTestContext();
}
}
Expand All @@ -125,6 +133,18 @@ private static void InvokeFixtureAction(Fixture fixture, Action action)
}
}

private static async Task InvokeFixtureAction(Fixture fixture, Func<Task> action)
{
try
{
await action().ConfigureAwait(false);
}
catch (Exception ex)
{
throw new FixtureFailedException(fixture.Description ?? $"{action.Method.Name} failed:", ex);
}
}

private void ExecuteSnapshotTests(ContainerComponent container)
{
foreach (var (_, snapshot) in container.GetComponents<SnapshotTest>())
Expand Down