forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ComponentFactories.Add<T>(instance)
- Loading branch information
Showing
6 changed files
with
144 additions
and
3 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
35 changes: 35 additions & 0 deletions
35
src/bunit.core/ComponentFactories/InstanceComponentFactory.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,35 @@ | ||
#if NET5_0_OR_GREATER | ||
using System; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Bunit.ComponentFactories | ||
{ | ||
internal sealed class InstanceComponentFactory<TComponent> : IComponentFactory | ||
where TComponent : IComponent | ||
{ | ||
private readonly TComponent instance; | ||
private int createCount; | ||
|
||
public InstanceComponentFactory(TComponent instance) | ||
=> this.instance = instance; | ||
|
||
public bool CanCreate(Type componentType) | ||
=> componentType == typeof(TComponent); | ||
|
||
public IComponent Create(Type componentType) | ||
{ | ||
if(createCount == 1) | ||
{ | ||
throw new InvalidOperationException( | ||
$"The instance object passed to the" + | ||
$"{nameof(TestContextBase.ComponentFactories)}.{nameof(ComponentFactoryCollectionExtensions.Add)}<{typeof(TComponent).Name}>(instance) method can only be used to replace " + | ||
$"one {typeof(TComponent)} component in the render tree."); | ||
} | ||
|
||
createCount++; | ||
|
||
return instance; | ||
} | ||
} | ||
} | ||
#endif |
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
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
55 changes: 55 additions & 0 deletions
55
tests/bunit.core.tests/ComponentFactories/InstanceComponentFactoryTest.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,55 @@ | ||
#if NET5_0_OR_GREATER | ||
using System; | ||
using Bunit.TestAssets.SampleComponents; | ||
using Moq; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Bunit.ComponentFactories | ||
{ | ||
public class InstanceComponentFactoryTest : TestContext | ||
{ | ||
[Fact(DisplayName = "Add throws when factories is null")] | ||
public void Test001() | ||
=> Should.Throw<ArgumentNullException>(() => ComponentFactoryCollectionExtensions.Add<Simple1>(default, default)); | ||
|
||
[Fact(DisplayName = "Add throws when instance is null")] | ||
public void Test002() | ||
=> Should.Throw<ArgumentNullException>(() => ComponentFactories.Add<Simple1>(default)); | ||
|
||
[Fact(DisplayName = "Factory replaces one TComponent with instance in the render tree")] | ||
public void Test010() | ||
{ | ||
var simple1Mock = new Mock<Simple1>(); | ||
ComponentFactories.Add<Simple1>(simple1Mock.Object); | ||
|
||
var cut = RenderComponent<Wrapper>(ps => ps.AddChildContent<Simple1>()); | ||
|
||
cut.FindComponent<Simple1>().Instance | ||
.ShouldBeSameAs(simple1Mock.Object); | ||
} | ||
|
||
[Fact(DisplayName = "Factory throws if component instance is requested twice for TComponent that inherits from ComponentBase")] | ||
public void Test020() | ||
{ | ||
var simple1Mock = new Mock<Simple1>(); | ||
ComponentFactories.Add<Simple1>(simple1Mock.Object); | ||
|
||
Should.Throw<InvalidOperationException>(() => RenderComponent<TwoComponentWrapper>(ps => ps | ||
.Add<Simple1>(p => p.First) | ||
.Add<Simple1>(p => p.Second))); | ||
} | ||
|
||
[Fact(DisplayName = "Factory throws if component instance is requested twice for TComponent that implements from IComponent")] | ||
public void Test021() | ||
{ | ||
var simple1Mock = new Mock<BasicComponent>(); | ||
ComponentFactories.Add<BasicComponent>(simple1Mock.Object); | ||
|
||
Should.Throw<InvalidOperationException>(() => RenderComponent<TwoComponentWrapper>(ps => ps | ||
.Add<BasicComponent>(p => p.First) | ||
.Add<BasicComponent>(p => p.Second))); | ||
} | ||
} | ||
} | ||
#endif |
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,21 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Bunit.TestAssets.SampleComponents | ||
{ | ||
public class BasicComponent : IComponent | ||
{ | ||
private RenderHandle renderHandle; | ||
|
||
public void Attach(RenderHandle renderHandle) | ||
{ | ||
this.renderHandle = renderHandle; | ||
} | ||
|
||
public Task SetParametersAsync(ParameterView parameters) | ||
{ | ||
renderHandle.Render(builder => builder.AddMarkupContent(0, nameof(BasicComponent))); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |