-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed dead code, added dispose event handler cleanup and wrote test…
…s for World- and SpaceFormContainer
- Loading branch information
1 parent
7384e4a
commit c4c068f
Showing
4 changed files
with
236 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
PresentationTest/Components/Forms/Space/SpaceFormContainerUt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Bunit; | ||
using Bunit.TestDoubles; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using Presentation.Components.Forms.Space; | ||
using Presentation.PresentationLogic.LearningSpace; | ||
using Presentation.PresentationLogic.SelectedViewModels; | ||
using TestContext = Bunit.TestContext; | ||
|
||
namespace PresentationTest.Components.Forms.Space; | ||
|
||
[TestFixture] | ||
public class SpaceFormContainerUt | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
_testContext = new TestContext(); | ||
_testContext.ComponentFactories.AddStub<CreateSpaceForm>(); | ||
_testContext.ComponentFactories.AddStub<EditSpaceForm>(); | ||
Presenter = Substitute.For<ILearningSpacePresenter>(); | ||
SelectedVmProvider = Substitute.For<ISelectedViewModelsProvider>(); | ||
_testContext.Services.AddSingleton(Presenter); | ||
_testContext.Services.AddSingleton(SelectedVmProvider); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
_testContext.Dispose(); | ||
} | ||
|
||
private TestContext _testContext = null!; | ||
|
||
private ILearningSpacePresenter Presenter { get; set; } | ||
|
||
private ISelectedViewModelsProvider SelectedVmProvider { get; set; } | ||
|
||
[Test] | ||
public void OnParametersSet_RegistersToPresenterEvent() | ||
{ | ||
var sut = GetRenderedComponent(); | ||
|
||
Presenter.Received().PropertyChanged += Arg.Any<PropertyChangedEventHandler>(); | ||
} | ||
|
||
[Test] | ||
public void Dispose_UnregistersFromPresenterEvent() | ||
{ | ||
var sut = GetRenderedComponent(); | ||
sut.Instance.Dispose(); | ||
|
||
Presenter.Received().PropertyChanged -= Arg.Any<PropertyChangedEventHandler>(); | ||
} | ||
|
||
[Test] | ||
public void Render_SpaceNull_ShowsCreate() | ||
{ | ||
Presenter.LearningSpaceVm.Returns((ILearningSpaceViewModel?)null); | ||
|
||
var sut = GetRenderedComponent(); | ||
|
||
var createStubs = sut.FindComponents<Stub<CreateSpaceForm>>().ToList(); | ||
var editStubs = sut.FindComponents<Stub<EditSpaceForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(1)); | ||
Assert.That(editStubs, Has.Count.EqualTo(0)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Render_SpaceNotNull_ShowsEdit() | ||
{ | ||
Presenter.LearningSpaceVm.Returns(Substitute.For<ILearningSpaceViewModel>()); | ||
|
||
var sut = GetRenderedComponent(); | ||
|
||
var createStubs = sut.FindComponents<Stub<CreateSpaceForm>>().ToList(); | ||
var editStubs = sut.FindComponents<Stub<EditSpaceForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(0)); | ||
Assert.That(editStubs, Has.Count.EqualTo(1)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task OnForceNew_ForcesNew() | ||
{ | ||
Presenter.LearningSpaceVm.Returns(Substitute.For<ILearningSpaceViewModel>()); | ||
|
||
var sut = GetRenderedComponent(); | ||
|
||
var createStubs = sut.FindComponents<Stub<CreateSpaceForm>>().ToList(); | ||
var editStubs = sut.FindComponents<Stub<EditSpaceForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(0)); | ||
Assert.That(editStubs, Has.Count.EqualTo(1)); | ||
}); | ||
|
||
var editStub = editStubs[0]; | ||
|
||
var onNewCallback = (EventCallback)editStub.Instance.Parameters["OnNewButtonClicked"]; | ||
await sut.InvokeAsync(async () => await onNewCallback.InvokeAsync()); | ||
|
||
|
||
createStubs = sut.FindComponents<Stub<CreateSpaceForm>>().ToList(); | ||
editStubs = sut.FindComponents<Stub<EditSpaceForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(1)); | ||
Assert.That(editStubs, Has.Count.EqualTo(0)); | ||
}); | ||
|
||
SelectedVmProvider.Received().SetLearningObjectInPathWay(null, null); | ||
} | ||
|
||
private IRenderedComponent<SpaceFormContainer> GetRenderedComponent() | ||
{ | ||
return _testContext.RenderComponent<SpaceFormContainer>(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
PresentationTest/Components/Forms/World/WorldFormContainerUt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Bunit; | ||
using Bunit.TestDoubles; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using Presentation.Components.Forms.World; | ||
using Presentation.PresentationLogic.LearningWorld; | ||
using TestHelpers; | ||
using TestContext = Bunit.TestContext; | ||
|
||
namespace PresentationTest.Components.Forms.World; | ||
|
||
[TestFixture] | ||
public class WorldFormContainerUt | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
_testContext = new TestContext(); | ||
_testContext.ComponentFactories.AddStub<CreateWorldForm>(); | ||
_testContext.ComponentFactories.AddStub<EditWorldForm>(); | ||
Presenter = Substitute.For<ILearningWorldPresenter>(); | ||
_testContext.Services.AddSingleton(Presenter); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
_testContext.Dispose(); | ||
} | ||
|
||
private TestContext _testContext = null!; | ||
|
||
private ILearningWorldPresenter Presenter { get; set; } | ||
|
||
[Test] | ||
public void OnParametersSet_RegistersToPresenterEvent() | ||
{ | ||
var sut = GetRenderedComponent(); | ||
|
||
Presenter.Received().PropertyChanged += Arg.Any<PropertyChangedEventHandler>(); | ||
} | ||
|
||
[Test] | ||
public void Dispose_UnregistersFromPresenterEvent() | ||
{ | ||
var sut = GetRenderedComponent(); | ||
sut.Instance.Dispose(); | ||
|
||
Presenter.Received().PropertyChanged -= Arg.Any<PropertyChangedEventHandler>(); | ||
} | ||
|
||
[Test] | ||
public void Render_WorldNull_ShowsCreate() | ||
{ | ||
Presenter.LearningWorldVm.Returns((ILearningWorldViewModel?)null); | ||
|
||
var sut = GetRenderedComponent(); | ||
|
||
var createStubs = sut.FindComponents<Stub<CreateWorldForm>>().ToList(); | ||
var editStubs = sut.FindComponents<Stub<EditWorldForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(1)); | ||
Assert.That(editStubs, Has.Count.EqualTo(0)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Render_WorldNotNull_ShowsEdit() | ||
{ | ||
Presenter.LearningWorldVm.Returns(ViewModelProvider.GetLearningWorld()); | ||
|
||
var sut = GetRenderedComponent(); | ||
|
||
var createStubs = sut.FindComponents<Stub<CreateWorldForm>>().ToList(); | ||
var editStubs = sut.FindComponents<Stub<EditWorldForm>>().ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(createStubs, Has.Count.EqualTo(0)); | ||
Assert.That(editStubs, Has.Count.EqualTo(1)); | ||
}); | ||
} | ||
|
||
private IRenderedComponent<WorldFormContainer> GetRenderedComponent() | ||
{ | ||
return _testContext.RenderComponent<WorldFormContainer>(); | ||
} | ||
} |