Skip to content

Commit

Permalink
Add unit tests on device create/detail pages about save and duplicate…
Browse files Browse the repository at this point in the history
… feature #1001 (#1163)

* Add unit tests on DeviceToDuplicateSelector #1001

* Add unit tests on create device page about save and new/duplicate #1001

* Add unit test on device details page about duplicate device #1001
  • Loading branch information
hocinehacherouf authored Sep 7, 2022
1 parent 4f566cc commit 620f339
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,157 @@ public async Task ChangeModelShouldProcessProblemDetailsExceptionWhenIssueOccurs
// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public async Task ClickOnSaveAndAddNewShouldCreateDeviceAndResetCreateDevicePage()
{
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.mockDeviceClientService.Setup(service => service.CreateDevice(It.Is<DeviceDetails>(details => expectedDeviceDetails.DeviceID.Equals(details.DeviceID, StringComparison.Ordinal))))
.Returns(Task.CompletedTask);

_ = this.mockDeviceModelsClientService.Setup(service => service.GetDeviceModels())
.ReturnsAsync(new List<DeviceModel>
{
mockDeviceModel
});

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>
{
new()
{
Label = Guid.NewGuid().ToString(),
Name = Guid.NewGuid().ToString(),
Required = false,
Searchable = false
}
});

_ = this.mockDeviceModelsClientService
.Setup(service => service.GetDeviceModelModelProperties(mockDeviceModel.ModelId))
.ReturnsAsync(new List<DeviceProperty>());

_ = this.mockDeviceClientService
.Setup(service => service.SetDeviceProperties(expectedDeviceDetails.DeviceID, It.IsAny<IList<DevicePropertyValue>>()))
.Returns(Task.CompletedTask);

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<CreateDevicePage>();
var saveButton = cut.WaitForElement("#SaveButton");

cut.WaitForElement($"#{nameof(DeviceDetails.DeviceName)}").Change(expectedDeviceDetails.DeviceName);
cut.WaitForElement($"#{nameof(DeviceDetails.DeviceID)}").Change(expectedDeviceDetails.DeviceID);
await cut.Instance.ChangeModel(mockDeviceModel);

var mudButtonGroup = cut.FindComponent<MudButtonGroup>();

mudButtonGroup.Find(".mud-menu button").Click();

popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(3));

var items = popoverProvider.FindAll("div.mud-list-item");

// Click on Save and New
items[1].Click();

// Act
saveButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
cut.WaitForAssertion(() => cut.Find($"#{nameof(DeviceDetails.DeviceName)}").TextContent.Should().BeEmpty());
cut.WaitForAssertion(() => cut.Find($"#{nameof(DeviceDetails.DeviceID)}").TextContent.Should().BeEmpty());
cut.WaitForAssertion(() => this.mockNavigationManager.Uri.Should().NotEndWith("/devices"));
}

[Test]
public async Task ClickOnSaveAndDuplicateShouldCreateDeviceAndDuplicateDeviceDetailsInCreateDevicePage()
{
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.mockDeviceClientService.Setup(service => service.CreateDevice(It.Is<DeviceDetails>(details => expectedDeviceDetails.DeviceID.Equals(details.DeviceID, StringComparison.Ordinal))))
.Returns(Task.CompletedTask);

_ = this.mockDeviceModelsClientService.Setup(service => service.GetDeviceModels())
.ReturnsAsync(new List<DeviceModel>
{
mockDeviceModel
});

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>
{
new()
{
Label = Guid.NewGuid().ToString(),
Name = Guid.NewGuid().ToString(),
Required = false,
Searchable = false
}
});

_ = this.mockDeviceModelsClientService
.Setup(service => service.GetDeviceModelModelProperties(mockDeviceModel.ModelId))
.ReturnsAsync(new List<DeviceProperty>());

_ = this.mockDeviceClientService
.Setup(service => service.SetDeviceProperties(expectedDeviceDetails.DeviceID, It.IsAny<IList<DevicePropertyValue>>()))
.Returns(Task.CompletedTask);

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<CreateDevicePage>();
var saveButton = cut.WaitForElement("#SaveButton");

cut.WaitForElement($"#{nameof(DeviceDetails.DeviceName)}").Change(expectedDeviceDetails.DeviceName);
cut.WaitForElement($"#{nameof(DeviceDetails.DeviceID)}").Change(expectedDeviceDetails.DeviceID);
await cut.Instance.ChangeModel(mockDeviceModel);

var mudButtonGroup = cut.FindComponent<MudButtonGroup>();

mudButtonGroup.Find(".mud-menu button").Click();

popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(3));

var items = popoverProvider.FindAll("div.mud-list-item");

// Click on Save and Duplicate
items[2].Click();

// Act
saveButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
cut.WaitForAssertion(() => cut.Find($"#{nameof(DeviceDetails.DeviceName)}").TextContent.Should().BeEmpty());
cut.WaitForAssertion(() => cut.Find($"#{nameof(DeviceDetails.DeviceID)}").TextContent.Should().BeEmpty());
cut.WaitForAssertion(() => this.mockNavigationManager.Uri.Should().NotEndWith("/devices"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
using MudBlazor.Services;
using NUnit.Framework;
using UnitTests.Mocks;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Client.Pages.DeviceModels;

[TestFixture]
Expand Down Expand Up @@ -533,5 +533,76 @@ public void ClickOnDeleteShouldDisplayConfirmationDialogAndRedirectIfConfirmed()
cut.WaitForState(() => this.mockNavigationManager.Uri.EndsWith("/devices", StringComparison.OrdinalIgnoreCase));
cut.WaitForAssertion(() => MockRepository.VerifyAll());
}

[Test]
public void ClickOnDuplicateShouldDuplicateDeviceDetailAndRedirectToCreateDevicePage()
{
var mockDeviceModel = new DeviceModel
{
ModelId = Guid.NewGuid().ToString(),
Description = Guid.NewGuid().ToString(),
SupportLoRaFeatures = false,
Name = Guid.NewGuid().ToString()
};

var mockTag = new DeviceTag
{
Label = Guid.NewGuid().ToString(),
Name = Guid.NewGuid().ToString(),
Required = false,
Searchable = false
};

var mockDeviceDetails = new DeviceDetails
{
DeviceName = Guid.NewGuid().ToString(),
ModelId = mockDeviceModel.ModelId,
DeviceID = Guid.NewGuid().ToString(),
Tags = new Dictionary<string, string>()
{
{mockTag.Name,Guid.NewGuid().ToString()}
}
};

_ = this.mockDeviceClientService
.Setup(service => service.GetDevice(mockDeviceDetails.DeviceID))
.ReturnsAsync(mockDeviceDetails);

_ = this.mockDeviceModelsClientService.Setup(service => service.GetDeviceModel(mockDeviceDetails.ModelId))
.ReturnsAsync(mockDeviceModel);

_ = this.mockDeviceTagSettingsClientService.Setup(service => service.GetDeviceTags())
.ReturnsAsync(new List<DeviceTag>
{
mockTag
});

_ = this.mockDeviceClientService
.Setup(service => service.GetDeviceProperties(mockDeviceDetails.DeviceID))
.ReturnsAsync(new List<DevicePropertyValue>());

var popoverProvider = RenderComponent<MudPopoverProvider>();
var cut = RenderComponent<DeviceDetailPage>(ComponentParameter.CreateParameter("DeviceID", mockDeviceDetails.DeviceID));
cut.WaitForAssertion(() => cut.Find($"#{nameof(DeviceModel.Name)}").InnerHtml.Should().NotBeEmpty());

var saveButton = cut.WaitForElement("#saveButton");

var mudButtonGroup = cut.FindComponent<MudButtonGroup>();

mudButtonGroup.Find(".mud-menu button").Click();
popoverProvider.WaitForAssertion(() => popoverProvider.FindAll("div.mud-list-item").Count.Should().Be(2));

var items = popoverProvider.FindAll("div.mud-list-item");

// Click on Duplicate
items[1].Click();

// Act
saveButton.Click();

// Assert
cut.WaitForAssertion(() => MockRepository.VerifyAll());
cut.WaitForAssertion(() => this.mockNavigationManager.Uri.Should().EndWith("/devices/new"));
}
}
}
Loading

0 comments on commit 620f339

Please sign in to comment.