|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.AspNetCore.Components; |
| 5 | +using Microsoft.AspNetCore.Components.Rendering; |
| 6 | +using System.Runtime.ExceptionServices; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using System.Text.Encodings.Web; |
| 9 | +using Moq; |
| 10 | +using System.Linq; |
| 11 | +using Microsoft.AspNetCore.Components.RenderTree; |
| 12 | +using Microsoft.JSInterop; |
| 13 | +using Shouldly; |
| 14 | + |
| 15 | +namespace Egil.RazorComponents.Testing |
| 16 | +{ |
| 17 | + public class ComponentBuilder<TComponent> where TComponent : ComponentBase |
| 18 | + { |
| 19 | + private readonly Func<string, string> _encoder = (t) => HtmlEncoder.Default.Encode(t); |
| 20 | + private readonly IDispatcher _dispatcher = Renderer.CreateDefaultDispatcher(); |
| 21 | + |
| 22 | + protected ServiceCollection Services { get; } = new ServiceCollection(); |
| 23 | + protected Dictionary<string, object> Parameters { get; } = new Dictionary<string, object>(); |
| 24 | + |
| 25 | + public ComponentBuilder() |
| 26 | + { |
| 27 | + Services.AddSingleton(x => Mock.Of<IUriHelper>()); |
| 28 | + Services.AddSingleton(x => Mock.Of<IJSRuntime>()); |
| 29 | + Services.AddSingleton<TComponent>(); |
| 30 | + } |
| 31 | + |
| 32 | + public ComponentBuilder<TComponent> WithServices(Action<IServiceCollection> addServices) |
| 33 | + { |
| 34 | + addServices(Services); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public ComponentBuilder<TComponent> WithParams(params (string name, object value)[] paramValues) |
| 39 | + { |
| 40 | + foreach (var (name, value) in paramValues) Parameters[name] = value; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public ComponentBuilder<TComponent> WithChildContent(RenderFragment renderFragment) |
| 45 | + { |
| 46 | + Parameters[RenderTreeBuilder.ChildContent] = renderFragment; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public ComponentBuilder<TComponent> WithChildContent(params RenderFragment[] childContents) |
| 51 | + { |
| 52 | + Parameters[RenderTreeBuilder.ChildContent] = ToFragment(childContents); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public ComponentBuilder<TComponent> WithChildContent(string childMarkupContent) |
| 57 | + { |
| 58 | + return WithChildContent(b => b.AddMarkupContent(0, childMarkupContent)); |
| 59 | + } |
| 60 | + |
| 61 | + public ComponentRenderedText Render() |
| 62 | + { |
| 63 | + var serviceProvider = Services.BuildServiceProvider(); |
| 64 | + var paramCollection = Parameters.Count > 0 ? ParameterCollection.FromDictionary(Parameters) : ParameterCollection.Empty; |
| 65 | + using var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, _dispatcher); |
| 66 | + return GetResult(_dispatcher.InvokeAsync(() => htmlRenderer.RenderComponentAsync<TComponent>(paramCollection))); |
| 67 | + } |
| 68 | + |
| 69 | + private ComponentRenderedText GetResult(Task<ComponentRenderedText> task) |
| 70 | + { |
| 71 | + task.IsCompleted.ShouldBeTrue(); |
| 72 | + if (task.IsCompletedSuccessfully) |
| 73 | + { |
| 74 | + return task.Result; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + ExceptionDispatchInfo.Capture(task.Exception!).Throw(); |
| 79 | + throw new InvalidOperationException("We will never hit this line"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static RenderFragment ToFragment(RenderFragment[] childContents) |
| 84 | + { |
| 85 | + return builder => |
| 86 | + { |
| 87 | + for (int i = 0; i < childContents.Length; i++) |
| 88 | + { |
| 89 | + builder.AddContent(i, childContents[i]); |
| 90 | + } |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public class ComponentBuilder<TComponent, TItem> : ComponentBuilder<TComponent> |
| 96 | + where TComponent : ComponentBase |
| 97 | + { |
| 98 | + public new ComponentBuilder<TComponent, TItem> WithServices(Action<IServiceCollection> addServices) |
| 99 | + { |
| 100 | + addServices(Services); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + public new ComponentBuilder<TComponent, TItem> WithParams(params (string name, object value)[] paramValues) |
| 106 | + { |
| 107 | + foreach (var (name, value) in paramValues) Parameters[name] = value; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public ComponentBuilder<TComponent, TItem> WithItems(IEnumerable<TItem> items) |
| 112 | + { |
| 113 | + Parameters["Items"] = items; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + public ComponentBuilder<TComponent, TItem> WithItems(params TItem[] items) |
| 118 | + { |
| 119 | + Parameters["Items"] = items; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public ComponentBuilder<TComponent, TItem> WithItems(int itemCount, Func<int, TItem> itemGenerator) |
| 124 | + { |
| 125 | + Parameters["Items"] = Enumerable.Range(1, itemCount).Select(itemGenerator).ToArray(); |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public ComponentBuilder<TComponent, TItem> WithDefaultItems(int itemCount) |
| 130 | + { |
| 131 | + return WithItems(itemCount, _ => default!); |
| 132 | + } |
| 133 | + |
| 134 | + public ComponentBuilder<TComponent, TItem> WithTemplate(RenderFragment<TItem> renderTemplate) |
| 135 | + { |
| 136 | + Parameters[RenderTreeBuilder.ChildContent] = renderTemplate; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + public ComponentBuilder<TComponent, TItem> WithTemplate(Func<TemplateBuilder<TItem>, RenderFragment<TItem>> templateBuilder) |
| 141 | + { |
| 142 | + Parameters[RenderTreeBuilder.ChildContent] = templateBuilder(new TemplateBuilder<TItem>()); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + public ComponentBuilder<TComponent, TItem> WithTemplate(params RenderFragment[] renderFragments) |
| 147 | + { |
| 148 | + Parameters[RenderTreeBuilder.ChildContent] = ToFragment<TItem>(renderFragments); |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + private static RenderFragment<TItem> ToFragment<ITtem>(RenderFragment[] childContents) |
| 153 | + { |
| 154 | + return (item) => builder => |
| 155 | + { |
| 156 | + for (int i = 0; i < childContents.Length; i++) |
| 157 | + { |
| 158 | + builder.AddContent(i, childContents[i]); |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | +} |
0 commit comments