From 9000e9ea09c53f1900e0334381aa52665ad01cf6 Mon Sep 17 00:00:00 2001 From: Jamie Pollock Date: Fri, 26 Jan 2024 17:57:49 -0500 Subject: [PATCH] feat (Tests): adds tests for DefaultDropPictureTagHelperRenderer --- .../DefaultPictureTagHelperRendererTests.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Images/DefaultPictureTagHelperRendererTests.cs diff --git a/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Images/DefaultPictureTagHelperRendererTests.cs b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Images/DefaultPictureTagHelperRendererTests.cs new file mode 100644 index 0000000..5048c67 --- /dev/null +++ b/tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Images/DefaultPictureTagHelperRendererTests.cs @@ -0,0 +1,84 @@ +namespace Rhythm.Drop.Web.Tests.TagHelperRenderers.Images; + +using Rhythm.Drop.Web.Infrastructure.TagHelperRenderers.Images; +using Rhythm.Drop.Web.Infrastructure; +using Rhythm.Drop.Web.TagHelperRenderers.Images; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Rhythm.Drop.Models.Images; +using Microsoft.AspNetCore.Html; + +[TestFixture] +public class DefaultPictureTagHelperRendererTests : TagHelperRendererTestsBase +{ + private const string PictureTagName = "picture"; + + private static readonly IHtmlContent _imgHtmlContent = new HtmlString(""); + + [Test] + public async Task RenderAsync_With_No_Existing_Img_Tag_Should_Return_Output_With_Modified_Content() + { + // arrange + var tagHelperRenderer = new DefaultDropPictureTagHelperRenderer(); + var image = new Image("/image.gif", "Test", 200, 200); + var rendererContext = new DropImageTagHelperRendererContext(image, LoadingMode.Default); + var context = CreateTagHelperContext(PictureTagName); + var output = CreateTagHelperOutput(PictureTagName); + + await tagHelperRenderer.RenderAsync(rendererContext, context, output); + + Assert.That(output.Content.IsModified, Is.True); + } + + [Test] + public async Task RenderAsync_With_Existing_Img_Tag_Should_Return_Output_With_No_Modified_Content() + { + // arrange + var tagHelperRenderer = new DefaultDropPictureTagHelperRenderer(); + var image = new Image("/image.gif", "Test", 200, 200); + var rendererContext = new DropImageTagHelperRendererContext(image, LoadingMode.Default); + var context = CreateTagHelperContext(PictureTagName); + var output = CreateTagHelperOutput(PictureTagName, _imgHtmlContent); + + await tagHelperRenderer.RenderAsync(rendererContext, context, output); + + Assert.That(output.Content.IsModified, Is.False); + } + + [Test] + public async Task RenderAsync_With_Img_And_Source_Tag_Should_Return_Output_With_Modified_PreContent() + { + // arrange + var tagHelperRenderer = new DefaultDropPictureTagHelperRenderer(); + var image = new Image("/image.gif", "Test", 200, 200, [ new ImageSource("/image2.gif")]); + var rendererContext = new DropImageTagHelperRendererContext(image, LoadingMode.Default); + var context = CreateTagHelperContext(PictureTagName); + var output = CreateTagHelperOutput(PictureTagName, _imgHtmlContent); + + await tagHelperRenderer.RenderAsync(rendererContext, context, output); + + Assert.That(output.PreContent.IsModified, Is.True); + } + + [Test] + public async Task RenderAsync_With_No_Img_Should_Return_Surpressed_Output() + { + // arrange + var tagHelperRenderer = new DefaultDropPictureTagHelperRenderer(); + var rendererContext = new DropImageTagHelperRendererContext(default, LoadingMode.Default); + var context = CreateTagHelperContext(PictureTagName); + var output = CreateTagHelperOutput(PictureTagName, _imgHtmlContent); + + await tagHelperRenderer.RenderAsync(rendererContext, context, output); + + Assert.Multiple(() => + { + Assert.That(output.TagName, Is.Default); + Assert.That(output.PreContent.IsEmptyOrWhiteSpace, Is.True); + Assert.That(output.Content.IsEmptyOrWhiteSpace, Is.True); + }); + } +}