-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (Tests): adds link tag helper renderer tests
- Loading branch information
1 parent
0e6f2af
commit fbfaac0
Showing
2 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...lperRenderers/Components/FakeComponent.cs → ...Tests/TagHelperRenderers/FakeComponent.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
99 changes: 99 additions & 0 deletions
99
tests/Rhythm.Drop.Web.Tests/TagHelperRenderers/Links/DefaultLinkTagHelperRendererTests.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,99 @@ | ||
namespace Rhythm.Drop.Web.Tests.TagHelperRenderers.Links; | ||
|
||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Rhythm.Drop.Models.Common.Attributes; | ||
using Rhythm.Drop.Models.Links; | ||
using Rhythm.Drop.Models.Modals; | ||
using Rhythm.Drop.Web.TagHelperRenderers.Links; | ||
using System.Threading.Tasks; | ||
|
||
[TestFixture] | ||
public class DefaultLinkTagHelperRendererTests : TagHelperRendererTestsBase | ||
{ | ||
private const string DefaultUrl = "#"; | ||
|
||
private const string DefaultLabel = "Click Me"; | ||
|
||
private const string HrefAttributeName = "href"; | ||
|
||
[Test] | ||
public async Task RenderAsync_With_No_Link_Should_Return_Nothing() | ||
{ | ||
var tagHelperRenderer = new DefaultDropLinkTagHelperRenderer(); | ||
var context = CreateTagHelperContext(DefaultTagName); | ||
var output = CreateTagHelperOutput(DefaultTagName); | ||
|
||
await tagHelperRenderer.RenderAsync(default, context, output); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(output.TagName, Is.Not.EqualTo(DefaultTagName)); | ||
Assert.That(output.Content.IsModified, Is.False); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task RenderAsync_With_Anchor_Link_Should_Return_Modified_Content() | ||
{ | ||
var tagHelperRenderer = new DefaultDropLinkTagHelperRenderer(); | ||
var context = CreateTagHelperContext(DefaultTagName); | ||
var output = CreateTagHelperOutput(DefaultTagName); | ||
var link = CreateAnchorLink(DefaultUrl); | ||
|
||
await tagHelperRenderer.RenderAsync(link, context, output); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(output.TagName, Is.EqualTo("a")); | ||
Assert.That(output.Attributes, Has.One.Matches<TagHelperAttribute>(x => x.Name == HrefAttributeName && x.Value.ToString() == DefaultUrl)); | ||
Assert.That(output.Content.IsModified, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task RenderAsync_With_Modal_Link_Should_Return_Modified_Content() | ||
{ | ||
var tagHelperRenderer = new DefaultDropLinkTagHelperRenderer(); | ||
var context = CreateTagHelperContext(DefaultTagName); | ||
var output = CreateTagHelperOutput(DefaultTagName); | ||
|
||
var modal = new Modal("test", [new FakeComponent()]); | ||
var link = new ModalLink(modal, "Click Me", ReadOnlyHtmlAttributeCollection.Empty()); | ||
|
||
await tagHelperRenderer.RenderAsync(link, context, output); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(output.TagName, Is.EqualTo("button")); | ||
Assert.That(output.Attributes, Has.One.Matches<TagHelperAttribute>(x => x.Name == "data-modal-target" && x.Value.ToString() == "test")); | ||
Assert.That(output.Content.IsModified, Is.True); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task RenderAsync_With_Link_And_Existing_Content_Should_Return_Only_Outter_Modified_Content() | ||
{ | ||
var tagHelperRenderer = new DefaultDropLinkTagHelperRenderer(); | ||
var context = CreateTagHelperContext(DefaultTagName); | ||
var output = CreateTagHelperOutput(DefaultTagName, new HtmlString("Existing Content")); | ||
|
||
var link = CreateAnchorLink(DefaultUrl); | ||
await tagHelperRenderer.RenderAsync(link, context, output); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(output.TagName, Is.EqualTo("a")); | ||
Assert.That(output.Attributes, Has.One.Matches<TagHelperAttribute>(x => x.Name == HrefAttributeName && x.Value.ToString() == DefaultUrl)); | ||
Assert.That(output.Content.IsModified, Is.False); | ||
}); | ||
} | ||
|
||
private static AnchorLink CreateAnchorLink(string url) | ||
{ | ||
var linkAttributes = new HtmlAttributeCollection(); | ||
linkAttributes.SetAttribute("href", url); | ||
|
||
return new AnchorLink(DefaultLabel, linkAttributes.ToReadOnly()); | ||
} | ||
} |