|
| 1 | +using System.Globalization; |
| 2 | +using System.Reflection; |
| 3 | +using Bunit.Rendering; |
| 4 | +using Microsoft.AspNetCore.Components.Routing; |
| 5 | + |
| 6 | +namespace Bunit.TestDoubles.Router; |
| 7 | + |
| 8 | +internal sealed class FakeRouter : IDisposable |
| 9 | +{ |
| 10 | + private readonly NavigationManager navigationManager; |
| 11 | + private readonly ComponentRegistry componentRegistry; |
| 12 | + |
| 13 | + public FakeRouter(NavigationManager navigationManager, ComponentRegistry componentRegistry) |
| 14 | + { |
| 15 | + this.navigationManager = navigationManager; |
| 16 | + this.componentRegistry = componentRegistry; |
| 17 | + navigationManager.LocationChanged += UpdatePageParameters; |
| 18 | + } |
| 19 | + |
| 20 | + public void Dispose() => navigationManager.LocationChanged -= UpdatePageParameters; |
| 21 | + |
| 22 | + private void UpdatePageParameters(object? sender, LocationChangedEventArgs e) |
| 23 | + { |
| 24 | + var uri = new Uri(e.Location); |
| 25 | + var relativeUri = uri.PathAndQuery; |
| 26 | + |
| 27 | + foreach (var instance in componentRegistry.Components) |
| 28 | + { |
| 29 | + var routeAttributes = GetRouteAttributesFromComponent(instance); |
| 30 | + |
| 31 | + if (routeAttributes.Length == 0) |
| 32 | + { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + foreach (var template in routeAttributes.Select(r => r.Template)) |
| 37 | + { |
| 38 | + var templateSegments = template.Trim('/').Split("/"); |
| 39 | + var uriSegments = relativeUri.Trim('/').Split("/"); |
| 40 | + |
| 41 | + if (templateSegments.Length > uriSegments.Length) |
| 42 | + { |
| 43 | + continue; |
| 44 | + } |
| 45 | +#if NET6_0_OR_GREATER |
| 46 | + var parameters = new Dictionary<string, object?>(); |
| 47 | +#else |
| 48 | + var parameters = new Dictionary<string, object>(); |
| 49 | +#endif |
| 50 | + |
| 51 | + for (var i = 0; i < templateSegments.Length; i++) |
| 52 | + { |
| 53 | + var templateSegment = templateSegments[i]; |
| 54 | + if (templateSegment.StartsWith('{') && templateSegment.EndsWith('}')) |
| 55 | + { |
| 56 | + var parameterName = GetParameterName(templateSegment); |
| 57 | + var property = GetParameterProperty(instance, parameterName); |
| 58 | + |
| 59 | + if (property is null) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + var isCatchAllParameter = templateSegment[1] == '*'; |
| 65 | + if (!isCatchAllParameter) |
| 66 | + { |
| 67 | + parameters[property.Name] = Convert.ChangeType(uriSegments[i], property.PropertyType, |
| 68 | + CultureInfo.InvariantCulture); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + parameters[parameterName] = string.Join("/", uriSegments.Skip(i)); |
| 73 | + } |
| 74 | + } |
| 75 | + else if (templateSegment != uriSegments[i]) |
| 76 | + { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (parameters.Count == 0) |
| 82 | + { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + // Shall we await this? This should be synchronous in most cases |
| 87 | + // If not, very likely the user has overriden the SetParametersAsync method |
| 88 | + // And should use WaitForXXX methods to await the desired state |
| 89 | + instance.SetParametersAsync(ParameterView.FromDictionary(parameters)); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance) |
| 95 | + { |
| 96 | + var routeAttributes = instance |
| 97 | + .GetType() |
| 98 | + .GetCustomAttributes(typeof(RouteAttribute), true) |
| 99 | + .Cast<RouteAttribute>() |
| 100 | + .ToArray(); |
| 101 | + return routeAttributes; |
| 102 | + } |
| 103 | + |
| 104 | + private static string GetParameterName(string templateSegment) => templateSegment.Trim('{', '}', '*').Split(':')[0]; |
| 105 | + |
| 106 | + private static PropertyInfo? GetParameterProperty(object instance, string propertyName) |
| 107 | + { |
| 108 | + var propertyInfos = instance.GetType() |
| 109 | + .GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 110 | + |
| 111 | + return Array.Find(propertyInfos, prop => prop.GetCustomAttributes(typeof(ParameterAttribute), true).Any() && |
| 112 | + string.Equals(prop.Name, propertyName, StringComparison.OrdinalIgnoreCase)); |
| 113 | + } |
| 114 | +} |
0 commit comments