diff --git a/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/FakeModal.cs b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/FakeModal.cs new file mode 100644 index 0000000..02f8851 --- /dev/null +++ b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/FakeModal.cs @@ -0,0 +1,22 @@ +namespace Rhythm.Drop.Web.Tests.TagHelperRenderers; + +using Rhythm.Drop.Models.Common.Attributes; +using Rhythm.Drop.Models.Components; +using Rhythm.Drop.Models.Modals; +using System.Collections.Generic; + +internal class FakeModal : IModal +{ + public string ViewName => "Fake"; + + public string UniqueKey => "fake"; + + public IReadOnlyCollection Content => [ new FakeComponent() ]; + + public IReadOnlyHtmlAttributeCollection Attributes => ReadOnlyHtmlAttributeCollection.Empty(); + + public IReadOnlyCollection GetComponents() + { + return Content; + } +} \ No newline at end of file diff --git a/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTests.cs b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTests.cs new file mode 100644 index 0000000..3a6a0d1 --- /dev/null +++ b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTests.cs @@ -0,0 +1,58 @@ +namespace Rhythm.Drop.Web.Tests.TagHelperRenderers.Modals; + +using Rhythm.Drop.Models.Common.Attributes; +using Rhythm.Drop.Models.Modals; +using Rhythm.Drop.Web.Infrastructure.TagHelperRenderers.Modals; + +[TestFixture] +public class DefaultDropModalTagHelperRendererTests : DefaultDropModalTagHelperRendererTestsBase +{ + [Test] + public async Task RenderAsync_With_No_TagName_Returns_No_Output_Tag() + { + // arrange + var tagHelperRenderer = CreateDefaultDropModalsTagHelperRenderer(); + var model = CreateRendererContext(new FakeModal()); + var context = CreateTagHelperContext(DefaultTagName); + var output = CreateTagHelperOutput(DefaultTagName); + + // act + await tagHelperRenderer.RenderAsync(model, context, output); + + // assert + Assert.Multiple(() => + { + Assert.That(output.TagName, Is.Not.EqualTo(DefaultTagName)); + Assert.That(output.Content.IsEmptyOrWhiteSpace, Is.False); + }); + + } + + [Test] + public async Task RenderAsync_With_No_Modal_Returns_No_Output() + { + // arrange + var tagHelperRenderer = CreateDefaultDropModalsTagHelperRenderer(); + var model = CreateRendererContext(default); + var context = CreateTagHelperContext(DefaultTagName); + var output = CreateTagHelperOutput(DefaultTagName); + + // act + await tagHelperRenderer.RenderAsync(model, context, output); + + // assert + Assert.Multiple(() => + { + Assert.That(output.TagName, Is.Not.EqualTo(DefaultTagName)); + Assert.That(output.Content.IsEmptyOrWhiteSpace, Is.True); + }); + } + + + private static DropModalTagHelperRendererContext CreateRendererContext(IModal? modal) + { + var viewContext = CreateViewContext(); + + return new DropModalTagHelperRendererContext(modal, DefaultTheme, ReadOnlyHtmlAttributeCollection.Empty(), viewContext, default); + } +} diff --git a/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTestsBase.cs b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTestsBase.cs new file mode 100644 index 0000000..b8d4d2f --- /dev/null +++ b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalTagHelperRendererTestsBase.cs @@ -0,0 +1,49 @@ +namespace Rhythm.Drop.Web.Tests.TagHelperRenderers.Modals; + +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc.Rendering; +using Moq; +using Rhythm.Drop.Web.Factories.MetaData; +using Rhythm.Drop.Web.Infrastructure.Factories.MetaData; +using Rhythm.Drop.Web.Infrastructure.Helpers.Rendering; +using Rhythm.Drop.Web.Infrastructure.MetaData; +using Rhythm.Drop.Web.Infrastructure.TagHelperRenderers.Modals; +using Rhythm.Drop.Web.TagHelperRenderers.Modals; +using System.Threading.Tasks; + +public abstract class DefaultDropModalTagHelperRendererTestsBase : TagHelperRendererTestsBase +{ + protected const string DefaultTheme = "Default"; + + + + protected static IRenderingHelper CreateRenderingHelper() + { + return CreateRenderingHelper(new HtmlString("
Test
")); + } + + protected static IRenderingHelper CreateRenderingHelper(IHtmlContent htmlContent) + { + var mock = new Mock(); + mock.Setup(x => x.RenderAsync(It.IsAny())).Returns(Task.FromResult(htmlContent)); + + return mock.Object; + } + + protected static IModalMetaDataFactory CreateModalMetaDataFactory() + { + return new DefaultModalMetaDataFactory(); + } + + protected static IDropModalsTagHelperRenderer CreateDefaultDropModalsTagHelperRenderer() + { + var modalMetaDataFactory = CreateModalMetaDataFactory(); + var renderingHelper = CreateRenderingHelper(); + return new DefaultDropModalsTagHelperRenderer(modalMetaDataFactory, renderingHelper); + } + + protected static ViewContext CreateViewContext() + { + return Mock.Of(); + } +} diff --git a/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalsTagHelperRendererTests.cs b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalsTagHelperRendererTests.cs new file mode 100644 index 0000000..fa146ba --- /dev/null +++ b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Modals/DefaultDropModalsTagHelperRendererTests.cs @@ -0,0 +1,70 @@ +namespace Rhythm.Drop.Web.Tests.TagHelperRenderers.Modals; +using Rhythm.Drop.Models.Modals; +using Rhythm.Drop.Web.Infrastructure.MetaData; +using Rhythm.Drop.Web.Infrastructure.TagHelperRenderers.Modals; +using System.Threading.Tasks; + +public class DefaultDropModalsTagHelperRendererTests : DefaultDropModalTagHelperRendererTestsBase +{ + [Test] + public async Task RenderAsync_With_Modals_And_No_TagName_Returns_Output_With_No_TagName() + { + // arrange + var tagHelperRenderer = CreateDefaultDropModalsTagHelperRenderer(); + var model = CreateRendererContext([new FakeModal()]); + var context = CreateTagHelperContext(DefaultTagName); + var output = CreateTagHelperOutput(DefaultTagName); + + // act + await tagHelperRenderer.RenderAsync(model, context, output); + + // assert + Assert.That(output.TagName, Is.Default); + Assert.That(output.TagName, Is.Not.EqualTo(DefaultTagName)); + } + + [Test] + public async Task RenderAsync_With_TagName_And_No_Modals_Returns_Output_With_No_TagName() + { + // arrange + var tagHelperRenderer = CreateDefaultDropModalsTagHelperRenderer(); + var model = CreateRendererContext([]); + var context = CreateTagHelperContext(DefaultTagName); + var output = CreateTagHelperOutput(DefaultTagName); + + // act + await tagHelperRenderer.RenderAsync(model, context, output); + + // assert + Assert.That(output.TagName, Is.Default); + Assert.That(output.TagName, Is.Not.EqualTo(DefaultTagName)); + } + + [Test] + public async Task RenderAsync_With_TagName_And_Modals_Returns_Output_With_Expected_TagName() + { + // arrange + const string NewTagName = "section"; + var tagHelperRenderer = CreateDefaultDropModalsTagHelperRenderer(); + var model = CreateRendererContext([new FakeModal()], NewTagName); + var context = CreateTagHelperContext(DefaultTagName); + var output = CreateTagHelperOutput(DefaultTagName); + + // act + await tagHelperRenderer.RenderAsync(model, context, output); + + // assert + Assert.That(output.TagName, Is.EqualTo(NewTagName)); + } + + private static DropModalsTagHelperRendererContext CreateRendererContext(IModal[] modals) + { + return CreateRendererContext(modals, default); + } + private static DropModalsTagHelperRendererContext CreateRendererContext(IModal[] modals, string? tagName) + { + var viewContext = CreateViewContext(); + + return new DropModalsTagHelperRendererContext(modals, DefaultTheme, tagName, viewContext, default); + } +}