Skip to content

Commit ea034d1

Browse files
committed
Added async setup method to snapshot test
1 parent 6d57f19 commit ea034d1

5 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/Components/Fixture.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace Egil.RazorComponents.Testing
1313
public class Fixture : FragmentBase
1414
{
1515
private Action _setup = NoopTestMethod;
16-
private Func<Task> _setupAsync = NoopAsyncTestMethod;
16+
private Func<Task> _setupAsync = NoopTestMethodAsync;
1717
private Action _test = NoopTestMethod;
18-
private Func<Task> _testAsync = NoopAsyncTestMethod;
18+
private Func<Task> _testAsync = NoopTestMethodAsync;
1919
private IReadOnlyCollection<Action> _tests = Array.Empty<Action>();
2020
private IReadOnlyCollection<Func<Task>> _testsAsync = Array.Empty<Func<Task>>();
2121

@@ -34,7 +34,7 @@ public class Fixture : FragmentBase
3434
/// Gets or sets the asynchronous setup action to perform before the <see cref="Test"/> action,
3535
/// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked.
3636
/// </summary>
37-
[Parameter] public Func<Task> SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopAsyncTestMethod; }
37+
[Parameter] public Func<Task> SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopTestMethodAsync; }
3838

3939
/// <summary>
4040
/// Gets or sets the first test action to invoke, after the <see cref="Setup"/> action has
@@ -52,7 +52,7 @@ public class Fixture : FragmentBase
5252
/// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s
5353
/// defined in the <see cref="Fixture"/>.
5454
/// </summary>
55-
[Parameter] public Func<Task> TestAsync { get => _testAsync; set => _testAsync = value ?? NoopAsyncTestMethod; }
55+
[Parameter] public Func<Task> TestAsync { get => _testAsync; set => _testAsync = value ?? NoopTestMethodAsync; }
5656

5757
/// <summary>
5858
/// Gets or sets the test actions to invoke, one at the time, in the order they are placed
@@ -73,9 +73,5 @@ public class Fixture : FragmentBase
7373
/// defined in the <see cref="Fixture"/>.
7474
/// </summary>
7575
[Parameter] public IReadOnlyCollection<Func<Task>> TestsAsync { get => _testsAsync; set => _testsAsync = value ?? Array.Empty<Func<Task>>(); }
76-
77-
private static void NoopTestMethod() { }
78-
79-
private static Task NoopAsyncTestMethod() => Task.CompletedTask;
8076
}
8177
}

src/Components/FragmentBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Egil.RazorComponents.Testing
99
/// </summary>
1010
public abstract class FragmentBase : IComponent
1111
{
12+
internal static void NoopTestMethod() { }
13+
internal static Task NoopTestMethodAsync() => Task.CompletedTask;
14+
1215
/// <summary>
1316
/// Gets or sets the child content of the fragment.
1417
/// </summary>

src/Components/SnapshotTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@ namespace Egil.RazorComponents.Testing
1616
public class SnapshotTest : FragmentBase
1717
{
1818
private Action _setup = NoopTestMethod;
19+
private Func<Task> _setupAsync = NoopTestMethodAsync;
1920

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

2526
/// <summary>
26-
/// A method to be called <see cref="TestInput"/> component and <see cref="ExpectedOutput"/> component
27-
/// is rendered. Use to e.g. setup services that the test input needs to render.
27+
/// Gets or sets the setup action to perform before the <see cref="TestInput"/> and <see cref="ExpectedOutput"/>
28+
/// is rendered and compared.
2829
/// </summary>
2930
[Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; }
3031

31-
private static void NoopTestMethod() { }
32+
/// <summary>
33+
/// Gets or sets the setup action to perform before the <see cref="TestInput"/> and <see cref="ExpectedOutput"/>
34+
/// is rendered and compared.
35+
/// </summary>
36+
[Parameter] public Func<Task> SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopTestMethodAsync; }
3237
}
3338

3439
/// <summary>

src/Components/TestComponentBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task RazorTest()
5656
container.Render(BuildRenderTree);
5757

5858
await ExecuteFixtureTests(container).ConfigureAwait(false);
59-
ExecuteSnapshotTests(container);
59+
await ExecuteSnapshotTests(container).ConfigureAwait(false);
6060
}
6161

6262
/// <inheritdoc/>
@@ -148,7 +148,7 @@ private static async Task InvokeFixtureAction(Fixture fixture, Func<Task> action
148148
}
149149
}
150150

151-
private void ExecuteSnapshotTests(ContainerComponent container)
151+
private async Task ExecuteSnapshotTests(ContainerComponent container)
152152
{
153153
foreach (var (_, snapshot) in container.GetComponents<SnapshotTest>())
154154
{
@@ -157,6 +157,7 @@ private void ExecuteSnapshotTests(ContainerComponent container)
157157

158158
var context = _testContextAdapter.ActivateSnapshotTestContext(testData);
159159
snapshot.Setup();
160+
await snapshot.SetupAsync().ConfigureAwait(false);
160161
var actual = context.RenderTestInput();
161162
var expected = context.RenderExpectedOutput();
162163
actual.MarkupMatches(expected, snapshot.Description);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@inherits TestComponentBase
2+
@using Shouldly
3+
4+
<SnapshotTest Setup="() => Services">
5+
<TestInput>
6+
<SimpleWithDeps></SimpleWithDeps>
7+
<SimpleWithAyncDeps></SimpleWithAyncDeps>
8+
</TestInput>
9+
<ExpectedOutput>
10+
@inject ITestDep testDep
11+
<p>@testDep.Name</p>
12+
</ExpectedOutput>
13+
</SnapshotTest>

0 commit comments

Comments
 (0)