|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.AspNetCore.Components; |
| 6 | + |
| 7 | +namespace Bunit.Rendering |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a root render tree, wherein components under tests will be rendered. |
| 11 | + /// Components added to the render tree must have either a <c>ChildContent</c> or |
| 12 | + /// <c>Body</c> parameter. |
| 13 | + /// </summary> |
| 14 | + public sealed class RootRenderTree : IReadOnlyCollection<RootRenderTreeRegistration> |
| 15 | + { |
| 16 | + private readonly List<RootRenderTreeRegistration> _registrations = new(); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Adds a component to the render tree. This method can |
| 20 | + /// be called multiple times, with each invocation adding a component |
| 21 | + /// to the render tree. The <typeparamref name="TComponent"/> must have a <c>ChildContent</c> |
| 22 | + /// or <c>Body</c> parameter. |
| 23 | + /// </summary> |
| 24 | + /// <typeparam name="TComponent">The type of the component to add to the render tree.</typeparam> |
| 25 | + /// <param name="parameterBuilder">An optional parameter builder, used to pass parameters to <typeparamref name="TComponent"/>.</param> |
| 26 | + public void Add<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null) where TComponent : IComponent |
| 27 | + { |
| 28 | + var registration = new RootRenderTreeRegistration( |
| 29 | + typeof(TComponent), |
| 30 | + CreateRenderFragmentBuilder<TComponent>(parameterBuilder) |
| 31 | + ); |
| 32 | + _registrations.Add(registration); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Try to add a component to the render tree if it has not already been added. This method can |
| 37 | + /// be called multiple times, with each invocation adding a component |
| 38 | + /// to the render tree. The <typeparamref name="TComponent"/> must have a <c>ChildContent</c> |
| 39 | + /// or <c>Body</c> parameter. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 42 | + /// This method will only add the component to the render tree if it has not already been added. |
| 43 | + /// Use <see cref="Add{TComponent}(Action{ComponentParameterCollectionBuilder{TComponent}}?)"/> to |
| 44 | + /// add the same component multiple times. |
| 45 | + /// </remarks> |
| 46 | + /// <typeparam name="TComponent">The type of the component to add to the render tree.</typeparam> |
| 47 | + /// <param name="parameterBuilder">An optional parameter builder, used to pass parameters to <typeparamref name="TComponent"/>.</param> |
| 48 | + /// <returns>True if component was added, false if it was previously added and not added again.</returns> |
| 49 | + public bool TryAdd<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null) where TComponent : IComponent |
| 50 | + { |
| 51 | + var componentType = typeof(TComponent); |
| 52 | + if (_registrations.Any(x => x.ComponentType == componentType)) |
| 53 | + return false; |
| 54 | + |
| 55 | + var registration = new RootRenderTreeRegistration( |
| 56 | + componentType, |
| 57 | + CreateRenderFragmentBuilder<TComponent>(parameterBuilder) |
| 58 | + ); |
| 59 | + _registrations.Add(registration); |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /// <inheritdoc/> |
| 65 | + public int Count => _registrations.Count; |
| 66 | + |
| 67 | + /// <inheritdoc/> |
| 68 | + public IEnumerator<RootRenderTreeRegistration> GetEnumerator() => _registrations.GetEnumerator(); |
| 69 | + |
| 70 | + /// <inheritdoc/> |
| 71 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Creates a new <see cref="RenderFragment"/> that wraps <paramref name="target"/> |
| 75 | + /// inside the components registered in this <see cref="RootRenderTree"/>. |
| 76 | + /// </summary> |
| 77 | + /// <param name="target"><see cref="RenderFragment"/> to render inside the render tree.</param> |
| 78 | + /// <returns>A <see cref="RenderFragment"/> that renders the <paramref name="target"/> inside this <see cref="RootRenderTree"/> render tree.</returns> |
| 79 | + internal RenderFragment Wrap(RenderFragment target) |
| 80 | + { |
| 81 | + // Wrap from the last added to the first added, as we start with the |
| 82 | + // target and goes from inside to out. |
| 83 | + var result = target; |
| 84 | + for (int i = _registrations.Count - 1; i >= 0; i--) |
| 85 | + { |
| 86 | + result = _registrations[i].RenderFragmentBuilder(result); |
| 87 | + } |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Gets the number of registered components of type <typeparamref name="TComponent"/> |
| 93 | + /// in the render tree. |
| 94 | + /// </summary> |
| 95 | + /// <typeparam name="TComponent">Component type to count.</typeparam> |
| 96 | + /// <returns>Number of components of type <typeparamref name="TComponent"/> in render tree.</returns> |
| 97 | + internal int GetCountOf<TComponent>() where TComponent : IComponent |
| 98 | + { |
| 99 | + var result = 0; |
| 100 | + var countType = typeof(TComponent); |
| 101 | + |
| 102 | + for (int i = 0; i < _registrations.Count; i++) |
| 103 | + { |
| 104 | + if (countType == _registrations[i].ComponentType) |
| 105 | + result++; |
| 106 | + } |
| 107 | + |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + private static RenderFragment<RenderFragment> CreateRenderFragmentBuilder<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder) where TComponent : IComponent |
| 112 | + { |
| 113 | + return rc => |
| 114 | + { |
| 115 | + var builder = new ComponentParameterCollectionBuilder<TComponent>(); |
| 116 | + parameterBuilder?.Invoke(builder); |
| 117 | + |
| 118 | + var added = builder.TryAdd("ChildContent", rc) || builder.TryAdd("Body", rc); |
| 119 | + if (!added) |
| 120 | + throw new ArgumentException($"The {typeof(TComponent)} does not have a ChildContent or Body parameter. Only components with one of these parameters can be added to the root render tree."); |
| 121 | + |
| 122 | + return builder.Build().ToRenderFragment<TComponent>(); |
| 123 | + }; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments