|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | + |
| 4 | +using Bunit.Rendering; |
| 5 | + |
| 6 | +using Microsoft.AspNetCore.Components; |
| 7 | +using Microsoft.AspNetCore.Components.Rendering; |
| 8 | + |
| 9 | +namespace Bunit |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// <see cref="ComponentParameter"/> factory methods. |
| 13 | + /// </summary> |
| 14 | + public static class ComponentParameterFactory |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="name">Parameter name.</param> |
| 20 | + /// <param name="callback">The event callback.</param> |
| 21 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 22 | + public static ComponentParameter EventCallback(string name, Action callback) |
| 23 | + { |
| 24 | + return ComponentParameter.CreateParameter(name, new EventCallback(null, callback)); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 29 | + /// </summary> |
| 30 | + /// <param name="name">Parameter name.</param> |
| 31 | + /// <param name="callback">The event callback.</param> |
| 32 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 33 | + public static ComponentParameter EventCallback(string name, Action<object> callback) |
| 34 | + { |
| 35 | + return ComponentParameter.CreateParameter(name, new EventCallback(null, callback)); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 40 | + /// </summary> |
| 41 | + /// <param name="name">Parameter name.</param> |
| 42 | + /// <param name="callback">The event callback.</param> |
| 43 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 44 | + public static ComponentParameter EventCallback(string name, Func<Task> callback) |
| 45 | + { |
| 46 | + return ComponentParameter.CreateParameter(name, new EventCallback(null, callback)); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 51 | + /// </summary> |
| 52 | + /// <param name="name">Parameter name.</param> |
| 53 | + /// <param name="callback">The event callback.</param> |
| 54 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 55 | + public static ComponentParameter EventCallback(string name, Func<object, Task> callback) |
| 56 | + { |
| 57 | + return ComponentParameter.CreateParameter(name, new EventCallback(null, callback)); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 62 | + /// </summary> |
| 63 | + /// <param name="name">Parameter name.</param> |
| 64 | + /// <param name="callback">The event callback.</param> |
| 65 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 66 | + public static ComponentParameter EventCallback<TValue>(string name, Action callback) |
| 67 | + { |
| 68 | + return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback)); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 73 | + /// </summary> |
| 74 | + /// <param name="name">Parameter name.</param> |
| 75 | + /// <param name="callback">The event callback.</param> |
| 76 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 77 | + public static ComponentParameter EventCallback<TValue>(string name, Action<TValue> callback) |
| 78 | + { |
| 79 | + return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback)); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 84 | + /// </summary> |
| 85 | + /// <param name="name">Parameter name.</param> |
| 86 | + /// <param name="callback">The event callback.</param> |
| 87 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 88 | + public static ComponentParameter EventCallback<TValue>(string name, Func<Task> callback) |
| 89 | + { |
| 90 | + return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback)); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Creates a <see cref="ComponentParameter"/> with an <see cref="Microsoft.AspNetCore.Components.EventCallback"/> that will call the provided <paramref name="callback"/>. |
| 95 | + /// </summary> |
| 96 | + /// <param name="name">Parameter name.</param> |
| 97 | + /// <param name="callback">The event callback.</param> |
| 98 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 99 | + public static ComponentParameter EventCallback<TValue>(string name, Func<TValue, Task> callback) |
| 100 | + { |
| 101 | + return ComponentParameter.CreateParameter(name, new EventCallback<TValue>(null, callback)); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Creates a component parameter which can be passed to a test contexts render methods. |
| 106 | + /// </summary> |
| 107 | + /// <param name="name">Parameter name</param> |
| 108 | + /// <param name="value">Value or null of the parameter</param> |
| 109 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 110 | + public static ComponentParameter Parameter(string name, object? value) |
| 111 | + { |
| 112 | + return ComponentParameter.CreateParameter(name, value); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Creates a cascading value which can be passed to a test contexts render methods. |
| 117 | + /// </summary> |
| 118 | + /// <param name="name">Parameter name</param> |
| 119 | + /// <param name="value">Value of the parameter</param> |
| 120 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 121 | + public static ComponentParameter CascadingValue(string name, object value) |
| 122 | + { |
| 123 | + return ComponentParameter.CreateCascadingValue(name, value); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Creates a cascading value which can be passed to a test contexts render methods. |
| 128 | + /// </summary> |
| 129 | + /// <param name="value">Value of the parameter</param> |
| 130 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 131 | + public static ComponentParameter CascadingValue(object value) |
| 132 | + { |
| 133 | + return ComponentParameter.CreateCascadingValue(null, value); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Creates a ChildContent <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> with the provided |
| 138 | + /// <paramref name="markup"/> as rendered output. |
| 139 | + /// </summary> |
| 140 | + /// <param name="markup">Markup to pass to the child content parameter</param> |
| 141 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 142 | + public static ComponentParameter ChildContent(string markup) |
| 143 | + { |
| 144 | + return RenderFragment(nameof(ChildContent), markup); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Creates a ChildContent <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> which will render a <typeparamref name="TComponent"/> component |
| 149 | + /// with the provided <paramref name="parameters"/> as input. |
| 150 | + /// </summary> |
| 151 | + /// <typeparam name="TComponent">The type of the component to render with the <see cref="Microsoft.AspNetCore.Components.RenderFragment"/></typeparam> |
| 152 | + /// <param name="parameters">Parameters to pass to the <typeparamref name="TComponent"/>.</param> |
| 153 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 154 | + public static ComponentParameter ChildContent<TComponent>(params ComponentParameter[] parameters) where TComponent : class, IComponent |
| 155 | + { |
| 156 | + return RenderFragment<TComponent>(nameof(ChildContent), parameters); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Creates a <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> with the provided |
| 161 | + /// <paramref name="markup"/> as rendered output and passes it to the parameter specified in <paramref name="name"/>. |
| 162 | + /// </summary> |
| 163 | + /// <param name="name">Parameter name.</param> |
| 164 | + /// <param name="markup">Markup to pass to the render fragment parameter</param> |
| 165 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 166 | + public static ComponentParameter RenderFragment(string name, string markup) |
| 167 | + { |
| 168 | + return ComponentParameter.CreateParameter(name, markup.ToMarkupRenderFragment()); |
| 169 | + } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Creates a <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> which will render a <typeparamref name="TComponent"/> component |
| 173 | + /// with the provided <paramref name="parameters"/> as input, and passes it to the parameter specified in <paramref name="name"/>. |
| 174 | + /// </summary> |
| 175 | + /// <typeparam name="TComponent">The type of the component to render with the <see cref="Microsoft.AspNetCore.Components.RenderFragment"/></typeparam> |
| 176 | + /// <param name="name">Parameter name.</param> |
| 177 | + /// <param name="parameters">Parameters to pass to the <typeparamref name="TComponent"/>.</param> |
| 178 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 179 | + public static ComponentParameter RenderFragment<TComponent>(string name, params ComponentParameter[] parameters) where TComponent : class, IComponent |
| 180 | + { |
| 181 | + return ComponentParameter.CreateParameter(name, parameters.ToComponentRenderFragment<TComponent>()); |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Creates a template component parameter which will pass the <paramref name="template"/> <see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" /> |
| 186 | + /// to the parameter with the name <paramref name="name"/>. |
| 187 | + /// </summary> |
| 188 | + /// <typeparam name="TValue">The value used to build the content.</typeparam> |
| 189 | + /// <param name="name">Parameter name.</param> |
| 190 | + /// <param name="template"><see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" /> to pass to the parameter.</param> |
| 191 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 192 | + public static ComponentParameter Template<TValue>(string name, RenderFragment<TValue> template) |
| 193 | + { |
| 194 | + return ComponentParameter.CreateParameter(name, template); |
| 195 | + } |
| 196 | + |
| 197 | + /// <summary> |
| 198 | + /// Creates a template component parameter which will pass the a <see cref="Microsoft.AspNetCore.Components.RenderFragment{TValue}" /> |
| 199 | + /// to the parameter with the name <paramref name="name"/>. |
| 200 | + /// The <paramref name="markupFactory"/> will be used to generate the markup inside the template. |
| 201 | + /// </summary> |
| 202 | + /// <typeparam name="TValue">The value used to build the content.</typeparam> |
| 203 | + /// <param name="name">Parameter name.</param> |
| 204 | + /// <param name="markupFactory">A markup factory that takes a <typeparamref name="TValue"/> as input and returns markup/HTML.</param> |
| 205 | + /// <returns>The <see cref="ComponentParameter"/>.</returns> |
| 206 | + public static ComponentParameter Template<TValue>(string name, Func<TValue, string> markupFactory) |
| 207 | + { |
| 208 | + return Template<TValue>(name, value => (RenderTreeBuilder builder) => builder.AddMarkupContent(0, markupFactory(value))); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments