Skip to content

Commit

Permalink
add new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sben65 committed Oct 19, 2022
1 parent 1b8f0f5 commit 734fd3c
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// 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.Tests.Unit.Client.Pages.EdgeDevices
{
using System.Collections.Generic;
using AngleSharp.Dom;
using AutoFixture;
using AzureIoTHub.Portal.Client.Exceptions;
using AzureIoTHub.Portal.Client.Models;
using AzureIoTHub.Portal.Client.Services;
using Models.v10;
using UnitTests.Bases;
using Bunit;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor;
using NUnit.Framework;
using AzureIoTHub.Portal.Client.Pages.EdgeDevices;

[TestFixture]
public class EdgeDeviceToDuplicateSelectorTests : BlazorUnitTest
{
private Mock<IEdgeDeviceClientService> mockEdgeDeviceClientService;
private Mock<IEdgeModelClientService> mockEdgeDeviceModelsClientService;
private Mock<ILoRaWanDeviceModelsClientService> mockLoRaWanDeviceModelsClientService;

public override void Setup()
{
base.Setup();

this.mockEdgeDeviceClientService = MockRepository.Create<IEdgeDeviceClientService>();
this.mockEdgeDeviceModelsClientService = MockRepository.Create<IEdgeModelClientService>();
this.mockLoRaWanDeviceModelsClientService = MockRepository.Create<ILoRaWanDeviceModelsClientService>();

_ = Services.AddSingleton(this.mockEdgeDeviceClientService.Object);
_ = Services.AddSingleton(this.mockEdgeDeviceModelsClientService.Object);
_ = Services.AddSingleton(this.mockLoRaWanDeviceModelsClientService.Object);

_ = Services.AddSingleton<IEdgeDeviceLayoutService, EdgeDeviceLayoutService>();
}

[Test]
public void EdgeDeviceToDuplicateSelectorShouldRenderCorrectly()
{
// Act
var cut = RenderComponent<EdgeDeviceToDuplicateSelector>();

// Assert
cut.WaitForAssertion(() => cut.FindAll("#search-device").Count.Should().Be(1));
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void TypingOnMudAutocompleteShouldTriggerSearch()
{
// Arrange
var query = Fixture.Create<string>();

var url = $"api/edge/devices?pageSize=10&searchText={query}";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(url))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>()
{
Items = new List<IoTEdgeListItem>
{
new()
{
DeviceId = Fixture.Create<string>()
}
}
});

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<EdgeDeviceToDuplicateSelector>();
var autocompleteComponent = cut.FindComponent<MudAutocomplete<IoTEdgeListItem>>();

// Act
autocompleteComponent.Find(TagNames.Input).Click();
autocompleteComponent.Find(TagNames.Input).Input(query);

// Assert
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(1));
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void SelectEdgeDeviceShouldDuplicateDeviceAndItsModel()
{
// Arrange
var query = Fixture.Create<string>();

var expectedDeviceModel = new IoTEdgeModel
{
ModelId = Fixture.Create<string>()
};

var expectedDevice = new IoTEdgeDevice
{
DeviceId = Fixture.Create<string>(),
ModelId = expectedDeviceModel.ModelId
};

var expectedDeviceItem = new IoTEdgeListItem
{
DeviceId = expectedDevice.DeviceId,
};

var url = $"api/edge/devices?pageSize=10&searchText={query}";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(url))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>()
{
Items = new List<IoTEdgeListItem>
{
expectedDeviceItem
}
});

_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevice(expectedDevice.DeviceId))
.ReturnsAsync(expectedDevice);

_ = this.mockEdgeDeviceModelsClientService.Setup(service => service.GetIoTEdgeModel(expectedDevice.ModelId))
.ReturnsAsync(expectedDeviceModel);

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<EdgeDeviceToDuplicateSelector>();

var autocompleteComponent = cut.FindComponent<MudAutocomplete<IoTEdgeListItem>>();
autocompleteComponent.Find(TagNames.Input).Click();
autocompleteComponent.Find(TagNames.Input).Input(query);
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(1));

// Act
var item = popoverProvider.Find("div.mud-list-item");
item.Click();

// Assert
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeFalse());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void SelectEdgeDeviceShouldProcessProblemDetailsExceptionWhenGettingDeviceDetails()
{
// Arrange
var query = Fixture.Create<string>();

var expectedDeviceModel = new IoTEdgeModel
{
ModelId = Fixture.Create<string>()
};

var expectedDevice = new IoTEdgeDevice
{
DeviceId = Fixture.Create<string>(),
ModelId = expectedDeviceModel.ModelId
};

var expectedDeviceItem = new IoTEdgeListItem
{
DeviceId = expectedDevice.DeviceId,
};

var url = $"api/edge/devices?pageSize=10&searchText={query}";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(url))
.ReturnsAsync(new PaginationResult<IoTEdgeListItem>()
{
Items = new List<IoTEdgeListItem>
{
expectedDeviceItem
}
});

_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevice(expectedDevice.DeviceId))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<EdgeDeviceToDuplicateSelector>();

var autocompleteComponent = cut.FindComponent<MudAutocomplete<IoTEdgeListItem>>();
autocompleteComponent.Find(TagNames.Input).Click();
autocompleteComponent.Find(TagNames.Input).Input(query);
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(1));

// Act
var item = popoverProvider.Find("div.mud-list-item");
item.Click();

// Assert
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeFalse());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void TypingOnMudAutocompleteShouldProcessProblemDetailsExceptionWhenTriggerSearch()
{
// Arrange
var query = Fixture.Create<string>();

var url = $"api/edge/devices?pageSize=10&searchText={query}";
_ = this.mockEdgeDeviceClientService.Setup(service => service.GetDevices(url))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var cut = RenderComponent<EdgeDeviceToDuplicateSelector>();
var autocompleteComponent = cut.FindComponent<MudAutocomplete<IoTEdgeListItem>>();

// Act
autocompleteComponent.Find(TagNames.Input).Click();
autocompleteComponent.Find(TagNames.Input).Input(query);

// Assert
cut.WaitForAssertion(() => autocompleteComponent.Instance.IsOpen.Should().BeTrue());
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
Variant="Variant.Outlined"
Required="true">
<ItemTemplate>
@*@context.DeviceName*@
<MudText Typo="Typo.subtitle1" Class="mud-input-helper-text">
Id: @context.DeviceId
</MudText>
@*<MudText Typo="Typo.subtitle1" Class="mud-input-helper-text">
LoRaWAN: @(@context.SupportLoRaFeatures ? "Yes" : "No")
</MudText>*@
</ItemTemplate>
<NoItemsTemplate>
<MudText Align="Align.Center" Class="px-4 py-1">
Expand Down

0 comments on commit 734fd3c

Please sign in to comment.