Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joao4all committed Jul 29, 2024
1 parent e71a641 commit 4f94992
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace COMETwebapp.Tests.Components.EngineeringModel.FileStore

using DevExpress.Blazor;

using FluentResults;

using Microsoft.AspNetCore.Components.Forms;

using Moq;
Expand All @@ -55,10 +57,14 @@ public class FileFormTestFixture
public void SetUp()
{
this.context = new TestContext();

var domainSelectorViewModel = new Mock<IDomainOfExpertiseSelectorViewModel>();

this.viewModel = new Mock<IFileHandlerViewModel>();
this.viewModel.Setup(x => x.CurrentThing).Returns(new File());
var domainSelectorViewModel = new Mock<IDomainOfExpertiseSelectorViewModel>();
this.viewModel.Setup(x => x.DomainOfExpertiseSelectorViewModel).Returns(domainSelectorViewModel.Object);
this.viewModel.Setup(x => x.CreateOrEditFile(It.IsAny<bool>())).ReturnsAsync(new Result());

this.context.ConfigureDevExpressBlazor();

this.renderer = this.context.RenderComponent<FileForm>(parameters => { parameters.Add(p => p.ViewModel, this.viewModel.Object); });
Expand All @@ -72,14 +78,8 @@ public void Teardown()
}

[Test]
public async Task VerifyOnValidSubmitAndDelete()
public async Task VerifyFileFormDelete()
{
Assert.That(this.renderer.Instance.IsDeletePopupVisible, Is.EqualTo(false));

var form = this.renderer.FindComponent<EditForm>();
await this.renderer.InvokeAsync(form.Instance.OnValidSubmit.InvokeAsync);
this.viewModel.Verify(x => x.CreateOrEditFile(It.IsAny<bool>()), Times.Once);

var deleteFileButton = this.renderer.FindComponents<DxButton>().First(x => x.Instance.Id == "deleteFileButton");
await this.renderer.InvokeAsync(deleteFileButton.Instance.Click.InvokeAsync);
Assert.That(this.renderer.Instance.IsDeletePopupVisible, Is.EqualTo(true));
Expand All @@ -93,5 +93,31 @@ public async Task VerifyOnValidSubmitAndDelete()
this.viewModel.Verify(x => x.DeleteFile(), Times.Once);
});
}

[Test]
public async Task VerifyOnValidSubmitAndDelete()
{
Assert.That(this.renderer.Instance.IsDeletePopupVisible, Is.EqualTo(false));

var form = this.renderer.FindComponent<EditForm>();
await this.renderer.InvokeAsync(form.Instance.OnValidSubmit.InvokeAsync);

Assert.Multiple(() =>
{
this.viewModel.Verify(x => x.CreateOrEditFile(It.IsAny<bool>()), Times.Once);
Assert.That(this.renderer.Instance.ErrorMessage, Is.Null.Or.Empty);
});

var failingResult = new Result();
failingResult.Reasons.Add(new ExceptionalError(new Exception("Invalid")));
this.viewModel.Setup(x => x.CreateOrEditFile(It.IsAny<bool>())).ReturnsAsync(failingResult);
await this.renderer.InvokeAsync(form.Instance.OnValidSubmit.InvokeAsync);

Assert.Multiple(() =>
{
this.viewModel.Verify(x => x.CreateOrEditFile(It.IsAny<bool>()), Times.Exactly(2));
Assert.That(this.renderer.Instance.ErrorMessage, Is.Not.Null.Or.Empty);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileHandlerViewModelTestFixture.cs" company="Starion Group S.A.">
// Copyright (c) 2023-2024 Starion Group S.A.
// Copyright (c) 2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Antoine Théate, João Rua
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, João Rua
//
// This file is part of CDP4-COMET WEB Community Edition
// The CDP4-COMET WEB Community Edition is the Starion Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C.
// 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 CDP4-COMET WEB Community Edition is free software; you can redistribute it and/or
// 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 CDP4-COMET WEB Community Edition is distributed in the hope that it will be useful,
// 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.
Expand All @@ -35,6 +35,8 @@ namespace COMETwebapp.Tests.ViewModels.Components.EngineeringModel.FileStore
using COMETwebapp.ViewModels.Components.EngineeringModel.FileStore.FileHandler;
using COMETwebapp.ViewModels.Components.EngineeringModel.FileStore.FileRevisionHandler;

using FluentResults;

using Microsoft.Extensions.Logging;

using Moq;
Expand Down Expand Up @@ -63,44 +65,44 @@ public void Setup()
this.iteration = new Iteration();
var person = new Person();

var siteDirectory = new SiteDirectory()
var siteDirectory = new SiteDirectory
{
Domain =
{
new DomainOfExpertise()
new DomainOfExpertise
{
ShortName = "doe",
Name = "Domain Of Expertise"
}
}
};

var engineeringModelSetup = new EngineeringModelSetup()
var engineeringModelSetup = new EngineeringModelSetup
{
Participant = { new Participant { Person = person } }
};

var folder1 = new Folder()
var folder1 = new Folder
{
Iid = Guid.NewGuid(),
Name = "folder 1"
};

var file1 = new File()
var file1 = new File
{
Iid = Guid.NewGuid(),
Iid = Guid.NewGuid(),
CurrentContainingFolder = folder1
};

var file2 = new File()
var file2 = new File
{
Iid = Guid.NewGuid(),
CurrentContainingFolder = folder1
};

file1.FileRevision.Add(new FileRevision());

this.commonFileStore = new CommonFileStore()
this.commonFileStore = new CommonFileStore
{
Name = "CFS",
Folder = { folder1 },
Expand All @@ -111,6 +113,8 @@ public void Setup()

this.sessionService.Setup(x => x.GetSiteDirectory()).Returns(siteDirectory);
this.sessionService.Setup(x => x.Session.ActivePerson).Returns(person);
this.sessionService.Setup(x => x.CreateOrUpdateThings(It.IsAny<Thing>(), It.IsAny<List<Thing>>(), It.IsAny<List<string>>())).ReturnsAsync(new Result());

this.viewModel = new FileHandlerViewModel(this.sessionService.Object, this.messageBus, this.logger.Object, this.fileRevisionHandlerViewModel.Object);
}

Expand All @@ -121,30 +125,11 @@ public void TearDown()
this.messageBus.Dispose();
}

[Test]
public void VerifyInitializeViewModel()
{
this.viewModel.InitializeViewModel(this.commonFileStore, this.iteration);
Assert.That(this.viewModel.DomainOfExpertiseSelectorViewModel.AvailableDomainsOfExpertise.Count(), Is.EqualTo(1));
}

[Test]
public async Task VerifyMoveAndDeleteFile()
{
this.viewModel.InitializeViewModel(this.commonFileStore, this.iteration);
this.viewModel.CurrentThing = this.commonFileStore.File[0];

await this.viewModel.MoveFile(this.commonFileStore.File[0], this.commonFileStore.Folder[0]);
this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny<FileStore>(), It.IsAny<IReadOnlyCollection<Thing>>()), Times.Once);

await this.viewModel.DeleteFile();
this.sessionService.Verify(x => x.DeleteThings(It.IsAny<FileStore>(), It.IsAny<IReadOnlyCollection<Thing>>()), Times.Once);
}

[Test]
public async Task VerifyCreateOrEditFolder()
{
this.viewModel.InitializeViewModel(this.commonFileStore, this.iteration);
this.viewModel.CurrentThing = this.commonFileStore.File.First();

var domain = new DomainOfExpertise();
this.viewModel.DomainOfExpertiseSelectorViewModel.SelectedDomainOfExpertise = domain;
Expand All @@ -153,20 +138,45 @@ public async Task VerifyCreateOrEditFolder()
await this.viewModel.CreateOrEditFile(false);
this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny<FileStore>(), It.Is<IReadOnlyCollection<Thing>>(c => !c.OfType<FileStore>().Any()), It.IsAny<IReadOnlyCollection<string>>()), Times.Once);

this.viewModel.SelectedFileRevisions = [new FileRevision(){ LocalPath = "/localpath" }];
this.viewModel.SelectedFileRevisions = [new FileRevision { LocalPath = "/localpath" }];
this.viewModel.IsLocked = true;

var failingResult = new Result();
failingResult.Reasons.Add(new ExceptionalError(new Exception("Invalid")));
this.sessionService.Setup(x => x.CreateOrUpdateThings(It.IsAny<Thing>(), It.IsAny<List<Thing>>(), It.IsAny<List<string>>())).ReturnsAsync(failingResult);

await this.viewModel.CreateOrEditFile(true);

Assert.Multiple(() =>
{
this.sessionService.Verify(x => x.CreateOrUpdateThings(
It.IsAny<FileStore>(),
It.Is<IReadOnlyCollection<Thing>>(c => c.OfType<FileStore>().Any()),
It.Is<IReadOnlyCollection<string>>(c => c.Contains("/localpath")))
, Times.Once);
It.IsAny<FileStore>(),
It.Is<IReadOnlyCollection<Thing>>(c => c.OfType<FileStore>().Any()),
It.Is<IReadOnlyCollection<string>>(c => c.Contains("/localpath")))
, Times.Once);

Assert.That(this.viewModel.CurrentThing.LockedBy, Is.Not.Null);
});
}

[Test]
public void VerifyInitializeViewModel()
{
this.viewModel.InitializeViewModel(this.commonFileStore, this.iteration);
Assert.That(this.viewModel.DomainOfExpertiseSelectorViewModel.AvailableDomainsOfExpertise.Count(), Is.EqualTo(1));
}

[Test]
public async Task VerifyMoveAndDeleteFile()
{
this.viewModel.InitializeViewModel(this.commonFileStore, this.iteration);
this.viewModel.CurrentThing = this.commonFileStore.File[0];

await this.viewModel.MoveFile(this.commonFileStore.File[0], this.commonFileStore.Folder[0]);
this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny<FileStore>(), It.IsAny<IReadOnlyCollection<Thing>>()), Times.Once);

await this.viewModel.DeleteFile();
this.sessionService.Verify(x => x.DeleteThings(It.IsAny<FileStore>(), It.IsAny<IReadOnlyCollection<Thing>>()), Times.Once);
}
}
}

0 comments on commit 4f94992

Please sign in to comment.