|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Bunit; |
| 7 | +using Microsoft.AspNetCore.Components; |
| 8 | +using Microsoft.AspNetCore.Components.Web; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging.Abstractions; |
| 11 | +using Microsoft.JSInterop; |
| 12 | +using Xunit; |
| 13 | +using static Radzen.Blazor.Tests.ChartTestHelper; |
| 14 | + |
| 15 | +namespace Radzen.Blazor.Tests |
| 16 | +{ |
| 17 | + public class ChartTooltipPrerenderTests |
| 18 | + { |
| 19 | + class PrerenderJSRuntime : IJSRuntime |
| 20 | + { |
| 21 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) => |
| 22 | + throw new InvalidOperationException("JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered."); |
| 23 | + |
| 24 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args) => |
| 25 | + InvokeAsync<TValue>(identifier, args); |
| 26 | + } |
| 27 | + |
| 28 | + class TestNavigationManager : NavigationManager |
| 29 | + { |
| 30 | + public TestNavigationManager() |
| 31 | + { |
| 32 | + Initialize("http://localhost/", "http://localhost/"); |
| 33 | + } |
| 34 | + |
| 35 | + protected override void NavigateToCore(string uri, bool forceLoad) |
| 36 | + { |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task DisposeAsync_Completes_WhenChartTooltipIsOpenedDuringPrerendering() |
| 42 | + { |
| 43 | + var services = new ServiceCollection(); |
| 44 | + services.AddSingleton<IJSRuntime, PrerenderJSRuntime>(); |
| 45 | + services.AddSingleton<NavigationManager, TestNavigationManager>(); |
| 46 | + services.AddScoped<TooltipService>(); |
| 47 | + |
| 48 | + var provider = services.BuildServiceProvider(); |
| 49 | + var scope = provider.CreateAsyncScope(); |
| 50 | + |
| 51 | + var renderer = new HtmlRenderer(scope.ServiceProvider, NullLoggerFactory.Instance); |
| 52 | + var service = scope.ServiceProvider.GetRequiredService<TooltipService>(); |
| 53 | + |
| 54 | + await renderer.Dispatcher.InvokeAsync(async () => |
| 55 | + { |
| 56 | + await renderer.RenderComponentAsync<RadzenChartTooltip>(); |
| 57 | + |
| 58 | + service.OpenChartTooltip(default, 10, 10, _ => builder => builder.AddContent(0, "tooltip"), new ChartTooltipOptions()); |
| 59 | + }); |
| 60 | + |
| 61 | + var dispose = Task.Run(async () => |
| 62 | + { |
| 63 | + await renderer.DisposeAsync(); |
| 64 | + await scope.DisposeAsync(); |
| 65 | + await provider.DisposeAsync(); |
| 66 | + }); |
| 67 | + |
| 68 | + var completed = await Task.WhenAny(dispose, Task.Delay(TimeSpan.FromSeconds(10))); |
| 69 | + |
| 70 | + Assert.True(completed == dispose, "RadzenChartTooltip.DisposeAsync did not complete after a chart tooltip was opened during prerendering."); |
| 71 | + |
| 72 | + await dispose; |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void Sparkline_RenderedWithoutMouseInteraction_DoesNotOpenTooltip() |
| 77 | + { |
| 78 | + using var ctx = CreateChartContext(); |
| 79 | + var tooltipService = ctx.Services.GetRequiredService<TooltipService>(); |
| 80 | + var opens = new List<(double x, double y)>(); |
| 81 | + tooltipService.OnOpenChartTooltip += (element, x, y, options) => opens.Add((x, y)); |
| 82 | + |
| 83 | + var data = new[] |
| 84 | + { |
| 85 | + new DataItem { Category = "Quiz 1", Value = 92 }, |
| 86 | + new DataItem { Category = "Quiz 2", Value = 58 }, |
| 87 | + new DataItem { Category = "Quiz 3", Value = 52 }, |
| 88 | + new DataItem { Category = "Quiz 4", Value = 61 }, |
| 89 | + }; |
| 90 | + |
| 91 | + ctx.RenderComponent<RadzenSparkline>(p => p |
| 92 | + .Add(c => c.Style, "width: 100px; height: 20px") |
| 93 | + .AddChildContent<RadzenLineSeries<DataItem>>(s => s |
| 94 | + .Add(x => x.Smooth, true) |
| 95 | + .Add(x => x.CategoryProperty, nameof(DataItem.Category)) |
| 96 | + .Add(x => x.ValueProperty, nameof(DataItem.Value)) |
| 97 | + .Add(x => x.Data, data)) |
| 98 | + .AddChildContent<RadzenCategoryAxis>(a => a |
| 99 | + .Add(x => x.Visible, false) |
| 100 | + .Add(x => x.Padding, -20))); |
| 101 | + |
| 102 | + Assert.Empty(opens); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments