Skip to content

Commit

Permalink
feat (Tests): adds modal THR tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiepollock committed Feb 7, 2024
1 parent 78502bd commit ad778d1
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/FakeModal.cs
Original file line number Diff line number Diff line change
@@ -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<IComponent> Content => [ new FakeComponent() ];

public IReadOnlyHtmlAttributeCollection Attributes => ReadOnlyHtmlAttributeCollection.Empty();

public IReadOnlyCollection<IComponent> GetComponents()
{
return Content;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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("<div>Test</div>"));
}

protected static IRenderingHelper CreateRenderingHelper(IHtmlContent htmlContent)
{
var mock = new Mock<IRenderingHelper>();
mock.Setup(x => x.RenderAsync(It.IsAny<ModalMetaData>())).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<ViewContext>();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ad778d1

Please sign in to comment.