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.
fix: allow using 3rd party IComponentActivator at the same time as co…
…mponent factories
- Loading branch information
Showing
6 changed files
with
91 additions
and
9 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
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#if NET5_0_OR_GREATER | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Bunit; | ||
|
||
|
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
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
61 changes: 61 additions & 0 deletions
61
tests/bunit.core.tests/Rendering/BunitComponentActivatorTest.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,61 @@ | ||
#if NET5_0_OR_GREATER | ||
|
||
using Bunit.TestDoubles; | ||
|
||
namespace Bunit.Rendering; | ||
|
||
public class BunitComponentActivatorTest : TestContext | ||
{ | ||
[Fact(DisplayName = "Default activator")] | ||
public void Test001() | ||
{ | ||
var activator = new CustomComponentActivator(); | ||
Services.AddSingleton<IComponentActivator>(activator); | ||
|
||
var cut = RenderComponent<Simple1>(); | ||
|
||
cut.Instance.ShouldBeOfType<Simple1>(); | ||
} | ||
|
||
[Fact(DisplayName = "Custom singleton IComponentActivator registered in Services")] | ||
public void Test002() | ||
{ | ||
var activator = new CustomComponentActivator(); | ||
Services.AddSingleton<IComponentActivator>(activator); | ||
|
||
RenderComponent<Simple1>(); | ||
|
||
activator.RequestedComponentTypes | ||
.ShouldHaveSingleItem() | ||
.ShouldBe(typeof(Simple1)); | ||
} | ||
|
||
[Fact(DisplayName = "Custom singleton IComponentActivator registered in Services with ComponentFactories in use")] | ||
public void Test003() | ||
{ | ||
var activator = new CustomComponentActivator(); | ||
Services.AddSingleton<IComponentActivator>(activator); | ||
ComponentFactories.AddStub<ClickCounter>(); | ||
|
||
var cut = RenderComponent<Wrapper>(ps => ps.AddChildContent<ClickCounter>()); | ||
|
||
activator.RequestedComponentTypes | ||
.ShouldHaveSingleItem() | ||
.ShouldBe(typeof(Wrapper)); | ||
cut.HasComponent<ClickCounter>().ShouldBeFalse(); | ||
cut.HasComponent<Stub<ClickCounter>>().ShouldBeTrue(); | ||
} | ||
|
||
private sealed class CustomComponentActivator : IComponentActivator | ||
{ | ||
public List<Type> RequestedComponentTypes { get; } = new(); | ||
|
||
public IComponent CreateInstance(Type componentType) | ||
{ | ||
RequestedComponentTypes.Add(componentType); | ||
return (IComponent)Activator.CreateInstance(componentType)!; | ||
} | ||
} | ||
} | ||
|
||
#endif |