Skip to content

Commit d06dde4

Browse files
committed
Moved InvokeAsync to IRenderedFragmentBase. Changed Timeout in RazorTestBase to a TimeSpan.
1 parent a9485e0 commit d06dde4

12 files changed

Lines changed: 70 additions & 27 deletions

File tree

src/bunit.core.tests/ComponentParameterBuilderTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,9 @@ public void Test104()
386386
}
387387

388388
private static ComponentParameterBuilder<AllTypesOfParams<string>> CreateSut()
389-
{
390-
return CreateSut<AllTypesOfParams<string>>();
391-
}
389+
=> CreateSut<AllTypesOfParams<string>>();
392390

393-
private static ComponentParameterBuilder<TComponent> CreateSut<TComponent>() where TComponent : class, IComponent
394-
{
395-
return new ComponentParameterBuilder<TComponent>();
396-
}
391+
private static ComponentParameterBuilder<TComponent> CreateSut<TComponent>() where TComponent : IComponent
392+
=> new ComponentParameterBuilder<TComponent>();
397393
}
398394
}

src/bunit.core.tests/Rendering/TestRendererTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task Test001()
3333
handler.ReceivedEvents.Count.ShouldBe(1);
3434

3535
// Act #2
36-
await sut.InvokeAsync(() => cut.Component.SetParametersAsync(ParameterView.Empty));
36+
await sut.Dispatcher.InvokeAsync(() => cut.Component.SetParametersAsync(ParameterView.Empty));
3737

3838
// Assert #2
3939
handler.ReceivedEvents.Count.ShouldBe(2);

src/bunit.core/IRenderedFragmentBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Threading.Tasks;
3+
using Bunit.Rendering;
24

35
namespace Bunit
46
{
@@ -26,5 +28,12 @@ public interface IRenderedFragmentBase
2628
/// Gets the <see cref="IServiceProvider"/> used when rendering the component.
2729
/// </summary>
2830
IServiceProvider Services { get; }
31+
32+
/// <summary>
33+
/// Invokes the given <paramref name="callback"/> in the context of the associated <see cref="ITestRenderer"/>.
34+
/// </summary>
35+
/// <param name="callback"></param>
36+
/// <returns>A <see cref="Task"/> that will be completed when the action has finished executing.</returns>
37+
Task InvokeAsync(Action callback);
2938
}
3039
}

src/bunit.core/RazorTesting/RazorTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class RazorTestBase : TestContextBase, ITestContext, IComponent
3333
/// <summary>
3434
/// Gets or sets the timeout of the test, in milliseconds; if zero or negative, means the test case has no timeout.
3535
/// </summary>
36-
[Parameter] public virtual int Timeout { get; set; } = 0;
36+
[Parameter] public virtual TimeSpan? Timeout { get; set; }
3737

3838
/// <summary>
3939
/// Run the test logic of the <see cref="RazorTestBase"/>.
@@ -62,7 +62,7 @@ public virtual Task SetParametersAsync(ParameterView parameters)
6262
{
6363
Skip = parameters.GetValueOrDefault<string>(nameof(Skip));
6464
Description = parameters.GetValueOrDefault<string>(nameof(Description));
65-
Timeout = parameters.GetValueOrDefault<int>(nameof(Timeout));
65+
Timeout = parameters.GetValueOrDefault<TimeSpan>(nameof(Timeout));
6666
return Task.CompletedTask;
6767
}
6868

src/bunit.core/Rendering/ITestRenderer.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ public interface ITestRenderer : IRenderEventProducer
1818
/// </summary>
1919
Dispatcher Dispatcher { get; }
2020

21-
/// <summary>
22-
/// Dispatches an callback in the context of the renderer synchronously and
23-
/// asserts no errors happened during dispatch
24-
/// </summary>
25-
/// <param name="callback"></param>
26-
/// <returns>A task that completes when the action finishes its invocation.</returns>
27-
Task InvokeAsync(Action callback);
21+
///// <summary>
22+
///// Invokes the given <paramref name="callback"/> in the context of this <see cref="ITestRenderer"/>.
23+
///// </summary>
24+
///// <param name="callback"></param>
25+
///// <returns>A <see cref="Task"/> that will be completed when the action has finished executing.</returns>
26+
//Task InvokeAsync(Action callback);
2827

2928
/// <summary>
3029
/// Instantiates and renders the component of type <typeparamref name="TComponent"/>.

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ public int RenderFragment(RenderFragment renderFragment)
114114
return result;
115115
}
116116

117-
/// <inheritdoc/>
118-
public Task InvokeAsync(Action callback)
119-
{
120-
return Dispatcher.InvokeAsync(callback);
121-
}
122-
123117
private int RenderFragmentInsideWrapper(RenderFragment renderFragment)
124118
{
125119
var wrapper = new WrapperComponent(renderFragment);

src/bunit.web/Rendering/RenderedComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RenderedComponent(IServiceProvider services, int componentId, TComponent
2222
/// <inheritdoc/>
2323
public void SetParametersAndRender(ParameterView parameters)
2424
{
25-
Renderer.InvokeAsync(() =>
25+
InvokeAsync(() =>
2626
{
2727
Instance.SetParametersAsync(parameters);
2828
});

src/bunit.web/Rendering/RenderedFragment.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
129129
return Nodes.CompareTo(_firstRenderNodes);
130130
}
131131

132+
/// <inheritdoc/>
133+
public Task InvokeAsync(Action callback) => Renderer.Dispatcher.InvokeAsync(callback);
134+
132135
private string RetrieveLatestMarkupFromRenderer() => Htmlizer.GetHtml(Renderer, ComponentId);
133136

134137
Task IRenderEventHandler.Handle(RenderEvent renderEvent)

src/bunit.web/TestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public IRenderedComponent<TComponent> RenderComponent<TComponent>(params Compone
3838
/// <typeparam name="TComponent">Type of the component to render</typeparam>
3939
/// <param name="parameterBuilder">The ComponentParameterBuilder action to add type safe parameters to pass to the component when it is rendered</param>
4040
/// <returns>The rendered <typeparamref name="TComponent"/></returns>
41-
public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : class, IComponent
41+
public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
4242
{
4343
if (parameterBuilder is null)
4444
throw new ArgumentNullException(nameof(parameterBuilder));

src/bunit.xunit.tests/RazorTesting/RazorTestDiscovererTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading;
34

45
using Bunit.SampleComponents;
@@ -77,5 +78,18 @@ public void Test004()
7778
x => x.DisplayName.ShouldBe(nameof(FixturesWithoutDescription.AsyncTest))
7879
);
7980
}
81+
82+
[Fact(DisplayName = "Timeout is set correctly in test case")]
83+
public void Test005()
84+
{
85+
var discoverer = new RazorTestDiscoverer(_messageBus);
86+
var testMethod = Mocks.TestMethod(typeof(TimeoutRazorComponent), nameof(TestComponentBase.RazorTests));
87+
88+
var testCases = discoverer.Discover(_options, testMethod, _attribute);
89+
90+
var actualTimeout = testCases.Single().Timeout;
91+
92+
TimeSpan.FromMilliseconds(actualTimeout).ShouldBe(TimeoutRazorComponent.TIMEOUT);
93+
}
8094
}
8195
}

0 commit comments

Comments
 (0)