Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimeatstariongroup committed Sep 21, 2023
1 parent 4c21840 commit 785977a
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 24 deletions.
143 changes: 143 additions & 0 deletions COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EditotPopupTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2023 RHEA System S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, Nabil Abbar
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the RHEA 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 COMET.Web.Common.Tests.Components.BookEditor
{
using Bunit;

using CDP4Common.ReportingData;
using CDP4Common.SiteDirectoryData;

using COMET.Web.Common.Components;
using COMET.Web.Common.Components.BookEditor;
using COMET.Web.Common.Test.Helpers;
using COMET.Web.Common.ViewModels.Components.BookEditor;

using DevExpress.Blazor;
using DevExpress.Blazor.Popup.Internal;

using DynamicData;

using Microsoft.AspNetCore.Components;

using Moq;

using NUnit.Framework;

using TestContext = Bunit.TestContext;

[TestFixture]
public class EditotPopupTestFixture
{
private TestContext context;
private Mock<IEditorPopupViewModel> viewmodel;
private IRenderedComponent<EditorPopup> component;
private Book book;
private List<DomainOfExpertise> activeDomains;
private List<Category> availableCategories;
private bool onCancelCalled;
private bool onAcceptCalled;

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

this.book = new Book();

this.activeDomains = new List<DomainOfExpertise>
{
new()
{
Name = "Sys"
}
};

this.availableCategories = new List<Category>
{
new()
{
Name = "Category"
}
};

var onCancelClicked = new EventCallbackFactory().Create(this, () => this.onCancelCalled = true);
var onConfirmClicked = new EventCallbackFactory().Create(this, () => this.onAcceptCalled = true);

this.viewmodel = new Mock<IEditorPopupViewModel>();
this.viewmodel.Setup(x => x.IsVisible).Returns(true);
this.viewmodel.Setup(x => x.ActiveDomains).Returns(this.activeDomains);
this.viewmodel.Setup(x => x.AvailableCategories).Returns(this.availableCategories);
this.viewmodel.Setup(x => x.HeaderText).Returns("Header");
this.viewmodel.Setup(x => x.OnCancelClick).Returns(onCancelClicked);
this.viewmodel.Setup(x => x.OnConfirmClick).Returns(onConfirmClicked);
this.viewmodel.Setup(x => x.ValidationErrors).Returns(new SourceList<string>());

this.component = this.context.RenderComponent<EditorPopup>(parameters =>
{
parameters.Add(p => p.ViewModel, this.viewmodel.Object);
});
}

[Test]
public void VerifyComponent()
{
var popup = this.component.FindComponent<DxPopup>();

Assert.Multiple(() =>
{
Assert.That(popup.Instance.Visible, Is.True);
Assert.That(popup.Instance.HeaderText, Is.EqualTo("Header"));
});

var okButton = this.component.Find(".ok-button");
okButton.Click();

Assert.That(this.onAcceptCalled, Is.True);

var cancelButton = this.component.Find(".cancel-button");
cancelButton.Click();

Assert.That(this.onCancelCalled, Is.True);

var errors = new SourceList<string>();
errors.Add("Error 1");
errors.Add("Error 2");

this.viewmodel.Setup(x => x.ValidationErrors).Returns(errors);

this.component.Render();

var errorMessages = this.component.FindComponents<ValidationMessageComponent>();

Assert.Multiple(() =>
{
Assert.That(errorMessages, Has.Count.EqualTo(2));
Assert.That(errorMessages[0].Instance.ValidationMessage, Is.EqualTo("Error 1"));
Assert.That(errorMessages[1].Instance.ValidationMessage, Is.EqualTo("Error 2"));
});
}
}
}
4 changes: 2 additions & 2 deletions COMET.Web.Common/Components/BookEditor/EditorPopup.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
</Content>
<FooterTemplate>
<div class="modal-footer">
<DxButton Text="OK" RenderStyle="ButtonRenderStyle.Primary" Click="@(async () => await this.OnConfirmClick())" />
<DxButton Text="Cancel" RenderStyle="ButtonRenderStyle.Danger" Click="@(async () => await this.ViewModel.OnCancelClick.InvokeAsync())" />
<DxButton CssClass="ok-button" Text="OK" RenderStyle="ButtonRenderStyle.Primary" Click="@(async () => await this.OnConfirmClick())" />
<DxButton CssClass="cancel-button" Text="Cancel" RenderStyle="ButtonRenderStyle.Danger" Click="@(async () => await this.ViewModel.OnCancelClick.InvokeAsync())" />
</div>
</FooterTemplate>
</DxPopup>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace COMETwebapp.Tests.Components.BookEditor
{
using Bunit;

using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Common.ReportingData;
using CDP4Common.SiteDirectoryData;
Expand Down Expand Up @@ -56,6 +57,10 @@ public class BookEditorBodyTestFixture
private IRenderedComponent<BookEditorBody> component;
private Mock<IBookEditorBodyViewModel> viewModel;
private Mock<ISessionService> sessionService;
private Book selectedBook;
private Section selectedSection;
private Page selectedPage;
private Note selectedNote;

[SetUp]
public void Setup()
Expand All @@ -64,10 +69,26 @@ public void Setup()
this.context.ConfigureDevExpressBlazor();
this.sessionService = new Mock<ISessionService>();

this.selectedBook = new Book();
this.selectedSection = new Section();
this.selectedPage = new Page();
this.selectedNote = new TextualNote();

this.selectedBook.Section.Add(this.selectedSection);
this.selectedSection.Page.Add(this.selectedPage);
this.selectedPage.Note.Add(this.selectedNote);

var availableBooks = new SourceList<Book>();
availableBooks.Add(this.selectedBook);

this.viewModel = new Mock<IBookEditorBodyViewModel>();
this.viewModel.Setup(x => x.CurrentIteration).Returns(new Iteration());
this.viewModel.Setup(x => x.CurrentDomain).Returns(new DomainOfExpertise());
this.viewModel.Setup(x => x.AvailableBooks).Returns(new SourceList<Book>());
this.viewModel.Setup(x => x.AvailableBooks).Returns(availableBooks);
this.viewModel.Setup(x => x.SelectedBook).Returns(this.selectedBook);
this.viewModel.Setup(x => x.SelectedSection).Returns(this.selectedSection);
this.viewModel.Setup(x => x.SelectedPage).Returns(this.selectedPage);
this.viewModel.Setup(x => x.SelectedNote).Returns(this.selectedNote);

var editorPopupViewModel = new Mock<IEditorPopupViewModel>();
editorPopupViewModel.Setup(x => x.ValidationErrors).Returns(new SourceList<string>());
Expand Down Expand Up @@ -109,7 +130,7 @@ public void VerifyComponent()
Assert.That(bookEditorColumn.Instance, Is.Not.Null);
Assert.That(bookEditorColumn.Instance.HeaderHexColor, Is.EqualTo("#eba434"));
Assert.That(bookEditorColumn.Instance.HeaderTitle, Is.EqualTo("Books"));
Assert.That(bookEditorColumn.Instance.Items, Is.Null.Or.Empty);
Assert.That(bookEditorColumn.Instance.Items, Is.Not.Null.Or.Empty);
Assert.That(bookEditorColumn.Instance.DrawLeftLines, Is.False);
Assert.That(bookEditorColumn.Instance.CollapseButtonIconClass, Is.EqualTo("no-display"));
Assert.That(bookEditorColumn.Instance.CssClass, Is.EqualTo("book-node"));
Expand All @@ -120,7 +141,7 @@ public void VerifyComponent()
Assert.That(sectionEditorColumn.Instance, Is.Not.Null);
Assert.That(sectionEditorColumn.Instance.HeaderHexColor, Is.EqualTo("#56bd08"));
Assert.That(sectionEditorColumn.Instance.HeaderTitle, Is.EqualTo("Sections"));
Assert.That(sectionEditorColumn.Instance.Items, Is.Null.Or.Empty);
Assert.That(sectionEditorColumn.Instance.Items, Is.Not.Null.Or.Empty);
Assert.That(sectionEditorColumn.Instance.DrawLeftLines, Is.True);
Assert.That(sectionEditorColumn.Instance.CssClass, Is.EqualTo("section-node"));
Assert.That(sectionEditorColumn.Instance.OnCreateNewItemClick.HasDelegate, Is.True);
Expand All @@ -130,7 +151,7 @@ public void VerifyComponent()
Assert.That(pageEditorColumn.Instance, Is.Not.Null);
Assert.That(pageEditorColumn.Instance.HeaderHexColor, Is.EqualTo("#51dded"));
Assert.That(pageEditorColumn.Instance.HeaderTitle, Is.EqualTo("Pages"));
Assert.That(pageEditorColumn.Instance.Items, Is.Null.Or.Empty);
Assert.That(pageEditorColumn.Instance.Items, Is.Not.Null.Or.Empty);
Assert.That(pageEditorColumn.Instance.DrawLeftLines, Is.True);
Assert.That(pageEditorColumn.Instance.CssClass, Is.EqualTo("page-node"));
Assert.That(pageEditorColumn.Instance.OnCreateNewItemClick.HasDelegate, Is.True);
Expand All @@ -141,14 +162,25 @@ public void VerifyComponent()
Assert.That(noteEditorColumn.Instance, Is.Not.Null);
Assert.That(noteEditorColumn.Instance.HeaderHexColor, Is.EqualTo("#eb6075"));
Assert.That(noteEditorColumn.Instance.HeaderTitle, Is.EqualTo("Notes"));
Assert.That(noteEditorColumn.Instance.Items, Is.Null.Or.Empty);
Assert.That(noteEditorColumn.Instance.Items, Is.Not.Null.Or.Empty);
Assert.That(noteEditorColumn.Instance.DrawLeftLines, Is.True);
Assert.That(noteEditorColumn.Instance.CssClass, Is.EqualTo("note-node"));
Assert.That(noteEditorColumn.Instance.OnCreateNewItemClick.HasDelegate, Is.True);
Assert.That(noteEditorColumn.Instance.OnEditClicked.HasDelegate, Is.True);
Assert.That(noteEditorColumn.Instance.OnDeleteClicked.HasDelegate, Is.True);
});
}

[Test]
public async Task VerifyCallbackAreCalled()
{
await this.component.InvokeAsync(() => this.component.FindAll(".add-item-button")[0].Click());
await this.component.InvokeAsync(() => this.component.FindAll(".add-item-button")[1].Click());
await this.component.InvokeAsync(() => this.component.FindAll(".add-item-button")[2].Click());
await this.component.InvokeAsync(() => this.component.FindAll(".add-item-button")[3].Click());

this.viewModel.Verify(x => x.SetThingToCreate(It.IsAny<Thing>()), Times.Exactly(4));
}
}
}

55 changes: 55 additions & 0 deletions COMETwebapp.Tests/Model/ApplicationsTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApplicationsTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2023 RHEA System S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, Nabil Abbar
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the RHEA 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.Model
{
using COMETwebapp.Model;

using NUnit.Framework;

[TestFixture]
public class ApplicationsTestFixture
{
[Test]
public void VerifyApplications()
{
var applications = Applications.ExistingApplications;

Assert.That(applications, Has.Count.EqualTo(11));

foreach (var application in applications)
{
Assert.Multiple(() =>
{
Assert.That(application.Name, Is.Not.Null.Or.Empty);
Assert.That(application.Color, Is.Not.Null.Or.Empty);
Assert.That(application.Icon, Is.Not.Null.Or.Empty);
Assert.That(application.Description, Is.Not.Null.Or.Empty);
Assert.That(application.Url, Is.Not.Null.Or.Empty);
});
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace COMETwebapp.Tests.Services.Interoperability
{
using Bunit;

using CDP4Common.ReportingData;

using COMETwebapp.Components.BookEditor;
Expand All @@ -35,37 +37,37 @@ namespace COMETwebapp.Tests.Services.Interoperability

using NUnit.Framework;

using TestContext = Bunit.TestContext;

[TestFixture]
public class DomDataServiceTestFixture
{
private Mock<IDomDataService> service;
private TestContext context;
private DomDataService service;

[SetUp]
public void Setup()
{
this.service = new Mock<IDomDataService>();
this.context = new TestContext();
var jsRuntime = new Mock<IJSRuntime>();

this.context.JSInterop.SetupVoid("setDotNetHelper");
this.context.JSInterop.SetupVoid("SubscribeToResizeEvent");
this.context.JSInterop.Setup<float[]>("GetElementSizeAndPosition").SetResult(new float[] { 1, 2, 3, 4 });

this.service.Setup(x => x.GetElementSizeAndPosition(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<bool>()))
.ReturnsAsync(new float[] { 1, 2, 3, 4 });
this.service = new DomDataService(jsRuntime.Object);
}

[Test]
public async Task VerifyMethods()
public void VerifyMethods()
{
var dotnet = DotNetObjectReference.Create(new BookEditorColumn<Book>());

Assert.Multiple(() =>
{
Assert.That(() => this.service.Object.LoadDotNetHelper(dotnet), Throws.Nothing);
Assert.That(async () => await this.service.Object.GetElementSizeAndPosition(0, "node", true), Throws.Nothing);
});

var sizeAndPosition = await this.service.Object.GetElementSizeAndPosition(0, "node", true);

Assert.Multiple(() =>
{
Assert.That(sizeAndPosition, Is.EquivalentTo(new float[] { 1, 2, 3, 4 }));
Assert.That(() => this.service.Object.SubscribeToResizeEvent("resize"), Throws.Nothing);
Assert.That(() => this.service.LoadDotNetHelper(dotnet), Throws.Nothing);
Assert.That(async () => await this.service.GetElementSizeAndPosition(0, "node", true), Throws.Nothing);
Assert.That(() => this.service.SubscribeToResizeEvent("resize"), Throws.Nothing);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions COMETwebapp/Components/BookEditor/BookEditorColumn.razor
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
@if (isSelected)
{
<div class="edit-node-container">
<button class="icon icon-edit" @onclick="@(async () => await this.OnEditClicked.InvokeAsync(item))"></button>
<button class="icon icon-trash" @onclick="@(async () => await this.OnDeleteClicked.InvokeAsync(item))"></button>
<button class="icon icon-edit edit-button" @onclick="@(async () => await this.OnEditClicked.InvokeAsync(item))"></button>
<button class="icon icon-trash delete-button" @onclick="@(async () => await this.OnDeleteClicked.InvokeAsync(item))"></button>
</div>
}
</div>
Expand Down

0 comments on commit 785977a

Please sign in to comment.