From 785977a2e6fa52605bfeb3818aadf41da96a5b08 Mon Sep 17 00:00:00 2001 From: jaimeatrhea Date: Thu, 21 Sep 2023 14:50:24 +0200 Subject: [PATCH] Add some tests --- .../BookEditor/EditotPopupTestFixture.cs | 143 ++++++++++++++++++ .../Components/BookEditor/EditorPopup.razor | 4 +- .../BookEditor/BookEditorBodyTestFixture.cs | 42 ++++- .../Model/ApplicationsTestFixture.cs | 55 +++++++ .../DomDataServiceTestFixture.cs | 32 ++-- .../BookEditor/BookEditorColumn.razor | 4 +- 6 files changed, 256 insertions(+), 24 deletions(-) create mode 100644 COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs create mode 100644 COMETwebapp.Tests/Model/ApplicationsTestFixture.cs diff --git a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs new file mode 100644 index 00000000..81cb95cb --- /dev/null +++ b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs @@ -0,0 +1,143 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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 . +// +// -------------------------------------------------------------------------------------------------------------------- + +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 viewmodel; + private IRenderedComponent component; + private Book book; + private List activeDomains; + private List 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 + { + new() + { + Name = "Sys" + } + }; + + this.availableCategories = new List + { + 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(); + 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()); + + this.component = this.context.RenderComponent(parameters => + { + parameters.Add(p => p.ViewModel, this.viewmodel.Object); + }); + } + + [Test] + public void VerifyComponent() + { + var popup = this.component.FindComponent(); + + 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(); + errors.Add("Error 1"); + errors.Add("Error 2"); + + this.viewmodel.Setup(x => x.ValidationErrors).Returns(errors); + + this.component.Render(); + + var errorMessages = this.component.FindComponents(); + + 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")); + }); + } + } +} diff --git a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor index 6603d0c1..4d3482fc 100644 --- a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor +++ b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor @@ -34,8 +34,8 @@ diff --git a/COMETwebapp.Tests/Components/BookEditor/BookEditorBodyTestFixture.cs b/COMETwebapp.Tests/Components/BookEditor/BookEditorBodyTestFixture.cs index 39e38c13..6b3e6d5f 100644 --- a/COMETwebapp.Tests/Components/BookEditor/BookEditorBodyTestFixture.cs +++ b/COMETwebapp.Tests/Components/BookEditor/BookEditorBodyTestFixture.cs @@ -26,6 +26,7 @@ namespace COMETwebapp.Tests.Components.BookEditor { using Bunit; + using CDP4Common.CommonData; using CDP4Common.EngineeringModelData; using CDP4Common.ReportingData; using CDP4Common.SiteDirectoryData; @@ -56,6 +57,10 @@ public class BookEditorBodyTestFixture private IRenderedComponent component; private Mock viewModel; private Mock sessionService; + private Book selectedBook; + private Section selectedSection; + private Page selectedPage; + private Note selectedNote; [SetUp] public void Setup() @@ -64,10 +69,26 @@ public void Setup() this.context.ConfigureDevExpressBlazor(); this.sessionService = new Mock(); + 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(); + availableBooks.Add(this.selectedBook); + this.viewModel = new Mock(); 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()); + 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(); editorPopupViewModel.Setup(x => x.ValidationErrors).Returns(new SourceList()); @@ -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")); @@ -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); @@ -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); @@ -141,7 +162,7 @@ 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); @@ -149,6 +170,17 @@ public void VerifyComponent() 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()), Times.Exactly(4)); + } } } diff --git a/COMETwebapp.Tests/Model/ApplicationsTestFixture.cs b/COMETwebapp.Tests/Model/ApplicationsTestFixture.cs new file mode 100644 index 00000000..85eb15d0 --- /dev/null +++ b/COMETwebapp.Tests/Model/ApplicationsTestFixture.cs @@ -0,0 +1,55 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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 . +// +// -------------------------------------------------------------------------------------------------------------------- + +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); + }); + } + } + } +} + diff --git a/COMETwebapp.Tests/Services/Interoperability/DomDataServiceTestFixture.cs b/COMETwebapp.Tests/Services/Interoperability/DomDataServiceTestFixture.cs index 52447e05..e9043dd1 100644 --- a/COMETwebapp.Tests/Services/Interoperability/DomDataServiceTestFixture.cs +++ b/COMETwebapp.Tests/Services/Interoperability/DomDataServiceTestFixture.cs @@ -24,6 +24,8 @@ namespace COMETwebapp.Tests.Services.Interoperability { + using Bunit; + using CDP4Common.ReportingData; using COMETwebapp.Components.BookEditor; @@ -35,37 +37,37 @@ namespace COMETwebapp.Tests.Services.Interoperability using NUnit.Framework; + using TestContext = Bunit.TestContext; + [TestFixture] public class DomDataServiceTestFixture { - private Mock service; + private TestContext context; + private DomDataService service; [SetUp] public void Setup() { - this.service = new Mock(); + this.context = new TestContext(); + var jsRuntime = new Mock(); + + this.context.JSInterop.SetupVoid("setDotNetHelper"); + this.context.JSInterop.SetupVoid("SubscribeToResizeEvent"); + this.context.JSInterop.Setup("GetElementSizeAndPosition").SetResult(new float[] { 1, 2, 3, 4 }); - this.service.Setup(x => x.GetElementSizeAndPosition(It.IsAny(), It.IsAny(), It.IsAny())) - .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()); 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); }); } } diff --git a/COMETwebapp/Components/BookEditor/BookEditorColumn.razor b/COMETwebapp/Components/BookEditor/BookEditorColumn.razor index 8ea5d3a3..f9874e5d 100644 --- a/COMETwebapp/Components/BookEditor/BookEditorColumn.razor +++ b/COMETwebapp/Components/BookEditor/BookEditorColumn.razor @@ -70,8 +70,8 @@ @if (isSelected) {
- - + +
}