-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added UseFor generic replacement helper method
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...unit.core/ComponentFactories/GenericComponentFactory{TComponent,TReplacementComponent}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Bunit.ComponentFactories | ||
{ | ||
internal sealed class GenericComponentFactory<TComponent, TReplacementComponent> : IComponentFactory | ||
where TComponent : IComponent | ||
where TReplacementComponent : IComponent | ||
{ | ||
public bool CanCreate(Type componentType) => componentType == typeof(TComponent); | ||
public IComponent Create(Type componentType) => (IComponent)Activator.CreateInstance(typeof(TReplacementComponent))!; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/bunit.core.tests/ComponentFactories/GenericComponentFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#if NET5_0_OR_GREATER | ||
using System; | ||
using System.Threading.Tasks; | ||
using Bunit.TestAssets.SampleComponents; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Bunit.ComponentFactories | ||
{ | ||
public class GenericComponentFactoryTest : TestContext | ||
{ | ||
[Fact(DisplayName = "UseFor throws when factories is null")] | ||
public void Test001() | ||
=> Should.Throw<ArgumentNullException>(() => ComponentFactoryCollectionExtensions.UseFor<Simple1, FakeSimple1>(null)); | ||
|
||
[Fact(DisplayName = "UseFor<TComponent, TReplacementComponent> replaces components of type TComponent with TReplacementComponent")] | ||
public void Test002() | ||
{ | ||
ComponentFactories.UseFor<Simple1, FakeSimple1>(); | ||
|
||
var cut = RenderComponent<RefToSimple1Child>(); | ||
|
||
cut.MarkupMatches(@"<div id=""ref-status"">Has ref = True</div>"); | ||
} | ||
|
||
private class FakeSimple1 : Simple1 | ||
{ | ||
protected override void OnInitialized() { } | ||
protected override Task OnInitializedAsync() => Task.CompletedTask; | ||
public override Task SetParametersAsync(ParameterView parameters) => Task.CompletedTask; | ||
protected override void OnParametersSet() { } | ||
protected override Task OnParametersSetAsync() => Task.CompletedTask; | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) { } | ||
protected override bool ShouldRender() => false; | ||
protected override void OnAfterRender(bool firstRender) { } | ||
protected override Task OnAfterRenderAsync(bool firstRender) => Task.CompletedTask; | ||
} | ||
} | ||
} | ||
#endif |