From a5b595b1755e27ccb3f27625c7eeda9308ad120c Mon Sep 17 00:00:00 2001 From: Joao Rua Date: Fri, 21 Jun 2024 09:50:04 +0100 Subject: [PATCH] unit tests to improve PR quality --- .../ReferenceDataPageTestFixture.cs | 117 ++++++++++++++++++ .../SiteDirectoryPageTestFixture.cs | 117 ++++++++++++++++++ .../ReferenceData/ReferenceDataPage.razor.cs | 2 +- .../SiteDirectory/DirectoryPage.razor.cs | 2 +- 4 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 COMETwebapp.Tests/Pages/ReferenceData/ReferenceDataPageTestFixture.cs create mode 100644 COMETwebapp.Tests/Pages/SiteDirectory/SiteDirectoryPageTestFixture.cs diff --git a/COMETwebapp.Tests/Pages/ReferenceData/ReferenceDataPageTestFixture.cs b/COMETwebapp.Tests/Pages/ReferenceData/ReferenceDataPageTestFixture.cs new file mode 100644 index 00000000..9e22ebc3 --- /dev/null +++ b/COMETwebapp.Tests/Pages/ReferenceData/ReferenceDataPageTestFixture.cs @@ -0,0 +1,117 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2024 Starion Group S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, João Rua +// +// This file is part of COMET WEB Community Edition +// The COMET WEB Community Edition is the Starion Group Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C. +// +// The COMET WEB Community Edition is free software; you can redistribute it and/or +// modify it under the terms of the GNU Affero General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// The COMET WEB Community Edition is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMETwebapp.Tests.Pages.ReferenceData +{ + using Bunit; + + using CDP4Dal; + + using COMET.Web.Common.Model.Configuration; + using COMET.Web.Common.Services.ConfigurationService; + using COMET.Web.Common.Services.SessionManagement; + using COMET.Web.Common.Test.Helpers; + using COMET.Web.Common.ViewModels.Components.Applications; + + using COMETwebapp.Components.ReferenceData.MeasurementScales; + using COMETwebapp.Components.ReferenceData.ParameterTypes; + using COMETwebapp.Pages.ReferenceData; + using COMETwebapp.ViewModels.Components.ReferenceData.MeasurementScales; + using COMETwebapp.ViewModels.Components.ReferenceData.ParameterTypes; + using COMETwebapp.ViewModels.Components.ReferenceData.Rows; + + using DynamicData; + + using Microsoft.AspNetCore.Components.Web; + using Microsoft.Extensions.DependencyInjection; + + using Moq; + + using NUnit.Framework; + + using TestContext = Bunit.TestContext; + + [TestFixture] + public class ReferenceDataPageTestFixture + { + private TestContext context; + private Mock parameterTypesTableViewModel; + private Mock measurementScalesTableViewModel; + private Mock sessionService; + private IRenderedComponent renderer; + + [SetUp] + public void Setup() + { + this.context = new TestContext(); + + this.parameterTypesTableViewModel = new Mock(); + this.parameterTypesTableViewModel.Setup(x => x.Rows).Returns(new SourceList()); + + this.measurementScalesTableViewModel = new Mock(); + this.measurementScalesTableViewModel.Setup(x => x.Rows).Returns(new SourceList()); + + this.sessionService = new Mock(); + this.sessionService.Setup(x => x.Session).Returns(new Mock().Object); + + var configuration = new Mock(); + configuration.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration()); + + this.context.ConfigureDevExpressBlazor(); + this.context.Services.AddSingleton(this.sessionService.Object); + this.context.Services.AddSingleton(this.parameterTypesTableViewModel.Object); + this.context.Services.AddSingleton(this.measurementScalesTableViewModel.Object); + this.context.Services.AddSingleton(configuration.Object); + this.context.Services.AddSingleton(); + + this.renderer = this.context.RenderComponent(); + } + + [TearDown] + public void Teardown() + { + this.context.CleanContext(); + } + + [Test] + public async Task VerifyReferenceDataPage() + { + Assert.Multiple(() => + { + Assert.That(this.renderer.Instance, Is.Not.Null); + Assert.That(this.renderer.Instance.SelectedComponent, Is.EqualTo(typeof(ParameterTypeTable))); + this.parameterTypesTableViewModel.Verify(x => x.InitializeViewModel(), Times.Once); + }); + + var toolBarItem = this.renderer.FindAll("button").ElementAt(1); + await this.renderer.InvokeAsync(() => toolBarItem.ClickAsync(new MouseEventArgs())); + + Assert.Multiple(() => + { + Assert.That(this.renderer.Instance.SelectedComponent, Is.EqualTo(typeof(MeasurementScalesTable))); + this.measurementScalesTableViewModel.Verify(x => x.InitializeViewModel(), Times.Once); + }); + } + } +} diff --git a/COMETwebapp.Tests/Pages/SiteDirectory/SiteDirectoryPageTestFixture.cs b/COMETwebapp.Tests/Pages/SiteDirectory/SiteDirectoryPageTestFixture.cs new file mode 100644 index 00000000..0214b115 --- /dev/null +++ b/COMETwebapp.Tests/Pages/SiteDirectory/SiteDirectoryPageTestFixture.cs @@ -0,0 +1,117 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2024 Starion Group S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, João Rua +// +// This file is part of COMET WEB Community Edition +// The COMET WEB Community Edition is the Starion Group Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C. +// +// The COMET WEB Community Edition is free software; you can redistribute it and/or +// modify it under the terms of the GNU Affero General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// The COMET WEB Community Edition is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMETwebapp.Tests.Pages.SiteDirectory +{ + using Bunit; + + using CDP4Dal; + + using COMET.Web.Common.Model.Configuration; + using COMET.Web.Common.Services.ConfigurationService; + using COMET.Web.Common.Services.SessionManagement; + using COMET.Web.Common.Test.Helpers; + using COMET.Web.Common.ViewModels.Components.Applications; + + using COMETwebapp.Components.SiteDirectory; + using COMETwebapp.Components.SiteDirectory.EngineeringModel; + using COMETwebapp.Pages.SiteDirectory; + using COMETwebapp.ViewModels.Components.SiteDirectory.DomainsOfExpertise; + using COMETwebapp.ViewModels.Components.SiteDirectory.EngineeringModels; + using COMETwebapp.ViewModels.Components.SiteDirectory.Rows; + + using DynamicData; + + using Microsoft.AspNetCore.Components.Web; + using Microsoft.Extensions.DependencyInjection; + + using Moq; + + using NUnit.Framework; + + using TestContext = Bunit.TestContext; + + [TestFixture] + public class SiteDirectoryPageTestFixture + { + private TestContext context; + private Mock engineeringModelsTableViewModel; + private Mock domainsOfExpertiseTableViewModel; + private Mock sessionService; + private IRenderedComponent renderer; + + [SetUp] + public void Setup() + { + this.context = new TestContext(); + + this.engineeringModelsTableViewModel = new Mock(); + this.engineeringModelsTableViewModel.Setup(x => x.Rows).Returns(new SourceList()); + + this.domainsOfExpertiseTableViewModel = new Mock(); + this.domainsOfExpertiseTableViewModel.Setup(x => x.Rows).Returns(new SourceList()); + + this.sessionService = new Mock(); + this.sessionService.Setup(x => x.Session).Returns(new Mock().Object); + + var configuration = new Mock(); + configuration.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration()); + + this.context.ConfigureDevExpressBlazor(); + this.context.Services.AddSingleton(this.sessionService.Object); + this.context.Services.AddSingleton(this.engineeringModelsTableViewModel.Object); + this.context.Services.AddSingleton(this.domainsOfExpertiseTableViewModel.Object); + this.context.Services.AddSingleton(configuration.Object); + this.context.Services.AddSingleton(); + + this.renderer = this.context.RenderComponent(); + } + + [TearDown] + public void Teardown() + { + this.context.CleanContext(); + } + + [Test] + public async Task VerifySiteDirectoryPage() + { + Assert.Multiple(() => + { + Assert.That(this.renderer.Instance, Is.Not.Null); + Assert.That(this.renderer.Instance.SelectedComponent, Is.EqualTo(typeof(EngineeringModelsTable))); + this.engineeringModelsTableViewModel.Verify(x => x.InitializeViewModel(), Times.Once); + }); + + var toolBarItem = this.renderer.FindAll("button").ElementAt(1); + await this.renderer.InvokeAsync(() => toolBarItem.ClickAsync(new MouseEventArgs())); + + Assert.Multiple(() => + { + Assert.That(this.renderer.Instance.SelectedComponent, Is.EqualTo(typeof(DomainsOfExpertiseTable))); + this.domainsOfExpertiseTableViewModel.Verify(x => x.InitializeViewModel(), Times.Once); + }); + } + } +} diff --git a/COMETwebapp/Pages/ReferenceData/ReferenceDataPage.razor.cs b/COMETwebapp/Pages/ReferenceData/ReferenceDataPage.razor.cs index effd2e43..7baf380e 100644 --- a/COMETwebapp/Pages/ReferenceData/ReferenceDataPage.razor.cs +++ b/COMETwebapp/Pages/ReferenceData/ReferenceDataPage.razor.cs @@ -50,7 +50,7 @@ public partial class ReferenceDataPage /// /// The selected component type /// - private Type SelectedComponent { get; set; } + public Type SelectedComponent { get; private set; } /// /// Method invoked when the component is ready to start, having received its diff --git a/COMETwebapp/Pages/SiteDirectory/DirectoryPage.razor.cs b/COMETwebapp/Pages/SiteDirectory/DirectoryPage.razor.cs index 27b455d9..f309a938 100644 --- a/COMETwebapp/Pages/SiteDirectory/DirectoryPage.razor.cs +++ b/COMETwebapp/Pages/SiteDirectory/DirectoryPage.razor.cs @@ -54,7 +54,7 @@ public partial class DirectoryPage /// /// The selected component type /// - private Type SelectedComponent { get; set; } + public Type SelectedComponent { get; private set; } /// /// Method invoked when the component is ready to start, having received its