Skip to content

Commit

Permalink
unit tests to improve PR quality
Browse files Browse the repository at this point in the history
  • Loading branch information
joao4all committed Jun 21, 2024
1 parent 1834359 commit a5b595b
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 2 deletions.
117 changes: 117 additions & 0 deletions COMETwebapp.Tests/Pages/ReferenceData/ReferenceDataPageTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReferenceDataPageTestFixture.cs" company="Starion Group S.A.">
// 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 <http://www.gnu.org/licenses/>.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

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<IParameterTypeTableViewModel> parameterTypesTableViewModel;
private Mock<IMeasurementScalesTableViewModel> measurementScalesTableViewModel;
private Mock<ISessionService> sessionService;
private IRenderedComponent<ReferenceDataPage> renderer;

[SetUp]
public void Setup()
{
this.context = new TestContext();

this.parameterTypesTableViewModel = new Mock<IParameterTypeTableViewModel>();
this.parameterTypesTableViewModel.Setup(x => x.Rows).Returns(new SourceList<ParameterTypeRowViewModel>());

this.measurementScalesTableViewModel = new Mock<IMeasurementScalesTableViewModel>();
this.measurementScalesTableViewModel.Setup(x => x.Rows).Returns(new SourceList<MeasurementScaleRowViewModel>());

this.sessionService = new Mock<ISessionService>();
this.sessionService.Setup(x => x.Session).Returns(new Mock<ISession>().Object);

var configuration = new Mock<IConfigurationService>();
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<IApplicationTemplateViewModel, ApplicationTemplateViewModel>();

this.renderer = this.context.RenderComponent<ReferenceDataPage>();
}

[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);
});
}
}
}
117 changes: 117 additions & 0 deletions COMETwebapp.Tests/Pages/SiteDirectory/SiteDirectoryPageTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SiteDirectoryPageTestFixture.cs" company="Starion Group S.A.">
// 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 <http://www.gnu.org/licenses/>.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

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<IEngineeringModelsTableViewModel> engineeringModelsTableViewModel;
private Mock<IDomainsOfExpertiseTableViewModel> domainsOfExpertiseTableViewModel;
private Mock<ISessionService> sessionService;
private IRenderedComponent<DirectoryPage> renderer;

[SetUp]
public void Setup()
{
this.context = new TestContext();

this.engineeringModelsTableViewModel = new Mock<IEngineeringModelsTableViewModel>();
this.engineeringModelsTableViewModel.Setup(x => x.Rows).Returns(new SourceList<EngineeringModelRowViewModel>());

this.domainsOfExpertiseTableViewModel = new Mock<IDomainsOfExpertiseTableViewModel>();
this.domainsOfExpertiseTableViewModel.Setup(x => x.Rows).Returns(new SourceList<DomainOfExpertiseRowViewModel>());

this.sessionService = new Mock<ISessionService>();
this.sessionService.Setup(x => x.Session).Returns(new Mock<ISession>().Object);

var configuration = new Mock<IConfigurationService>();
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<IApplicationTemplateViewModel, ApplicationTemplateViewModel>();

this.renderer = this.context.RenderComponent<DirectoryPage>();
}

[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);
});
}
}
}
2 changes: 1 addition & 1 deletion COMETwebapp/Pages/ReferenceData/ReferenceDataPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public partial class ReferenceDataPage
/// <summary>
/// The selected component type
/// </summary>
private Type SelectedComponent { get; set; }
public Type SelectedComponent { get; private set; }

/// <summary>
/// Method invoked when the component is ready to start, having received its
Expand Down
2 changes: 1 addition & 1 deletion COMETwebapp/Pages/SiteDirectory/DirectoryPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public partial class DirectoryPage
/// <summary>
/// The selected component type
/// </summary>
private Type SelectedComponent { get; set; }
public Type SelectedComponent { get; private set; }

/// <summary>
/// Method invoked when the component is ready to start, having received its
Expand Down

0 comments on commit a5b595b

Please sign in to comment.