Skip to content

Commit

Permalink
Fix Device Model list page loading since portal settings changes (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored Mar 12, 2022
1 parent fd9a56b commit 300b7b1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages
using System.Net.Http;
using AzureIoTHub.Portal.Client.Pages.DeviceModels;
using AzureIoTHub.Portal.Server.Tests.Unit.Helpers;
using AzureIoTHub.Portal.Shared.Models.V10;
using AzureIoTHub.Portal.Shared.Models.V10.DeviceModel;
using Bunit;
using Bunit.TestDoubles;
Expand All @@ -26,11 +27,9 @@ public class DeviceModelListPageTests : IDisposable

private MockRepository mockRepository;
private Mock<IDialogService> mockDialogService;
private FakeNavigationManager mockNavigationManager;
private MockHttpMessageHandler mockHttpClient;

private readonly string apiBaseUrl = "/api/models";
private readonly string apiSettingsBaseUrl = "/api/settings/lora";

[SetUp]
public void SetUp()
Expand All @@ -49,8 +48,6 @@ public void SetUp()
_ = testContext.JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true);
_ = testContext.JSInterop.SetupVoid("mudPopover.connect", _ => true);
_ = testContext.JSInterop.Setup<BoundingClientRect>("mudElementRef.getBoundingClientRect", _ => true);

mockNavigationManager = testContext.Services.GetRequiredService<FakeNavigationManager>();
}

private IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters)
Expand All @@ -69,9 +66,7 @@ public void WhenLoraFeatureEnableDeviceDetailLinkShouldContainLora()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceModel[] { new DeviceModel { ModelId = deviceId, SupportLoRaFeatures = true } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(true);
_ = testContext.Services.AddSingleton(new PortalSettings { IsLoRaSupported = true });

var cut = RenderComponent<DeviceModelListPage>();
_ = cut.WaitForElements(".detail-link");
Expand All @@ -97,9 +92,7 @@ public void WhenLoraFeatureDisableDeviceDetailLinkShouldNotContainLora()
.When(HttpMethod.Get, apiBaseUrl)
.RespondJson(new DeviceModel[] { new DeviceModel { ModelId = deviceId, SupportLoRaFeatures = true } });

_ = this.mockHttpClient
.When(HttpMethod.Get, apiSettingsBaseUrl)
.RespondJson(false);
_ = testContext.Services.AddSingleton(new PortalSettings { IsLoRaSupported = false });

var cut = RenderComponent<DeviceModelListPage>();
_ = cut.WaitForElements(".detail-link");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/device-models"
@using AzureIoTHub.Portal.Shared.Models.V10
@using AzureIoTHub.Portal.Shared.Models.V10.DeviceModel
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
Expand All @@ -10,6 +11,7 @@
@attribute [Authorize]
@inject HttpClient httpClient
@inject NavigationManager navigationManager
@inject PortalSettings Portal
@inject ISnackbar snackBar
@inject IDialogService dialogService

Expand Down Expand Up @@ -51,13 +53,13 @@
<img height="25" src="@context.ImageUrl" />
</MudTd>
<MudTd DataLabel="Name" Style="word-break: break-all;">
<a class="detail-link" href=@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")>@context.Name</a>
<a class="detail-link" href=@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && Portal.IsLoRaSupported) ? "?isLora=true": "")}")>@context.Name</a>
</MudTd>
<MudTd DataLabel="Description" Style="word-break: break-all; ">
@context.Description
</MudTd>
<MudTd DataLabel="Details" Style="text-align: center">
<a class="detail-link" href="@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && isLoraEnable) ? "?isLora=true": "")}")"><MudIconButton Icon="@Icons.Filled.Visibility" Color="Color.Default" /></a>
<a class="detail-link" href="@($"device-models/{@context.ModelId}{((@context.SupportLoRaFeatures && Portal.IsLoRaSupported) ? "?isLora=true": "")}")"><MudIconButton Icon="@Icons.Filled.Visibility" Color="Color.Default" /></a>
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudIconButton Color="Color.Default" Disabled="context.IsBuiltin" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(e => DeleteDeviceModel(context))"></MudIconButton>
Expand All @@ -78,11 +80,9 @@
@code {
private DeviceModel[] result;
private int[] pageSizeOptions = new int[] { 2, 5, 10 };
private bool isLoraEnable;

protected override async Task OnInitializedAsync()
{
isLoraEnable = await this.httpClient.GetFromJsonAsync<bool>("api/settings/lora");
await LoadDeviceModels();
}

Expand Down Expand Up @@ -122,4 +122,4 @@
// Update the list of devices after the deletion
await LoadDeviceModels();
}
}
}

0 comments on commit 300b7b1

Please sign in to comment.