|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Bunit.RazorTesting; |
| 5 | +using Bunit.Rendering; |
| 6 | +using Microsoft.AspNetCore.Components; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | + |
| 9 | +namespace Bunit |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Represents a single fixture in a Razor based test. Used to |
| 13 | + /// define the <see cref="ComponentUnderTest"/> and any <see cref="Fragment"/>'s |
| 14 | + /// you might need during testing, and assert against them in the Test methods. |
| 15 | + /// </summary> |
| 16 | + public abstract class FixtureBase<TFixture> : RazorTest |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Gets or sets the child content of the fragment. |
| 20 | + /// </summary> |
| 21 | + [Parameter] public RenderFragment ChildContent { get; set; } = default!; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets the setup action to perform before the <see cref="Test"/> action, |
| 25 | + /// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked. |
| 26 | + /// </summary> |
| 27 | + [Parameter] public Action<TFixture>? Setup { private get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets the asynchronous setup action to perform before the <see cref="Test"/> action, |
| 31 | + /// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked. |
| 32 | + /// </summary> |
| 33 | + [Parameter] public Func<TFixture, Task>? SetupAsync { private get; set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets or sets the first test action to invoke, after the <see cref="Setup"/> action has |
| 37 | + /// executed (if provided). |
| 38 | + /// |
| 39 | + /// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s |
| 40 | + /// defined in the <see cref="FixtureBase"/>. |
| 41 | + /// </summary> |
| 42 | + [Parameter] public Action<TFixture>? Test { private get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets or sets the first test action to invoke, after the <see cref="SetupAsync"/> action has |
| 46 | + /// executed (if provided). |
| 47 | + /// |
| 48 | + /// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s |
| 49 | + /// defined in the <see cref="FixtureBase"/>. |
| 50 | + /// </summary> |
| 51 | + [Parameter] public Func<TFixture, Task>? TestAsync { private get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets or sets the test actions to invoke, one at the time, in the order they are placed |
| 55 | + /// into the collection, after the <see cref="Setup"/> action and the <see cref="Test"/> action has |
| 56 | + /// executed (if provided). |
| 57 | + /// |
| 58 | + /// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s |
| 59 | + /// defined in the <see cref="FixtureBase"/>. |
| 60 | + /// </summary> |
| 61 | + [Parameter] public IReadOnlyCollection<Action<TFixture>>? Tests { private get; set; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Gets or sets the test actions to invoke, one at the time, in the order they are placed |
| 65 | + /// into the collection, after the <see cref="SetupAsync"/> action and the <see cref="TestAsync"/> action has |
| 66 | + /// executed (if provided). |
| 67 | + /// |
| 68 | + /// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s |
| 69 | + /// defined in the <see cref="FixtureBase"/>. |
| 70 | + /// </summary> |
| 71 | + [Parameter] public IReadOnlyCollection<Func<TFixture, Task>>? TestsAsync { private get; set; } |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public override Task SetParametersAsync(ParameterView parameters) |
| 75 | + { |
| 76 | + if (parameters.TryGetValue<RenderFragment>(nameof(ChildContent), out RenderFragment childContent)) |
| 77 | + ChildContent = childContent; |
| 78 | + else |
| 79 | + throw new InvalidOperationException($"No {nameof(ChildContent)} specified in the {GetType().Name} component."); |
| 80 | + |
| 81 | + Setup = parameters.GetValueOrDefault<Action<TFixture>>(nameof(Setup)); |
| 82 | + SetupAsync = parameters.GetValueOrDefault<Func<TFixture, Task>>(nameof(SetupAsync)); |
| 83 | + Test = parameters.GetValueOrDefault<Action<TFixture>>(nameof(Test)); |
| 84 | + TestAsync = parameters.GetValueOrDefault<Func<TFixture, Task>>(nameof(TestAsync)); |
| 85 | + Tests = parameters.GetValueOrDefault<IReadOnlyCollection<Action<TFixture>>>(nameof(Tests)); |
| 86 | + TestsAsync = parameters.GetValueOrDefault<IReadOnlyCollection<Func<TFixture, Task>>>(nameof(TestsAsync)); |
| 87 | + |
| 88 | + return base.SetParametersAsync(parameters); |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc/> |
| 92 | + protected virtual async Task Run(TFixture self) |
| 93 | + { |
| 94 | + if (Setup is { }) |
| 95 | + TryRun(Setup, self); |
| 96 | + |
| 97 | + if (SetupAsync is { }) |
| 98 | + await TryRunAsync(SetupAsync, self).ConfigureAwait(false); |
| 99 | + |
| 100 | + if (Test is { }) |
| 101 | + TryRun(Test, self); |
| 102 | + |
| 103 | + if (TestAsync is { }) |
| 104 | + await TryRunAsync(TestAsync, self).ConfigureAwait(false); |
| 105 | + |
| 106 | + foreach (var test in Tests ?? Array.Empty<Action<TFixture>>()) |
| 107 | + TryRun(test, self); |
| 108 | + |
| 109 | + foreach (var test in TestsAsync ?? Array.Empty<Func<TFixture, Task>>()) |
| 110 | + await TryRunAsync(test, self).ConfigureAwait(false); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments