diff --git a/src/BootstrapBlazor/Components/SweetAlert/SwalService.cs b/src/BootstrapBlazor/Components/SweetAlert/SwalService.cs index f0dff6cdaee9a33acea5b9dc4741593bec06d198..e797f0348f70ac537f7a415686514fec82361e32 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SwalService.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SwalService.cs @@ -9,9 +9,8 @@ namespace BootstrapBlazor.Components; /// /// SweetAlert 弹窗服务 /// -public class SwalService : BootstrapServiceBase, IDisposable +public class SwalService : BootstrapServiceBase { - private readonly IDisposable _optionsReloadToken; private BootstrapBlazorOptions _option; /// @@ -21,7 +20,6 @@ public class SwalService : BootstrapServiceBase, IDisposable public SwalService(IOptionsMonitor option) { _option = option.CurrentValue; - _optionsReloadToken = option.OnChange(op => _option = op); } /// @@ -38,25 +36,4 @@ public class SwalService : BootstrapServiceBase, IDisposable await Invoke(option, swal); } - - /// - /// - /// - /// - protected virtual void Dispose(bool disposing) - { - if (disposing) - { - _optionsReloadToken.Dispose(); - } - } - - /// - /// Dispose 方法 - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } } diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index fa4d45046f819534965fa9fa9ee852e26fe6db33..ebafc4e5b2d6a567190cd8323891a554eb16c271 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -131,6 +131,5 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddSingleton, ConfigurationChangeTokenSource>(); services.TryAddSingleton, ConfigureOptions>(); return services; - } } diff --git a/src/BootstrapBlazor/Services/WebClientService.cs b/src/BootstrapBlazor/Services/WebClientService.cs index e4a116799416bfc1b0a8837dae073a14131f30ab..9eb9e8d4e34965d1f3e17194c6400611a42929c9 100644 --- a/src/BootstrapBlazor/Services/WebClientService.cs +++ b/src/BootstrapBlazor/Services/WebClientService.cs @@ -8,7 +8,7 @@ using Microsoft.JSInterop; namespace BootstrapBlazor.Components; /// -/// +/// WebClient 服务类 /// public class WebClientService : IDisposable { @@ -33,7 +33,7 @@ public class WebClientService : IDisposable public WebClientService(IJSRuntime runtime, NavigationManager navigation) => (_runtime, _navigation) = (runtime, navigation); /// - /// + /// 获得 ClientInfo 实例方法 /// /// public async Task GetClientInfo() @@ -54,7 +54,7 @@ public class WebClientService : IDisposable } /// - /// + /// SetData 方法由 JS 调用 /// /// /// @@ -73,7 +73,7 @@ public class WebClientService : IDisposable Client.Ip = ip; Client.OS = os; Client.Browser = browser; - Client.Device = ParseDeviceType(device); + Client.Device = WebClientService.ParseDeviceType(device); Client.Language = language; Client.Engine = engine; Client.UserAgent = agent; @@ -81,7 +81,7 @@ public class WebClientService : IDisposable ReturnTask?.TrySetResult(true); } - private WebClientDeviceType ParseDeviceType(string device) + private static WebClientDeviceType ParseDeviceType(string device) { var ret = WebClientDeviceType.PC; if (Enum.TryParse(device, true, out var d)) diff --git a/test/UnitTest/Core/TableTestBase.cs b/test/UnitTest/Core/TableTestBase.cs index a8dd1cede024892a9f794f1981426a15e85c9f10..53546595e2ec5df7dddf44519d99e4fa078281c4 100644 --- a/test/UnitTest/Core/TableTestBase.cs +++ b/test/UnitTest/Core/TableTestBase.cs @@ -48,7 +48,6 @@ public class TableTestHost : IDisposable { services.AddBootstrapBlazor(op => op.ToastDelay = 2000); services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { typeof(Alert).Assembly }); - services.AddSingleton(); } protected virtual void ConfigureConfigration(IServiceCollection services) diff --git a/test/UnitTest/Services/BootstrapServiceBaseTest.cs b/test/UnitTest/Services/BootstrapServiceBaseTest.cs index 47707e330c89cd0bac69e89fbfa81a6abb647587..ac9d43afa83e19a2a7ca696d90bf913788be60e8 100644 --- a/test/UnitTest/Services/BootstrapServiceBaseTest.cs +++ b/test/UnitTest/Services/BootstrapServiceBaseTest.cs @@ -25,7 +25,7 @@ public class BootstrapServiceBaseTest private class MockBootstrapService : BootstrapServiceBase { - private Alert Alert { get; set; } = new Alert(); + private Alert Alert { get; } = new Alert(); public async Task Test1(TOption option, Func callback) { diff --git a/test/UnitTest/Services/LookupServiceTest.cs b/test/UnitTest/Services/LookupServiceTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..630f9022ee55f9a29845469ad4517760be58d225 --- /dev/null +++ b/test/UnitTest/Services/LookupServiceTest.cs @@ -0,0 +1,17 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.Extensions.DependencyInjection; + +namespace UnitTest.Services; + +public class LookupServiceTest : MessageTestBase +{ + [Fact] + public void LookupService_Null() + { + var service = Context.Services.GetRequiredService(); + Assert.Null(service.GetItemsByKey("")); + } +} diff --git a/test/UnitTest/Services/WebClientServiceTest.cs b/test/UnitTest/Services/WebClientServiceTest.cs new file mode 100644 index 0000000000000000000000000000000000000000..f40d12f66e52fac9320099307424f188fc70ccde --- /dev/null +++ b/test/UnitTest/Services/WebClientServiceTest.cs @@ -0,0 +1,42 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.Extensions.DependencyInjection; + +namespace UnitTest.Services; + +public class WebClientServiceTest : BootstrapBlazorTestBase +{ + [Fact] + public async Task WebClientService_Ok() + { + var service = Context.Services.GetRequiredService(); + service.SetData("test_id", "192.168.0.1", "ios", "chrome", "mobile", "zh", "engine", "test_agent"); + ClientInfo? client = null; + _ = Task.Run(async () => client = await service.GetClientInfo()); + while (client == null) + { + await Task.Delay(100); + service.SetData("test_id", "192.168.0.1", "ios", "chrome", "mobile", "zh", "engine", "test_agent"); + } + client.City = "test_city"; + client.RequestUrl = "test_url"; + Assert.Equal("test_id", client.Id); + Assert.Equal("192.168.0.1", client.Ip); + Assert.Equal("ios", client.OS); + Assert.Equal("chrome", client.Browser); + Assert.Equal(WebClientDeviceType.Mobile, client.Device); + Assert.Equal("zh", client.Language); + Assert.Equal("engine", client.Engine); + Assert.Equal("test_agent", client.UserAgent); + Assert.Equal("test_city", client.City); + Assert.Equal("test_url", client.RequestUrl); + } + + [Fact] + public void WebClientService_Dispose() + { + using var service = Context.Services.GetRequiredService(); + } +}