-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a processing dialog while waiting for an action to be performed (#…
…640) * Disable button while processing on DeviceDetailPage * Show dialog while processing on DeviceDetailPage * Show dialog while processing on DeviceModelDetailPage * Show dialog while processing on CreateDevicePage * Show dialog while processing on CreateDeviceModelPage * Show dialog while processing on EdgeDeviceDetailPage * Disable button while processing on CreateEdgeDeviceDialog * Show dialog while processing on Concentrator Pages * Show dialog while processing on DeviceTagsPage * Fix unit test on DeviceModelDetailPage * Fix unit test on CreateDeviceModelPage * OnInitializedAsync changed to OnInitialized * Setting DialogParameters changed * Addes unit test on ProcessingDialog * Fix code scanning errors * Unit test on CreateDevicePage * Delete useless variables * Fix rebase error * Add html id on DeviceID field in CreateDevicePage * Fix ClickOnSaveShouldPostDeviceDetailsAsync * Add test on DeviceDetailPage + fix an issue occuring when a DeviceDetail is missing some tags * ClickOnSaveShouldPutEdgeDeviceDetails draft * Fix ClickOnSaveShouldPutEdgeDeviceDetails * Add ClickOnSaveShouldPutEdgeDeviceDetails() on ConcentratorDetailPageTests * Fix ClickOnSaveShouldPutEdgeDeviceDetails() on EdgeDeviceDetailPageTests * Add DeviceDtailPageTests : ClickOnSaveShouldDisplaySnackbarIfValidationError() * EdgeDeviceDetailPage Tests on UpdateDevice() * Add tests on EdgeDeviceDetailPage * Fix unit tests * Added test on EdgeDeviceDetailPage * Add tests on DeviceDetailPage * Fix typo on DeviceDetailPageTest * Fix mockSnackbarService.Setup return type * Add test on Save() function on DeviceTagsPage * Tests on CreateConcentratorPage * Fix code scanning errors * Fix "Dereferenced variable may be null" exceptions
- Loading branch information
1 parent
a09296f
commit fc0f99b
Showing
22 changed files
with
1,995 additions
and
224 deletions.
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
src/AzureIoTHub.Portal.Server.Tests.Unit/Pages/Devices/CreateDevicePageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// 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.Server.Tests.Unit.Pages | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AzureIoTHub.Portal.Client.Pages.Devices; | ||
using AzureIoTHub.Portal.Client.Shared; | ||
using AzureIoTHub.Portal.Models.v10; | ||
using AzureIoTHub.Portal.Server.Tests.Unit.Helpers; | ||
using Bunit; | ||
using Bunit.TestDoubles; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using MudBlazor; | ||
using MudBlazor.Interop; | ||
using MudBlazor.Services; | ||
using NUnit.Framework; | ||
using RichardSzalay.MockHttp; | ||
|
||
[TestFixture] | ||
public class CreateDevicePageTests : IDisposable | ||
{ | ||
private Bunit.TestContext testContext; | ||
private MockHttpMessageHandler mockHttpClient; | ||
|
||
private MockRepository mockRepository; | ||
private Mock<IDialogService> mockDialogService; | ||
|
||
private FakeNavigationManager mockNavigationManager; | ||
|
||
private static string ApiBaseUrl => "/api/devices"; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
this.testContext = new Bunit.TestContext(); | ||
|
||
this.mockRepository = new MockRepository(MockBehavior.Strict); | ||
this.mockDialogService = this.mockRepository.Create<IDialogService>(); | ||
this.mockHttpClient = this.testContext.Services | ||
.AddMockHttpClient(); | ||
|
||
_ = this.testContext.Services.AddSingleton(this.mockDialogService.Object); | ||
|
||
_ = this.testContext.Services.AddMudServices(); | ||
|
||
_ = this.testContext.JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true); | ||
_ = this.testContext.JSInterop.SetupVoid("mudPopover.connect", _ => true); | ||
_ = this.testContext.JSInterop.SetupVoid("Blazor._internal.InputFile.init", _ => true); | ||
_ = this.testContext.JSInterop.Setup<BoundingClientRect>("mudElementRef.getBoundingClientRect", _ => true); | ||
_ = this.testContext.JSInterop.Setup<IEnumerable<BoundingClientRect>>("mudResizeObserver.connect", _ => true); | ||
|
||
this.mockNavigationManager = this.testContext.Services.GetRequiredService<FakeNavigationManager>(); | ||
|
||
this.mockHttpClient.AutoFlush = true; | ||
} | ||
|
||
private IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) | ||
where TComponent : IComponent | ||
{ | ||
return this.testContext.RenderComponent<TComponent>(parameters); | ||
} | ||
|
||
[Test] | ||
public async Task ClickOnSaveShouldPostDeviceDetailsAsync() | ||
{ | ||
var mockDeviceModel = new DeviceModel | ||
{ | ||
ModelId = Guid.NewGuid().ToString(), | ||
Description = Guid.NewGuid().ToString(), | ||
SupportLoRaFeatures = false, | ||
Name = Guid.NewGuid().ToString() | ||
}; | ||
|
||
var expectedDeviceDetails = new DeviceDetails | ||
{ | ||
DeviceName = Guid.NewGuid().ToString(), | ||
ModelId = mockDeviceModel.ModelId, | ||
DeviceID = Guid.NewGuid().ToString(), | ||
}; | ||
|
||
|
||
_ = this.mockHttpClient.When(HttpMethod.Post, $"{ApiBaseUrl}") | ||
.With(m => | ||
{ | ||
Assert.IsAssignableFrom<ObjectContent<DeviceDetails>>(m.Content); | ||
var objectContent = m.Content as ObjectContent<DeviceDetails>; | ||
Assert.IsNotNull(objectContent); | ||
Assert.IsAssignableFrom<DeviceDetails>(objectContent.Value); | ||
var deviceDetails = objectContent.Value as DeviceDetails; | ||
Assert.IsNotNull(deviceDetails); | ||
Assert.AreEqual(expectedDeviceDetails.DeviceID, deviceDetails.DeviceID); | ||
Assert.AreEqual(expectedDeviceDetails.DeviceName, deviceDetails.DeviceName); | ||
Assert.AreEqual(expectedDeviceDetails.ModelId, deviceDetails.ModelId); | ||
return true; | ||
}) | ||
.RespondText(string.Empty); | ||
|
||
_ = this.mockHttpClient.When(HttpMethod.Get, $"/api/models") | ||
.RespondJson(new DeviceModel[] | ||
{ | ||
mockDeviceModel | ||
}); | ||
|
||
_ = this.mockHttpClient.When(HttpMethod.Get, $"/api/settings/device-tags") | ||
.RespondJson(new List<DeviceTag>() | ||
{ | ||
new DeviceTag() | ||
{ | ||
Label = Guid.NewGuid().ToString(), | ||
Name = Guid.NewGuid().ToString(), | ||
Required = false, | ||
Searchable = false | ||
} | ||
}); | ||
|
||
_ = this.mockHttpClient.When(HttpMethod.Get, $"/api/models/{mockDeviceModel.ModelId}/properties") | ||
.RespondJson(Array.Empty<DeviceProperty>()); | ||
|
||
_ = this.mockHttpClient.When(HttpMethod.Post, $"{ApiBaseUrl}/{expectedDeviceDetails.DeviceID}/properties") | ||
.RespondText(string.Empty); | ||
|
||
var cut = RenderComponent<CreateDevicePage>(); | ||
Thread.Sleep(2500); | ||
var saveButton = cut.WaitForElement("#SaveButton"); | ||
|
||
var mockDialogReference = new DialogReference(Guid.NewGuid(), this.mockDialogService.Object); | ||
|
||
_ = this.mockDialogService.Setup(c => c.Show<ProcessingDialog>("Processing", It.IsAny<DialogParameters>())) | ||
.Returns(mockDialogReference); | ||
|
||
_ = this.mockDialogService.Setup(c => c.Close(It.Is<DialogReference>(x => x == mockDialogReference))); | ||
|
||
// Act | ||
cut.Find($"#{nameof(DeviceDetails.DeviceName)}").Change(expectedDeviceDetails.DeviceName); | ||
cut.Find($"#{nameof(DeviceDetails.DeviceID)}").Change(expectedDeviceDetails.DeviceID); | ||
await cut.Instance.ChangeModel(mockDeviceModel); | ||
|
||
saveButton.Click(); | ||
Thread.Sleep(2500); | ||
cut.WaitForState(() => | ||
{ | ||
return this.mockNavigationManager.Uri.EndsWith("/devices", StringComparison.OrdinalIgnoreCase); | ||
}); | ||
|
||
// Assert | ||
this.mockHttpClient.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.