diff --git a/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/ClipboardServiceTests.cs b/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/ClipboardServiceTests.cs new file mode 100644 index 000000000..946d3e39b --- /dev/null +++ b/src/AzureIoTHub.Portal.Server.Tests.Unit/Services/ClipboardServiceTests.cs @@ -0,0 +1,50 @@ +using AzureIoTHub.Portal.Client.Services; +using Microsoft.JSInterop; +using Moq; +using NUnit.Framework; +using System; +using System.Linq; +using Microsoft.JSInterop.Infrastructure; + +namespace AzureIoTHub.Portal.Server.Tests.Unit.Services +{ + [TestFixture] + public class ClipboardServiceTests + { + private MockRepository mockRepository; + + private Mock mockJSRuntime; + + [SetUp] + public void SetUp() + { + this.mockRepository = new MockRepository(MockBehavior.Strict); + + this.mockJSRuntime = this.mockRepository.Create(); + } + + private ClipboardService CreateService() + { + return new ClipboardService( + this.mockJSRuntime.Object); + } + + [Test] + public void WriteTextAsync_StateUnderTest_ExpectedBehavior() + { + // Arrange + var service = this.CreateService(); + string text = Guid.NewGuid().ToString(); + + // Act + var result = service.WriteTextAsync(text); + + // Assert + this.mockJSRuntime.Verify(c => c.InvokeAsync( + It.Is(x => x == "navigator.clipboard.writeText"), + It.Is(x => x.Single().ToString() == text)), Times.Once); + + this.mockRepository.VerifyAll(); + } + } +} diff --git a/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor index 97ed9e5ac..5c8805f6c 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor @@ -1,8 +1,10 @@ -@using Microsoft.AspNetCore.Authorization +@using AzureIoTHub.Portal.Client.Services +@using Microsoft.AspNetCore.Authorization @using AzureIoTHub.Portal.Shared.Models.V10 @inject HttpClient Http @inject ISnackbar Snackbar +@inject ClipboardService ClipboardService
@@ -20,15 +22,15 @@ Service Endpoint - + Device Id - + Symmetric Key - + diff --git a/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/ConnectionStringDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/ConnectionStringDialog.razor index 023cc4225..e3ca1b9e3 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/ConnectionStringDialog.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/ConnectionStringDialog.razor @@ -1,8 +1,10 @@ -@using Microsoft.AspNetCore.Authorization +@using AzureIoTHub.Portal.Client.Services +@using Microsoft.AspNetCore.Authorization @using AzureIoTHub.Portal.Shared.Models.V10 @inject HttpClient Http @inject ISnackbar Snackbar +@inject ClipboardService ClipboardService
@@ -20,15 +22,15 @@ Service Endpoint - + Device Id - + Symmetric Key - + @@ -72,5 +74,6 @@ } } + void Cancel() => MudDialog.Cancel(); } diff --git a/src/AzureIoTHub.Portal/Client/Program.cs b/src/AzureIoTHub.Portal/Client/Program.cs index 32da9441e..d3762c2f3 100644 --- a/src/AzureIoTHub.Portal/Client/Program.cs +++ b/src/AzureIoTHub.Portal/Client/Program.cs @@ -7,7 +7,7 @@ namespace AzureIoTHub.Portal.Client using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; - + using AzureIoTHub.Portal.Client.Services; using AzureIoTHub.Portal.Shared.Settings; using Blazored.Modal; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; @@ -36,6 +36,7 @@ public static async Task Main(string[] args) builder.Services.AddBlazoredModal(); builder.Services.AddMudServices(); + builder.Services.AddScoped(); await ConfigureOidc(builder); diff --git a/src/AzureIoTHub.Portal/Client/Services/ClipboardService.cs b/src/AzureIoTHub.Portal/Client/Services/ClipboardService.cs new file mode 100644 index 000000000..d54d63d40 --- /dev/null +++ b/src/AzureIoTHub.Portal/Client/Services/ClipboardService.cs @@ -0,0 +1,23 @@ +// Copyright (c) CGI France. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace AzureIoTHub.Portal.Client.Services +{ + using Microsoft.JSInterop; + using System.Threading.Tasks; + + public class ClipboardService + { + private readonly IJSRuntime _jsRuntime; + + public ClipboardService(IJSRuntime jsRuntime) + { + _jsRuntime = jsRuntime; + } + + public ValueTask WriteTextAsync(string text) + { + return _jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", text); + } + } +}