Skip to content

Commit

Permalink
test deviceTagsPage
Browse files Browse the repository at this point in the history
  • Loading branch information
crib authored and kbeaugrand committed Jul 29, 2022
1 parent 2bc6d9d commit 8d35ebb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.Settings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//using System.Threading.Tasks;
using AutoFixture;
using AzureIoTHub.Portal.Client.Exceptions;
using AzureIoTHub.Portal.Client.Models;
Expand All @@ -19,6 +19,7 @@ namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages.Settings
using Moq;
using MudBlazor;
using NUnit.Framework;
using System.Threading.Tasks;

[TestFixture]
public class DeviceTagsPageTests : BlazorUnitTest
Expand Down Expand Up @@ -125,33 +126,56 @@ public void OnInitializedAsyncShouldProcessProblemDetailsExceptionWhenIssueOccur


[Test]
public void ClickOnSaveShouldUpdateTagList()
public void ClickOnSaveShouldCreateUpdateTag()
{
var expectedTags = new List<DeviceTag>
{
new() {
Label = "Label",
Name = "Name",
Required = false,
Searchable = false
}
};

//Arrange
_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags())
.ReturnsAsync(expectedTags);

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.UpdateDeviceTags(It.IsAny<List<DeviceTag>>()))
.ReturnsAsync(new List<DeviceTag>
{
new()
{
Label = "tagLabel", Name = "tagName", Required = false,
Searchable = false
}
});
_ = this.mockDeviceTagSettingsClientService.Setup(service => service.CreateOrUpdateDeviceTag(It.IsAny<DeviceTag>()))
.Returns(Task.CompletedTask);

_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, null)).Returns((Snackbar)null);


var cut = RenderComponent<DeviceTagsPage>();
cut.WaitForAssertion(() => cut.Find("#saveButton"));
cut.WaitForAssertion(() => cut.Markup.Should().NotContain("Loading..."));

// Act
cut.Find("#saveButton").Click();
cut.WaitForElement("#saveButton").Click();

cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnDeleteShouldDeleteTag()
{
//Arrange
const string deviceTagName = "tagName";

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>
{
new()
{
Label = "tagLabel", Name = deviceTagName, Required = false,
Searchable = false
}
});
_ = this.mockDeviceTagSettingsClientService.Setup(service => service.DeleteDeviceTagByName(deviceTagName))
.Returns(Task.CompletedTask);

var cut = RenderComponent<DeviceTagsPage>();
cut.WaitForAssertion(() => cut.Markup.Should().NotContain("Loading..."));

// Act
cut.WaitForElement("#deleteButton").Click();

cut.WaitForAssertion(() => MockRepository.VerifyAll());
}
Expand Down Expand Up @@ -232,7 +256,7 @@ public void ClickOnSaveShouldProcessProblemDetailsExceptionIfIssueOccursWhenUpda
new () { Label = "Label", Name = "Name", Required = false, Searchable = false }
});

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.UpdateDeviceTags(It.IsAny<List<DeviceTag>>()))
_ = this.mockDeviceTagSettingsClientService.Setup(service => service.CreateOrUpdateDeviceTag(It.IsAny<DeviceTag>()))
.ThrowsAsync(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
{
using System.Collections.Generic;
//using System.Collections.Generic;
using System.Linq;
using System.Net;
//using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using AutoFixture;
Expand Down Expand Up @@ -49,28 +49,28 @@ public async Task GetDeviceTagsShouldReturnDeviceTags()
MockHttpClient.VerifyNoOutstandingExpectation();
}

[Test]
public async Task UpdateDeviceTagsShouldUpdateDeviceTags()
{
// Arrange
var expectedDeviceTags = Fixture.Build<DeviceTag>().CreateMany(3).ToList();
//[Test]
//public async Task UpdateDeviceTagsShouldUpdateDeviceTags()
//{
// // Arrange
// var expectedDeviceTags = Fixture.Build<DeviceTag>().CreateMany(3).ToList();

_ = MockHttpClient.When(HttpMethod.Post, "/api/settings/device-tags")
.With(m =>
{
_ = m.Content.Should().BeAssignableTo<ObjectContent<IList<DeviceTag>>>();
var tags = m.Content as ObjectContent<IList<DeviceTag>>;
_ = tags.Value.Should().BeEquivalentTo(expectedDeviceTags);
return true;
})
.Respond(HttpStatusCode.Created);
// _ = MockHttpClient.When(HttpMethod.Post, "/api/settings/device-tags")
// .With(m =>
// {
// _ = m.Content.Should().BeAssignableTo<ObjectContent<IList<DeviceTag>>>();
// var tags = m.Content as ObjectContent<IList<DeviceTag>>;
// _ = tags.Value.Should().BeEquivalentTo(expectedDeviceTags);
// return true;
// })
// .Respond(HttpStatusCode.Created);

// Act
await this.deviceTagSettingsClientService.UpdateDeviceTags(expectedDeviceTags);
// // Act
// await this.deviceTagSettingsClientService.UpdateDeviceTags(expectedDeviceTags);

// Assert
MockHttpClient.VerifyNoOutstandingRequest();
MockHttpClient.VerifyNoOutstandingExpectation();
}
// // Assert
// MockHttpClient.VerifyNoOutstandingRequest();
// MockHttpClient.VerifyNoOutstandingExpectation();
//}
}
}
20 changes: 16 additions & 4 deletions src/AzureIoTHub.Portal/Client/Pages/Settings/DeviceTagsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudTooltip Text="Delete device">
<MudIconButton Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(() => DeleteTag(TagContexte))"></MudIconButton>
<MudIconButton Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(() => DeleteTag(TagContexte))" id="deleteButton"></MudIconButton>
</MudTooltip>
</MudTd>
<MudTd DataLabel="Validate" Style="text-align: center;">
Expand Down Expand Up @@ -135,7 +135,7 @@
}
}

private async Task Save(DeviceTag item)
private async Task Save(DeviceTag deviceTag)
{
try
{
Expand All @@ -144,7 +144,19 @@
await FormLabel.Validate();
await FormName.Validate();

if (!FormLabel.IsValid || !FormName.IsValid)
// Checks duplicate
bool duplicated = false;
var query = Tags.GroupBy(x => x.Name)
.Where(x => x.Count() > 1)
.Select(x => x.Key)
.ToList();
foreach (var item in query)
{
Snackbar.Add($"The name '{item}' appears more than once!", Severity.Warning);
duplicated = true;
}

if (!FormLabel.IsValid || !FormName.IsValid || duplicated)
{
Snackbar.Add("One or more validation errors occurred", Severity.Error);

Expand All @@ -154,7 +166,7 @@
}
else
{
await DeviceTagSettingsClientService.CreateOrUpdateDeviceTag(item);
await DeviceTagSettingsClientService.CreateOrUpdateDeviceTag(deviceTag);
}

Snackbar.Add($"Settings have been successfully updated!", Severity.Success);
Expand Down

0 comments on commit 8d35ebb

Please sign in to comment.