-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
94 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
#if NET5_0_OR_GREATER | ||
|
||
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>(); | ||
|
||
RenderComponent<Wrapper>(ps => ps.AddChildContent<ClickCounter>()); | ||
RenderComponent<Simple1>(); | ||
|
||
activator.RequestedComponentTypes | ||
.ShouldBe(new List<Type> | ||
{ | ||
typeof(Wrapper), typeof(Simple1) | ||
}); | ||
} | ||
|
||
[Fact(DisplayName = "Custom scoped IComponentActivator registered in Services", Skip = "Decide if this should be supported. See #1138")] | ||
public void Test010() | ||
{ | ||
Services.AddScoped<IComponentActivator, CustomComponentActivator>(); | ||
|
||
RenderComponent<Simple1>(); | ||
|
||
Services.GetRequiredService<IComponentActivator>() | ||
.ShouldBeOfType<CustomComponentActivator>() | ||
.RequestedComponentTypes | ||
.ShouldHaveSingleItem() | ||
.ShouldBe(typeof(Simple1)); | ||
} | ||
|
||
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 |