Skip to content

Commit

Permalink
feat: added UseFor generic replacement helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed May 21, 2021
1 parent 264d9b2 commit 11c252e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ public static ComponentFactoryCollection UseStubFor(this ComponentFactoryCollect
factories.Add(new StubComponentFactory(componentTypePredicate, options ?? StubOptions.Default));
return factories;
}

/// <summary>
/// Configures bUnit to replace all components of type <typeparamref name="TComponent"/> with a component
/// of type <typeparamref name="TReplacementComponent"/>.
/// </summary>
/// <typeparam name="TComponent">Type of component to replace.</typeparam>
/// <typeparam name="TReplacementComponent">Type of component to replace with.</typeparam>
/// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param>
/// <returns>A <see cref="ComponentFactoryCollection"/>.</returns>
public static ComponentFactoryCollection UseFor<TComponent, TReplacementComponent>(this ComponentFactoryCollection factories)
where TComponent : IComponent
where TReplacementComponent : IComponent
{
if (factories is null)
throw new ArgumentNullException(nameof(factories));

factories.Add(new GenericComponentFactory<TComponent, TReplacementComponent>());

return factories;
}
}
}
#endif
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))!;
}
}
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

0 comments on commit 11c252e

Please sign in to comment.