Skip to content

Commit 18bc3eb

Browse files
committed
feat: added UseFor generic replacement helper method
1 parent ef1f8ca commit 18bc3eb

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/bunit.core/ComponentFactories/ComponentFactoryCollectionExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ public static ComponentFactoryCollection UseStubFor(this ComponentFactoryCollect
8080
factories.Add(new StubComponentFactory(componentTypePredicate, options ?? StubOptions.Default));
8181
return factories;
8282
}
83+
84+
/// <summary>
85+
/// Configures bUnit to replace all components of type <typeparamref name="TComponent"/> with a component
86+
/// of type <typeparamref name="TReplacementComponent"/>.
87+
/// </summary>
88+
/// <typeparam name="TComponent">Type of component to replace.</typeparam>
89+
/// <typeparam name="TReplacementComponent">Type of component to replace with.</typeparam>
90+
/// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param>
91+
/// <returns>A <see cref="ComponentFactoryCollection"/>.</returns>
92+
public static ComponentFactoryCollection UseFor<TComponent, TReplacementComponent>(this ComponentFactoryCollection factories)
93+
where TComponent : IComponent
94+
where TReplacementComponent : IComponent
95+
{
96+
if (factories is null)
97+
throw new ArgumentNullException(nameof(factories));
98+
99+
factories.Add(new GenericComponentFactory<TComponent, TReplacementComponent>());
100+
101+
return factories;
102+
}
83103
}
84104
}
85105
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components;
7+
8+
namespace Bunit.ComponentFactories
9+
{
10+
internal sealed class GenericComponentFactory<TComponent, TReplacementComponent> : IComponentFactory
11+
where TComponent : IComponent
12+
where TReplacementComponent : IComponent
13+
{
14+
public bool CanCreate(Type componentType) => componentType == typeof(TComponent);
15+
public IComponent Create(Type componentType) => (IComponent)Activator.CreateInstance(typeof(TReplacementComponent))!;
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#if NET5_0_OR_GREATER
2+
using System;
3+
using System.Threading.Tasks;
4+
using Bunit.TestAssets.SampleComponents;
5+
using Microsoft.AspNetCore.Components;
6+
using Microsoft.AspNetCore.Components.Rendering;
7+
using Shouldly;
8+
using Xunit;
9+
10+
namespace Bunit.ComponentFactories
11+
{
12+
public class GenericComponentFactoryTest : TestContext
13+
{
14+
[Fact(DisplayName = "UseFor throws when factories is null")]
15+
public void Test001()
16+
=> Should.Throw<ArgumentNullException>(() => ComponentFactoryCollectionExtensions.UseFor<Simple1, FakeSimple1>(null));
17+
18+
[Fact(DisplayName = "UseFor<TComponent, TReplacementComponent> replaces components of type TComponent with TReplacementComponent")]
19+
public void Test002()
20+
{
21+
ComponentFactories.UseFor<Simple1, FakeSimple1>();
22+
23+
var cut = RenderComponent<RefToSimple1Child>();
24+
25+
cut.MarkupMatches(@"<div id=""ref-status"">Has ref = True</div>");
26+
}
27+
28+
private class FakeSimple1 : Simple1
29+
{
30+
protected override void OnInitialized() { }
31+
protected override Task OnInitializedAsync() => Task.CompletedTask;
32+
public override Task SetParametersAsync(ParameterView parameters) => Task.CompletedTask;
33+
protected override void OnParametersSet() { }
34+
protected override Task OnParametersSetAsync() => Task.CompletedTask;
35+
protected override void BuildRenderTree(RenderTreeBuilder builder) { }
36+
protected override bool ShouldRender() => false;
37+
protected override void OnAfterRender(bool firstRender) { }
38+
protected override Task OnAfterRenderAsync(bool firstRender) => Task.CompletedTask;
39+
}
40+
}
41+
}
42+
#endif

0 commit comments

Comments
 (0)