|
| 1 | +#if NET5_0_OR_GREATER |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using Bunit.ComponentFactories; |
| 5 | +using Bunit.TestDoubles; |
| 6 | +using Microsoft.AspNetCore.Components; |
| 7 | + |
| 8 | +namespace Bunit |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Extension methods for using component doubles. |
| 12 | + /// </summary> |
| 13 | + public static class ComponentFactoryCollectionExtensions |
| 14 | + { |
| 15 | + private static readonly RenderFragment<IReadOnlyDictionary<string, object>> NoopReplacementTemplate = p => b => {}; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Configures bUnit to replace all components of type <typeparamref name="TComponent"/> with a component |
| 19 | + /// of type <typeparamref name="TReplacementComponent"/>. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TComponent">Type of component to replace.</typeparam> |
| 22 | + /// <typeparam name="TReplacementComponent">Type of component to replace with.</typeparam> |
| 23 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 24 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 25 | + public static ComponentFactoryCollection Add<TComponent, TReplacementComponent>(this ComponentFactoryCollection factories) |
| 26 | + where TComponent : IComponent |
| 27 | + where TReplacementComponent : IComponent |
| 28 | + { |
| 29 | + if (factories is null) |
| 30 | + throw new ArgumentNullException(nameof(factories)); |
| 31 | + |
| 32 | + factories.Add(new GenericComponentFactory<TComponent, TReplacementComponent>()); |
| 33 | + |
| 34 | + return factories; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Configures bUnit to use replace all components of type <typeparamref name="TComponent"/> (including derived components) |
| 39 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 40 | + /// </summary> |
| 41 | + /// <remarks>NOTE: This will replace any component of type <typeparamref name="TComponent"/> or components that derives/inherits from it.</remarks> |
| 42 | + /// <typeparam name="TComponent">The type of component to replace with a <see cref="Stub{TComponent}"/> component.</typeparam> |
| 43 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 44 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 45 | + public static ComponentFactoryCollection AddStub<TComponent>(this ComponentFactoryCollection factories) where TComponent : IComponent |
| 46 | + => AddStub<TComponent>(factories, NoopReplacementTemplate); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Configures bUnit to use replace all components of type <typeparamref name="TComponent"/> (including derived components) |
| 50 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 51 | + /// </summary> |
| 52 | + /// <remarks>NOTE: This will replace any component of type <typeparamref name="TComponent"/> or components that derives/inherits from it.</remarks> |
| 53 | + /// <typeparam name="TComponent">The type of component to replace with a <see cref="Stub{TComponent}"/> component.</typeparam> |
| 54 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 55 | + /// <param name="replacementTemplate">Optional replacement template that will be used to render output instead of the stubbed out component.</param> |
| 56 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 57 | + public static ComponentFactoryCollection AddStub<TComponent>( |
| 58 | + this ComponentFactoryCollection factories, |
| 59 | + Func<IReadOnlyDictionary<string, object>, string> replacementTemplate) |
| 60 | + where TComponent : IComponent |
| 61 | + { |
| 62 | + return AddStub<TComponent>( |
| 63 | + factories, |
| 64 | + ps => b => b.AddMarkupContent(0, replacementTemplate(ps))); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Configures bUnit to use replace all components of type <typeparamref name="TComponent"/> (including derived components) |
| 69 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 70 | + /// </summary> |
| 71 | + /// <remarks>NOTE: This will replace any component of type <typeparamref name="TComponent"/> or components that derives/inherits from it.</remarks> |
| 72 | + /// <typeparam name="TComponent">The type of component to replace with a <see cref="Stub{TComponent}"/> component.</typeparam> |
| 73 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 74 | + /// <param name="replacementTemplate">Optional replacement template that will be used to render output instead of the stubbed out component.</param> |
| 75 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 76 | + public static ComponentFactoryCollection AddStub<TComponent>( |
| 77 | + this ComponentFactoryCollection factories, |
| 78 | + RenderFragment<IReadOnlyDictionary<string, object>> replacementTemplate) |
| 79 | + where TComponent : IComponent |
| 80 | + { |
| 81 | + return AddStub(factories, CreatePredicate(typeof(TComponent)), replacementTemplate); |
| 82 | + |
| 83 | + static Predicate<Type> CreatePredicate(Type componentTypeToStub) |
| 84 | + => componentType => componentType == componentTypeToStub || componentType.IsAssignableTo(componentTypeToStub); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Configures bUnit to use replace all components whose type make the <paramref name="componentTypePredicate"/> predicate return <c>true</c> |
| 89 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 90 | + /// </summary> |
| 91 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 92 | + /// <param name="componentTypePredicate">The predicate which decides if a component should be replaced with a <see cref="Stub{TComponent}"/> component.</param> |
| 93 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 94 | + public static ComponentFactoryCollection AddStub( |
| 95 | + this ComponentFactoryCollection factories, |
| 96 | + Predicate<Type> componentTypePredicate) |
| 97 | + => AddStub( |
| 98 | + factories, |
| 99 | + componentTypePredicate, |
| 100 | + NoopReplacementTemplate); |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Configures bUnit to use replace all components whose type make the <paramref name="componentTypePredicate"/> predicate return <c>true</c> |
| 104 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 105 | + /// </summary> |
| 106 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 107 | + /// <param name="componentTypePredicate">The predicate which decides if a component should be replaced with a <see cref="Stub{TComponent}"/> component.</param> |
| 108 | + /// <param name="replacementTemplate">Optional replacement template that will be used to render output instead of the stubbed out component.</param> |
| 109 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 110 | + public static ComponentFactoryCollection AddStub( |
| 111 | + this ComponentFactoryCollection factories, |
| 112 | + Predicate<Type> componentTypePredicate, |
| 113 | + Func<IReadOnlyDictionary<string, object>, string> replacementTemplate) |
| 114 | + => AddStub( |
| 115 | + factories, |
| 116 | + componentTypePredicate, |
| 117 | + ps => b => b.AddMarkupContent(0, replacementTemplate(ps))); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Configures bUnit to use replace all components whose type make the <paramref name="componentTypePredicate"/> predicate return <c>true</c> |
| 121 | + /// with a <see cref="Stub{TComponent}"/> component in the render tree. |
| 122 | + /// </summary> |
| 123 | + /// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param> |
| 124 | + /// <param name="componentTypePredicate">The predicate which decides if a component should be replaced with a <see cref="Stub{TComponent}"/> component.</param> |
| 125 | + /// <param name="replacementTemplate">Optional replacement template that will be used to render output instead of the stubbed out component.</param> |
| 126 | + /// <returns>A <see cref="ComponentFactoryCollection"/>.</returns> |
| 127 | + public static ComponentFactoryCollection AddStub( |
| 128 | + this ComponentFactoryCollection factories, |
| 129 | + Predicate<Type> componentTypePredicate, |
| 130 | + RenderFragment<IReadOnlyDictionary<string, object>> replacementTemplate) |
| 131 | + { |
| 132 | + if (factories is null) |
| 133 | + throw new ArgumentNullException(nameof(factories)); |
| 134 | + if (componentTypePredicate is null) |
| 135 | + throw new ArgumentNullException(nameof(componentTypePredicate)); |
| 136 | + |
| 137 | + factories.Add(new StubComponentFactory(componentTypePredicate, replacementTemplate)); |
| 138 | + return factories; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +#endif |
0 commit comments