Skip to content

Commit

Permalink
feat: Enable constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 10, 2024
1 parent 8ff1bf3 commit 3cee566
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/bunit.core/Rendering/BunitComponentActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ internal class BunitComponentActivator : IComponentActivator
private readonly ComponentFactoryCollection factories;
private readonly IComponentActivator componentActivator;

public BunitComponentActivator(ComponentFactoryCollection factories, IComponentActivator? externalComponentActivator)
public BunitComponentActivator(
IServiceProvider serviceProvider,
ComponentFactoryCollection factories,
IComponentActivator? externalComponentActivator)
{
this.factories = factories ?? throw new ArgumentNullException(nameof(factories));
this.componentActivator = externalComponentActivator ?? DefaultComponentActivator.Instance;
this.componentActivator = externalComponentActivator ?? new DefaultComponentActivator(serviceProvider);
}

public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type componentType)
Expand Down Expand Up @@ -43,12 +46,17 @@ public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessed

private sealed class DefaultComponentActivator : IComponentActivator
{
public static IComponentActivator Instance { get; } = new DefaultComponentActivator();
private readonly IServiceProvider serviceProvider;

public DefaultComponentActivator(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

/// <inheritdoc />
public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type componentType)
{
return (IComponent)Activator.CreateInstance(componentType)!;
{
return (IComponent)ActivatorUtilities.CreateInstance(serviceProvider, componentType);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
: base(services, loggerFactory, new BunitComponentActivator(services, services.GetRequiredService<ComponentFactoryCollection>(), null))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
Expand All @@ -89,7 +89,7 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
: base(services, loggerFactory, new BunitComponentActivator(services, services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.JSInterop;

namespace Bunit.TestAssets.SampleComponents;

public class ConstructorInjectionComponent : ComponentBase
{
public IJSRuntime JSRuntime { get; }

public ConstructorInjectionComponent(IJSRuntime jsRuntime)
{
JSRuntime = jsRuntime;
}
}
10 changes: 10 additions & 0 deletions tests/bunit.web.tests/Rendering/RenderedComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ public void Test020()

Should.Throw<ComponentDisposedException>(() => target.Instance);
}
#if NET9_0_OR_GREATER

[Fact(DisplayName = "Component with constructor dependencies is resolved when rendered")]
public void Test021()
{
var cut = RenderComponent<ConstructorInjectionComponent>();

cut.Instance.JSRuntime.ShouldNotBeNull();
}
#endif
}

0 comments on commit 3cee566

Please sign in to comment.